请查看此代码,告诉我哪里出错了。我的JDBC已连接,但单击确定按钮时仍未显示数据。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel dtm=(DefaultTableModel)t1.getModel();
try
{
st=con.createStatement();
sql="Select * from train";
rs=st.executeQuery(sql);
if (rs.next())
{
tn = rs.getInt("TrainNo");
tna = rs.getString("Train_Name");
s = rs.getString("Start");
d = rs.getDate("Departure");
stp = rs.getString("Stop");
a = rs.getDate("Arrival");
da = rs.getString("Days");
c = rs.getString("Class");
Object []obj={tn,tna,s,d,stp,a,da,c};
dtm.addRow(obj);
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
答案 0 :(得分:1)
可能您忘记添加标题。所以它可能看起来像这样:
String[] header = {"TrainNo","Train Name","Start","Departure","Stop","Arrival","Days","Class"};
DefaultTableModel dtm = new DefaultTableModel(null,header);
try {
st=con.createStatement();
sql="Select * from train";
rs=st.executeQuery(sql);
while ( rs.next() ) {
tn = rs.getInt("TrainNo");
tna = rs.getString("Train_Name");
s = rs.getString("Start");
d = rs.getDate("Departure");
stp = rs.getString("Stop");
a = rs.getDate("Arrival");
da = rs.getString("Days");
c = rs.getString("Class");
Object []obj={tn,tna,s,d,stp,a,da,c};
dtm.addRow(obj);
}
st.close();
rs.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage());
}
t1 = new JTable(dtm);