gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:42:21 25 4
gpt4 key购买 nike

我有一个 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)

请帮忙!

最佳答案

我真傻。当我的表只提供一列时,我正在引用第 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();
}

}

关于java - 程序拒绝从 MySQL 数据库获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26097173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com