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)
}
}
希望能帮到你,谢谢采纳
一个类似的例子,从数据库里取数据放table里,请参考。
package Libaryimport java.awt.Cursor
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Statement
import java.util.Vector
import javax.swing.*
import javax.swing.table.DefaultTableModel
public class BorrowBookLog extends JFrame{
/**
*
*/
private static final long serialVersionUID = 5922888622610809963L
String BBookName, BBookId
JButton Borrow, Cancel
Statement SearchStmt
@SuppressWarnings({ "rawtypes", "unchecked" })
BorrowBookLog(){
ConDB CB3 = new ConDB()
CB3.connectionDB()
try {
SearchStmt = CB3.dbConn.createStatement()
} catch (SQLException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace()
}
DefaultTableModel tableModel = new DefaultTableModel()
String[] tableHeads= {"图书编号","图书名称","借阅人", "借阅时间"}
Vector cell
Vector row = new Vector()
Vector tableHeadName = new Vector()
for(int i = 0i<tableHeads.lengthi++){
tableHeadName.add(tableHeads[i])
}
try {
ResultSet s =SearchStmt.executeQuery("select * from BorrowedBooks where StudentId ="+ "'"+ StudentMainFrame.Name+"'")
while(s.next()){
cell = new Vector()
cell.add(s.getString("BookId"))
cell.add(s.getString("BookName"))
cell.add(s.getString("StudentId"))
cell.add(s.getString("BorrowedDate"))
row.add(cell)
BBookId = s.getString("BookId")
BBookName = s.getString("BookName")
}
} catch (SQLException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace()
}
setTitle("查询结果")
tableModel.setDataVector(row, tableHeadName)
JTable table = new JTable(tableModel)
table.setRowHeight(20)
table.setCursor(new Cursor(12))
getContentPane().setLayout(null)
JScrollPane scrollPane = new JScrollPane(table)
scrollPane.setBounds(10, 10, 420, 200)
scrollPane.setCursor(new Cursor(12))
this.getContentPane().add(scrollPane)
setLocation(450,220)
setSize(450,300)
setVisible(true)
}
public static void mian(String args[]){
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)