程序拒绝从MySQL数据库中获取字符串

时间:2014-09-29 09:57:02

标签: java mysql exception jdbc jbutton

我有一个JList (称为' listerine'),它成功从我的MySQL数据库中的表中提取整个列。现在我有一个jButton,它将从数据库(称为dosrocket)中提取两个字符串,然后将命令传递给Apache Commons Exec API来执行。

以下代码如下:

   private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    String selgame=listerine.getSelectedValue().toString();
    String qu="select filepath from dosrocket where name="+"'"+selgame+"'";
    String gname=null,gpath=null;
    try{
            ras=st.executeQuery(qu); 
            gname=ras.getString(3);
            gpath=ras.getString(4);

         String command="C:/Program Files/DOSBox-0.74/DOSBox.exe";
    CommandLine cmdinstance=CommandLine.parse(command);
    cmdinstance.addArgument("mount c:"+gpath);
    cmdinstance.addArgument(gname);
    DefaultExecutor exac=new DefaultExecutor();
    int exitval=exac.execute(cmdinstance);
    }
    catch (Exception e){
        e.printStackTrace();
    }
    try{
        ras.close();    
        st.close();
        con.close();
    }
    catch(SQLException e){
        e.printStackTrace();
    }

}        

但是,按下按钮后出现此错误:

java.sql.SQLException: Before end of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:855)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5773)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5693)
at Client.jButton2ActionPerformed(Client.java:518)
at Client.access$1400(Client.java:28)
at Client$12.actionPerformed(Client.java:194)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 31 seconds)

请帮忙!

2 个答案:

答案 0 :(得分:0)

傻傻的我。当我的桌子只提供一个时,我引用了第3列和第4列。

更正后的代码:

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    String selgame=listerine.getSelectedValue().toString();
    String qu="select filename,filepath from dosrocket where name="+"'"+selgame+"'";
    String gname=null,gpath=null;
    try{
            ras=st.executeQuery(qu);
            while(ras.next()){
                gname=ras.getString(1);
            gpath=ras.getString(2);
            }
         String cdcommand="cd C:/Program Files/DOSBox-0.74";

         CommandLine cmdinstance=CommandLine.parse(cdcommand);

    cmdinstance.addArgument("dosbox");
          cmdinstance.addArgument("mount c:"+gpath);
    cmdinstance.addArgument(gname);

    DefaultExecutor exac=new DefaultExecutor();

    int exitval2=exac.execute(cmdinstance);
    }
    catch (Exception e){
        e.printStackTrace();
    }
    try{
        ras.close();    
        st.close();
        con.close();
    }
    catch(SQLException e){
        e.printStackTrace();
    }

}             

答案 1 :(得分:0)

如前所述,您需要在访问返回的数据之前调用resultSet.next()。

如果可以的话,我还有三条评论 -  1.使用PreparedStatement而不是Statement。它效率更高。代码只编译一次,而不是每次执行。

PreparedStatemt ps = con.createPreparedStatement ("select filename,filepath from dosrocket where name= ?");
ps.setString (1, selgame);
rs = ps.execute ();
  1. 访问resultSet值时,我更喜欢使用列的名称而不是其数字位置。我觉得它更安全。

  2. 在catch异常中
  3. ,调用connection.rollback();