java.lang.ClassCastException: org.apache.derby.client.am.ClientPreparedStatement42 无法转换为 org.apache.derby.iapi.sql.PreparedStatement
java.lang.ClassCastException: org.apache.derby.client.am.ClientPreparedStatement42 cannot be cast to org.apache.derby.iapi.sql.PreparedStatement
我想创建我的第一个与 apache-derby 相关的项目。
public Circle getCircle(int circleId){
Connection conn = null;
try {
conn = dataSource.getConnection();
**PreparedStatement ps = (PreparedStatement) conn.prepareStatement("SELECT * FROM circle where id = ?");
((java.sql.PreparedStatement) ps).setInt(1,circleId);
Circle circle = null;
ResultSet rs = ((java.sql.PreparedStatement) ps).executeQuery();
if(rs.next()){
circle = new Circle(circleId,rs.getString("name"));
}**
rs.close();
((Connection) ps).close();
return circle;
}
catch (Exception e) {
throw new RuntimeException(e);
}
finally{
try{
conn.close();
}catch (SQLException e){}
}
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}
dataSource 在我的 .xml 文件中定义为 bean。使用或不使用 Spring 我有一个与准备好的语句相关的错误:
线程 "main" java.lang.RuntimeException 中的异常:java.lang.ClassCastException:org.apache.derby.client.am.ClientPreparedStatement42 无法转换为 org.apache.derby.iapi.sql.PreparedStatement
在 JdbcDemo.dao.JdbcDaoImpl.getCircle(JdbcDaoImpl.java:42)
在 JdbcDemo.JdbcDemo.main(JdbcDemo.java:17)
原因:java.lang.ClassCastException:org.apache.derby.client.am.ClientPreparedStatement42 无法转换为 org.apache.derby.iapi.sql.PreparedStatement
在 JdbcDemo.dao.JdbcDaoImpl.getCircle(JdbcDaoImpl.java:30)
... 1 个
有人可以帮助我吗?我可以添加其他 class,但我认为问题位于此 class 中。我正在使用 apache 10.12.1.1。感谢您的帮助:)
您似乎对 PreparedStatement
使用了错误的导入。您的导入应该是:
import java.sql.PreparedStatement;
应该不是org.apache.derby.iapi.sql.PreparedStatement
的任何导入,这不是你应该在这里使用的东西。
您的代码中不需要任何转换:
PreparedStatement ps = conn.prepareStatement("SELECT * FROM circle where id = ?");
ps.setInt(1, circleId);
Circle circle = null;
ResultSet rs = ps.executeQuery();
我想创建我的第一个与 apache-derby 相关的项目。
public Circle getCircle(int circleId){
Connection conn = null;
try {
conn = dataSource.getConnection();
**PreparedStatement ps = (PreparedStatement) conn.prepareStatement("SELECT * FROM circle where id = ?");
((java.sql.PreparedStatement) ps).setInt(1,circleId);
Circle circle = null;
ResultSet rs = ((java.sql.PreparedStatement) ps).executeQuery();
if(rs.next()){
circle = new Circle(circleId,rs.getString("name"));
}**
rs.close();
((Connection) ps).close();
return circle;
}
catch (Exception e) {
throw new RuntimeException(e);
}
finally{
try{
conn.close();
}catch (SQLException e){}
}
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}
dataSource 在我的 .xml 文件中定义为 bean。使用或不使用 Spring 我有一个与准备好的语句相关的错误:
线程 "main" java.lang.RuntimeException 中的异常:java.lang.ClassCastException:org.apache.derby.client.am.ClientPreparedStatement42 无法转换为 org.apache.derby.iapi.sql.PreparedStatement 在 JdbcDemo.dao.JdbcDaoImpl.getCircle(JdbcDaoImpl.java:42) 在 JdbcDemo.JdbcDemo.main(JdbcDemo.java:17) 原因:java.lang.ClassCastException:org.apache.derby.client.am.ClientPreparedStatement42 无法转换为 org.apache.derby.iapi.sql.PreparedStatement 在 JdbcDemo.dao.JdbcDaoImpl.getCircle(JdbcDaoImpl.java:30) ... 1 个
有人可以帮助我吗?我可以添加其他 class,但我认为问题位于此 class 中。我正在使用 apache 10.12.1.1。感谢您的帮助:)
您似乎对 PreparedStatement
使用了错误的导入。您的导入应该是:
import java.sql.PreparedStatement;
应该不是org.apache.derby.iapi.sql.PreparedStatement
的任何导入,这不是你应该在这里使用的东西。
您的代码中不需要任何转换:
PreparedStatement ps = conn.prepareStatement("SELECT * FROM circle where id = ?");
ps.setInt(1, circleId);
Circle circle = null;
ResultSet rs = ps.executeQuery();