Java 如何将Jtable数据插入oracle 11g数据库
我们已经创建了一个jtable,数据通过add按钮添加到表中。它通过submit按钮发送到数据库。但是当点击submit按钮时会生成异常。有人能告诉我代码哪里出错了吗Java 如何将Jtable数据插入oracle 11g数据库,java,jdbc,oracle11g,Java,Jdbc,Oracle11g,我们已经创建了一个jtable,数据通过add按钮添加到表中。它通过submit按钮发送到数据库。但是当点击submit按钮时会生成异常。有人能告诉我代码哪里出错了吗 btnSubmit.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { try { int count = table.getRowC
btnSubmit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try {
int count = table.getRowCount();
String driver = "oracle.jdbc.driver.OracleDriver";
Class.forName(driver);
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott", "tiger");
conn.setAutoCommit(false);
PreparedStatement pst = conn.prepareStatement("Insert into students(ROLL_NO,NAME,MID1,MID2,Marksinwords) values (?,?,?,?,?)");
for(int row = 0; row<count; row++)
{
String roll = (String)table.getValueAt(row, 0);
String name = (String)table.getValueAt(row, 1);
String MARKS1 = (String)tableModel.getValueAt(row, 2);
String MARKS2 = (String)table.getValueAt(row, 3);
String marksinwords = (String)table.getValueAt(row, 4);
pst.setString(1, roll);
pst.executeUpdate(roll);
pst.setString(2, name);
pst.executeUpdate(name);
pst.setString(3,MARKS1 );
pst.executeUpdate(MARKS1);
pst.setString(4,MARKS2 );
pst.executeUpdate(MARKS2);
pst.setString(5, marksinwords);
pst.executeUpdate(marksinwords);
pst.addBatch();
}
pst.executeBatch();
conn.commit();
} catch (Exception ex) {
Logger.getLogger(FacultyTableSubmit.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
btnSubmit.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
试一试{
int count=table.getRowCount();
String driver=“oracle.jdbc.driver.OracleDriver”;
Class.forName(驱动程序);
Connection conn=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:orcl”、“scott”、“tiger”);
连接设置自动提交(错误);
PreparedStatement pst=conn.prepareStatement(“插入学生(卷号、姓名、MID1、MID2、标记文字)值(?,,,,?)”;
对于(int row=0;row您的PreparedStatement序列在我看来有点奇怪,在更新它时不需要提供值,您可以做两件事,将它添加到db中,然后再添加到批处理中
试着这样做:
for(int row = 0; row<count; row++)
{
String roll = (String)table.getValueAt(row, 0);
String name = (String)table.getValueAt(row, 1);
String MARKS1 = (String)tableModel.getValueAt(row, 2);
String MARKS2 = (String)table.getValueAt(row, 3);
String marksinwords = (String)table.getValueAt(row, 4);
pst.setString(1, roll);;
pst.setString(2, name);
pst.setString(3,MARKS1 );
pst.setString(4,MARKS2 );
pst.setString(5, marksinwords);
pst.addBatch();
}
pst.executeBatch();
conn.commit();
用于(int row=0;rowhich exception?我已经添加了屏幕截图。请不要将异常作为屏幕截图发布,始终显示文本。如果幸运的话,我们会自动向您推荐一篇文章,如果它能够识别先前文章中已发现的异常,您可以在其中找到解决方案。此外,您的异常不会显示在c中完全地,它被剪掉了。谢谢你的建议。我改变了代码。同样的例外@Fredy Fisher