首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >java.sql.SQLException:耗尽的结果集

java.sql.SQLException:耗尽的结果集
EN

Stack Overflow用户
提问于 2015-06-17 03:22:08
回答 1查看 62关注 0票数 -1
代码语言:javascript
复制
add = new JButton("Add",new ImageIcon("D:/Android/Assignment/Images/bookAdd.png"));
            add.setToolTipText("Click Add to Store in Database"); // for toast 
    add.setBackground(new Color(233,214,199));
    add.setForeground(new Color(207,108,40));
    add.setFont(b);
            add.addActionListener(new ActionListener() 
            {
                @Override
                public void actionPerformed(ActionEvent ae) 
                {
                String subject=tf2.getText();
                String title=tf3.getText();
                String author=tf4.getText();
                                    int total_books=Integer.parseInt(tf5.getText());


                    try
            {
                                    DriverManager.registerDriver(new oracle.jdbc.OracleDriver());                   

                Class.forName("oracle.jdbc.driver.OracleDriver");

                Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
                                   System.out.println("11");
                                    String sql="SELECT * from ADD_BOOKS WHERE (SUBJECT=?) AND (BOOK_TITLE=?)AND (BOOK_AUTHOR=?)";
                                 // String sql="SELECT * from ADD_BOOKS VALUES(?,?,?)";

                                   PreparedStatement pst=con.prepareStatement(sql);
                                   // Statement stmt = con.createStatement();
                                        System.out.println("12");
                                   pst.setString(1, subject);
                                   System.out.println("13");
                                   pst.setString(2, title);
                                   System.out.println("14");
                                   pst.setString(3, author);
                                   System.out.println("15");
                                    ResultSet rs=pst.executeQuery();
                                    System.out.println("16");
                                 //    if (rs! = null)
                                  //   {

                                    while(rs.next())
                                         System.out.println("17");
                                    {

                                        System.out.println("18");

                                        String subjj=rs.getString(1);
                                        String titlee=rs.getString(2);
                                        String authorr=rs.getString(3);
                                     int totalBooks=Integer.parseInt(rs.getString(4));

                                   System.out.println("19");
                                    if((subject.equals(subjj)) && (title.equals(titlee)) && (author.equals(authorr)))
                                   {
                                       System.out.println("20");
                                       totalBooks=totalBooks+total_books;


                                        String updateTableSQL = "UPDATE ADD_BOOKS SET TOTAL_BOOKS = TOTAL_BOOKS+totalBooks";
                            //  + " WHERE SUBJECT = ?";
                                     PreparedStatement pstup=con.prepareStatement(updateTableSQL);            
                             //    pstup=con.prepareStatement(updateTableSQL);
                                    pstup.setInt(4, totalBooks);
                                     pstup.executeUpdate();

                                       JOptionPane.showMessageDialog(null,"Successfully Updated");

                                               }
                                       else
                                       {
                                        try
                                        {


                                            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());


                Class.forName("oracle.jdbc.driver.OracleDriver");

                Connection con1=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");

                                  PreparedStatement prep1 = con1.prepareStatement("INSERT INTO ADD_BOOKS VALUES(?,?, ?, ?)");

                                    prep1.setString(1, subject);

                                    prep1.setString(2, title);

                                    prep1.setString(3,author);
                                    prep1.setInt(4,total_books);

                                    prep1.executeUpdate();



                                    String nextRowId = Integer.toString(dm.getRowCount());
                                     dm.addRow(new Object[] { 
                                         nextRowId, 

                                            tf2.getText(), 
                                            tf3.getText(),
                                            tf4.getText(),
                                            tf5.getText()
                                     });

                                      tf2.setText("");
                                      tf3.setText("");
                                      tf4.setText("");
                                      tf5.setText("");
                                     System.out.println("Data Stored Into JTable Successfully");

                                    JOptionPane.showMessageDialog(null, "Data is successfully inserted into database");
                                     }
                                       catch(Exception e)
                    {
                        System.out.println(e.getMessage());
                    JOptionPane.showMessageDialog(null, "error in submitting data");
                    }
                                       }
                                   }}
                    catch(Exception e)
                    {
                        System.out.println(e.getMessage());
                    JOptionPane.showMessageDialog(null, "error in submitting data");
                    }
                }});
EN

回答 1

Stack Overflow用户

发布于 2015-06-17 03:28:24

建议--即使你只有一行代码,也要使用大括号。在本例中,您没有这样做,然后添加了在while循环中执行的System.out,然后在已经检索到数据的情况下尝试获取数据。

如下所示:

代码语言:javascript
复制
while (rs.next()) {
                        System.out.println("17");

                        System.out.println("18");
                  //...
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30876368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档