将数据从JDBC数据库检索到Jtable中 [英] Retrieving Data from JDBC Database into Jtable

查看:93
本文介绍了将数据从JDBC数据库检索到Jtable中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我已成功将我的jTable链接到JDBC数据库。
但是,我无法检索它们。我想在重新启动程序时显示已保存的数据,但它不能正常工作。

Hi I have successfully linked my jTable to JDBC database. However, I am having trouble retrieving them. I want the saved data to appear when I restart the program but it is not working.

alarm.setText("");  
    DefaultTableModel model =(DefaultTableModel) hwList.getModel();
    if(!className.getText().trim().equals(""))
    {
        model.addRow(new Object[]{className.getText(), homeWork.getText(), dueDate.getText()});
    }
    else
    {
        alarm.setText("Class Name should not be blank.");
    }

        Connection conn;
        Statement st;

        try
    {
        String myDriver = "com.mysql.jdbc.Driver";
        String myUrl = "jdbc:mysql://localhost:3306/mysql";
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Connecting to database");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "");
        System.out.println("Connected to databse");


        String a = className.getText();
        String b = homeWork.getText();
        String c = dueDate.getText();            

        System.out.println("Inserting into the table");
        st = conn.createStatement();
        String stmt="INSERT INTO hwList (className, homeWork, dueDate)"+ "VALUES ("+"\'"+a+"\',"+"\'"+b+"\',"+"\'"+c+"\')";
        System.out.println(stmt);
        st.executeUpdate(stmt);

        System.out.println("Saved!");

    }
    catch (Exception e)
    {
        System.err.println("Got an exception!");
        System.err.println(e.getMessage());
    }    

这是我保存文件的代码!

This is my code for saving the document!

有没有办法在JDBC数据库中检索数据并通过Jtable显示它?
我很抱歉问这么简单的问题,但我是java的新手,我迫切需要帮助!

Is there any way to retrieve data in JDBC data base and show it through Jtable? I'm so sorry for asking such a simple question but I am new to java and I desperately need help!

非常感谢你!

用于加载数据的代码...

Code used to load the data...

顺便说一句,我的jtable是一个包含三列的3列表 - className,homeWork,dueDate。
谢谢!

Btw, my jtable is a 3 column table with three columns--className, homeWork, dueDate respectively. Thank you!

String sql="SELECT * FROM hwList";
ResultSet rs = st.executeQuery(sql);
while(rs.next())
{
    String d = rs.getString("className");
    String e = rs.getString("homeWork");
    String f = rs.getString("dueDate");
}


推荐答案

首先创建一个新 TableModel ...

Start by creating a new TableModel...

DefaultTableModel model = new DefaultTableModel(new String[]{"Class Name", "Home work", "Due Date"}, 0);

从数据库加载数据......

Load the data from the database...

String sql="SELECT * FROM hwList";
ResultSet rs = st.executeQuery(sql);

将每行数据添加到表模型中...

Add each row of data to the table model...

while(rs.next())
{
    String d = rs.getString("className");
    String e = rs.getString("homeWork");
    String f = rs.getString("dueDate");
    model.addRow(new Object[]{d, e, f});
}

将模型应用于 JTable ...

Apply the model to your JTable...

table.setModel(model);

您可能还想查看 try-with-resources声明并确保您正确管理资源

You may also want to look at The try-with-resources Statement and make sure you're managing your resources properly

这篇关于将数据从JDBC数据库检索到Jtable中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆