您好我已成功将我的jTable链接到JDBC数据库.
但是,我无法检索它们.我想在重新启动程序时显示保存的数据,但它不起作用.
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());
}
这是我保存文档的代码!
有没有办法在JDBC数据库中检索数据并通过Jtable显示它?
我很抱歉提出这么简单的问题,但我是java新手,我迫切需要帮助!
非常感谢!
用于加载数据的代码……
顺便说一下,我的jtable是一个包含三列的3列表 – className,homeWork,dueDate.
谢谢!
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");
}