private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
String sql="insert into invoice (bill_no,name,number,title1,title2,title3,title4) values(?,?,?,?,?,?,?)";
pst=conn.prepareStatement(sql);
pst.setString(1, jTextField1.getText());
pst.setString(2, jTextField2.getText());
pst.setString(3, jTextField3.getText());
//this is my code to insert data from jTable1 into database
//but no entry is made in my database for the following lines
int last=jTable1.getRowCount();
String column1= (String) jTable1.getValueAt(last-1,0);
String column2= (String) jTable1.getValueAt(last-1,1);
String column3= (String) jTable1.getValueAt(last-1,2);
String column4= (String) jTable1.getValueAt(last-1,3);
pst.execute();
JOptionPane.showMessageDialog(null, "data saved");
}
catch(Exception e)
{}
}
这是我将数据插入数据库的代码
显示没有错误,但是当我检查我的数据库时,没有任何条目
将JTable
数据导入数据库,但成功插入了文本字段的数据
请帮助我
给我一个代码,将JTable
中的数据插入数据库。
我是初学者,不知道多少编码。
答案 0 :(得分:0)
您没有错误,因为您没有在catch块中打印错误。
您必须将数据从jTable
设置为pst
。
try
{
String sql="insert into invoice (bill_no,name,number,title1,title2,title3,title4) values(?,?,?,?,?,?,?)";
pst=conn.prepareStatement(sql);
pst.setString(1, jTextField1.getText());
pst.setString(2, jTextField2.getText());
pst.setString(3, jTextField3.getText());
int last=jTable1.getRowCount();
String column1= (String) jTable1.getValueAt(last-1,0);
String column2= (String) jTable1.getValueAt(last-1,1);
String column3= (String) jTable1.getValueAt(last-1,2);
String column4= (String) jTable1.getValueAt(last-1,3);
pst.setString(4,column1);
pst.setString(5,column2);
pst.setString(6,column3);
pst.setString(7,column4);
pst.execute();
JOptionPane.showMessageDialog(null, "data saved");
}
catch(Exception e)
{e.printStackTrace();}
您可以将e.printStackTrace()
放入catch block
。
答案 1 :(得分:0)
这是一个不完整的代码.. 试试这个
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
String sql="insert into invoice (bill_no,name,number,title1,title2,title3,title4) values(?,?,?,?,?,?,?)";
pst=conn.prepareStatement(sql);
pst.setString(1, jTextField1.getText());
pst.setString(2, jTextField2.getText());
pst.setString(3, jTextField3.getText());
int last=jTable1.getRowCount();
String column1= (String) jTable1.getValueAt(last-1,0);
String column2= (String) jTable1.getValueAt(last-1,1);
String column3= (String) jTable1.getValueAt(last-1,2);
String column4= (String) jTable1.getValueAt(last-1,3);
pst.setString(4,column1);
pst.setString(5,column2);
pst.setString(6,column3);
pst.setString(7,column4);
pst.execute();
JOptionPane.showMessageDialog(null, "data saved");
}
catch(Exception e) {
e.printStackTrace();
}
}