如何从不同的表获取值到jTable? Netbeans 和 H2 数据库。 我知道如何使用控制台执行此操作,但如何使用 JFrame JTable 执行此操作 - 我不明白。这段代码应该在哪里?它应该是新类还是应该在 JFrame 中?
请帮忙。
例如:
<小时/> package mainproject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MainProject {
@param args the command line arguments
public static void main(String[] args) throws Exception {
HotelAdmin program = new HotelAdmin();
program.table();
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
try {
Class.forName("org.h2.Driver");
conn = DriverManager.getConnection("jdbc:h2:file://d:\\КУРСАЧ\\database", "sa", "");
//create statement
try{
stmt = conn.createStatement();
} catch (SQLException ex){
Logger.getLogger(MainProject.class.getName()).log(Level.SEVERE,null,ex);
}
rs = stmt.executeQuery("select * from people");
while (rs.next()) {
System.out.println("id= " + rs.getLong("id_p") + ", FIO= " + rs.getString("fio") + ", pasportny dannye= " + rs.getString("pas_dan") + ", telefon= " + rs.getString("telef"));
}
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(MainProject.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (conn != null) {
try {
if(conn !=null) conn.close();
if(stmt !=null)stmt.close();
if(rs !=null)rs.close();
} catch (SQLException ex) {
Logger.getLogger(MainProject.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
最佳答案
我建议您阅读Tutorial on JTable
关于java - 如何从不同的表获取值到jTable.Netbeans和H2数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27862079/