【问题标题】:How to insert a Jtable data into oracle 11g database [closed]如何将 Jtable 数据插入 oracle 11g 数据库 [关闭]
【发布时间】:2018-03-19 16:12:50
【问题描述】:

These are the exceptions.我们创建了一个jtable,通过add按钮将数据添加到表中。通过提交按钮发送到数据库。但是单击提交按钮时会产生异常。谁能告诉我在哪里代码出错了??

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

    }

});

【问题讨论】:

  • 哪个例外?
  • 我已经添加了截图
  • 请不要将异常发布为屏幕截图,始终显示文本。如果幸运的话,SO 会自动向您推荐一个帖子,如果它能够识别出之前帖子中已经存在的异常,您可以在其中找到解决方案。除了你的异常没有完全显示,它被切断了
  • 好的。谢谢您的建议

标签: java jdbc oracle11g


【解决方案1】:

你的 PreparedStatement 序列对我来说有点奇怪,更新它时不需要提供值,你做了两件事,将它添加到数据库,然后再次添加到批处理中。

试试这样:

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

【讨论】:

  • 我已经更改了代码。同样的例外@Fredy Fisher
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 2014-08-08
  • 1970-01-01
  • 2012-08-21
  • 2015-02-02
  • 2015-02-19
相关资源
最近更新 更多