Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

java - Retrieving Data from JDBC Database into Jtable

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!

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!

Thank you so much!

Code used to load the data...

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");
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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});
}

Apply the model to your JTable...

table.setModel(model);

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...