NetBeans 如何让JTable显示数据库数据?
展开全部
这是将表中查询结果在表格中显示,你可以参考一下,希望对你有用!
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载驱动器类
Connection con = DriverManager.getConnection("jdbc:odbc:JuneText", "", ""); //创建的JuneText数据源名称
Statement statement = con.createStatement(); //创建执行对象
String sql=null;
String id = jtf2.getText().trim();
if (id.equals("")) {
sql = "select * from student";
} else {
sql = "select * from student where 学号='" + id + "'";
}
ResultSet rs = statement.executeQuery(sql);
int i = 0, j = 0;
while (rs.next()) {
jTable1.setValueAt(rs.getString(1), i, j);
jTable1.setValueAt(rs.getString(2), i, j + 1);
jTable1.setValueAt(rs.getString(3), i, j + 2);
jTable1.setValueAt(rs.getString(4), i, j + 3);
jTable1.setValueAt(rs.getString(5), i, j + 4);
i = i + 1;
j = 0;
}
rs.close();
statement.close();
} catch (Exception e) {
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载驱动器类
Connection con = DriverManager.getConnection("jdbc:odbc:JuneText", "", ""); //创建的JuneText数据源名称
Statement statement = con.createStatement(); //创建执行对象
String sql=null;
String id = jtf2.getText().trim();
if (id.equals("")) {
sql = "select * from student";
} else {
sql = "select * from student where 学号='" + id + "'";
}
ResultSet rs = statement.executeQuery(sql);
int i = 0, j = 0;
while (rs.next()) {
jTable1.setValueAt(rs.getString(1), i, j);
jTable1.setValueAt(rs.getString(2), i, j + 1);
jTable1.setValueAt(rs.getString(3), i, j + 2);
jTable1.setValueAt(rs.getString(4), i, j + 3);
jTable1.setValueAt(rs.getString(5), i, j + 4);
i = i + 1;
j = 0;
}
rs.close();
statement.close();
} catch (Exception e) {
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询