在下面的代码中我使用的查询是正确的我在sql server中检查过但是测试在eclipse中失败了我做错了什么?
public int ShipYearLaunched(Connection connection, String ship) throws
SQLException {
String a="select Launched from Ships where name=?";
PreparedStatement st=connection.prepareStatement(a);
st.setString(1, ship);
ResultSet rs =st.executeQuery();
rs.next();
int count = rs.getInt("c");
return count;
}
这就是它在故障追踪中的表现。
com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:206)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:940)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setValue(SQLServerPreparedStatement.java:954)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setString(SQLServerPreparedStatement.java:1532)
at jdbc.JDBC.MostShipsSunkInBattle(JDBC.java:43)
at jdbc.JDBC_Test.TestNumShipsSunkInBattle(JDBC_Test.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
答案 0 :(得分:1)
很难从代码和消息中分辨出你的问题。如果您为表发布创建SQL,将会有所帮助。
我不喜欢我在SQL中看到的内容。我会说它应该是SHIP表,而不是复数表。
您没有遵循Java编码标准。
您不会在方法范围内关闭资源。
我会写更多这样的东西:
private static final String SELECT_YEAR_LAUNCHED_SQL = "select yearLaunched from Ship where name = ?";
public int getShipYearLaunched(Connection connection, String shipName) throws SQLException {
int yearLaunched = 0;
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = connection.prepareStatement(SELECT_LAUNCHED_SQL);
st.setString(1, ship);
rs = st.executeQuery();
// Is there more than one row for a given ship name? If yes, you should get a List<Integer> and append the value returned to the List
while (rs.hasNext()) {
yearLaunched = rs.getInt(1);
}
} finally {
close(rs);
close(ps);
}
return yearLaunched;
}