java - 如何从不同的表获取值到jTable.Netbeans和H2数据库

标签 java swing netbeans jtable h2

如何从不同的表获取值到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/

相关文章:

java - 没有可用于类型的源代码。您是否忘记继承所需的模块

java - 无法知道标准 JButton 的大小

java - Netbeans/java : Could not find or load main class library

java - 向 JTextArea 添加 double ?

java - 模态对话框阻止正确的光标状态

java - 不能将 EJB 放在单独的 jar 中

php - 你能找到数组的项目类型吗?

java - 如何检测已接电话?

java - TextView 中的 setOnClickListener

java - 从文本文件中检索字符串数据并将其存储到对象中