我有以下数据库
我使用以下代码尝试返回“text”列中的两个字符串。
但是我遇到了以下错误。
ShorthandText
。textid | text
--------------------------
1 | just a test
2 | just another test
我使用以下代码尝试返回“text”列中的两个字符串。
public List<String> executeSQLWithMulipleReturns(String sql) throws SQLException{
List<String> results = null;
connection = ConnectDb();
stat = connection.createStatement();
resultSet = stat.executeQuery(sql); // sql = "SELECT text FROM ShorthandText;"
while(resultSet.next()){
results.add(resultSet.getString(resultSet.getRow()));
System.out.println(resultSet.toString());
}
closeAll(); // closes all connections
return results;
}
但是我遇到了以下错误。
java.sql.SQLException: column 2 out of bounds [1,1]
at org.sqlite.RS.checkCol(RS.java:64)
at org.sqlite.RS.markCol(RS.java:71)
at org.sqlite.RS.getString(RS.java:245)
at Java.ConnectToDatabase.executeSQLWithMulipleReturns(ConnectToDatabase.java:46)
at GUI.ShorthandText.jButton1ActionPerformed(ShorthandText.java:123)
at GUI.ShorthandText.access$000(ShorthandText.java:18)
at GUI.ShorthandText$2.actionPerformed(ShorthandText.java:82)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6505)
......
以下内容发生在这一行:results.add(resultSet.getString(resultSet.getRow()));
请问有谁能解释一下为什么我会遇到这个错误?
编辑:空指针异常
代码:
while(resultSet.next()){
results.add(resultSet.getString("text"));
System.out.println(resultSet.toString());
}
while(resultSet.next()){
results.add(resultSet.getString(1));
System.out.println(resultSet.toString());
}
两者都会抛出此异常
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Java.ConnectToDatabase.executeSQLWithMulipleReturns(ConnectToDatabase.java:46)
at GUI.ShorthandText.jButton1ActionPerformed(ShorthandText.java:123)
at GUI.ShorthandText.access$000(ShorthandText.java:18)
at GUI.ShorthandText$2.actionPerformed(ShorthandText.java:82)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
.......