private static final long serialVersionUID = 1L
private JTable table// 声明表格
void Stmessege11() {
setSize(460, 320)
setVisible(true)
}
public Vector getDataVector() throws SQLException, ClassNotFoundException {
Connection conn = null
java.sql.Statement stmt = null
Class.forName("com.mysql.jdbc.Driver")
String dburl = "jdbc:mysql://localhost:3306/hp"
String dbuser = "root"
String dbpassword = "19920102"
conn = DriverManager.getConnection(dburl, dbuser, dbpassword)
stmt = conn.createStatement()
String sql = "select* from xsgli"
ResultSet rs = stmt.executeQuery(sql)
Vector dataVector = new Vector()
while (rs.next()) {
Vector rowVector = new Vector()
for (int i = 1i <= 7i++) {
if (i == 2 || i == 4) {
rowVector.add(String.valueOf(rs.getInt(i)))
} else {
rowVector.add(rs.getString(i))
}
}
dataVector.add(rowVector)
}
return dataVector
}
public Vector getColumnVector() {
Vector columnVector = new Vector()
columnVector.add("姓名")
columnVector.add("学号")
columnVector.add("性别")
columnVector.add("班级")
columnVector.add("系别")
columnVector.add("成绩")
columnVector.add("专业")
return columnVector
}
public Stmessege1() throws SQLException, ClassNotFoundException {
super()
setTitle("学生学籍数据显示")
setBounds(100, 100, 382, 223)
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
Connection conn = null
Statement stmt = null
Class.forName("com.mysql.jdbc.Driver")
String dburl = "jdbc:mysql://localhost:3306/hp"
String dbuser = "root"
String dbpassword = "19920102"
conn = DriverManager.getConnection(dburl, dbuser, dbpassword)
java.sql.Statement st = conn.createStatement()
String sql = "select* from xsgli"
ResultSet rs = st.executeQuery(sql)
Vector columnVector = getColumnVector()
Vector dataVector = getDataVector()
final JScrollPane scrollPane = new JScrollPane()
getContentPane().add(scrollPane, BorderLayout.CENTER)
table = new JTable(dataVector, columnVector)
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF)
scrollPane.setViewportView(table)
}
}
希望能帮到你,谢谢采纳
每次点击的时候都重新创建MyTable并且加到Frame里面,看下是不是被之前加入的Table盖住了。或者换个写法,窗口初始化的时候同时创建表格,点击添加数据直接更新数据就行了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)