JDBC 关闭conn,rs的问题。谢谢大侠们!

geely2317 2010-08-06 04:25:23
不好意思,我是上学的时候,注册的账号,那时候不知道结账,现在结账率是0,现在肯定结账。放心。谢谢啦。


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class JdbcTest {

/**
* @param args
*/
public static void main(String[] args) {

String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String username = "scott";
String password = "tiger";
String sql = "select * from users";

Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;

// TODO Auto-generated method stub
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,username,password);
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();

while(rs.next()){
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));

}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {

rs.close();
pst.close();
conn.close();


} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


try {
System.out.println("rs's closing is :"+rs.isClosed());
System.out.println("pst's closing is :"+pst.isClosed());
System.out.println("conn's closing is :"+conn.isClosed());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
}



结果:

geely
123
Exception in thread "main" java.lang.AbstractMethodError: oracle.jdbc.driver.OracleResultSetImpl.isClosed()Z
at JdbcTest.main(JdbcTest.java:36)


=========================================================================
重点在判断iscloesd()这个方法,如果直接是conn.isclosed();的话,返回true。
加入pst和rs的 都会出现这个ERROR。
请各位高手帮忙,谢谢!
...全文
420 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DB {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/db?user=root&password=");
System.out.println(conn);
pstmt = conn.prepareStatement("select * from person");
rs = pstmt.executeQuery();

while(rs.next()) {
System.out.println(rs.getInt(1));
System.out.println(rs.getString("name"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pstmt != null) {
try {
pstmt.close();
pstmt = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
madFatso 2010-08-06
  • 打赏
  • 举报
回复
ojdbc14.jar?
geely2317 2010-08-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 madfatso 的回复:]
换高点的oracle jdbc驱动版本试试
[/Quote]

我用的是10g的;...
geely2317 2010-08-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kondor 的回复:]
rs和pst好像没有isClosed方法吧
你的关闭链接写的也有问题

Java code

try {
Class.forName(driver);
conn = DriverManager.getConnection(url,username,password);
pst = conn.prepareSt……
[/Quote]


恩看了大侠的关闭方式,的确比原来的好多了。但是rs和pst真的有isclosed方法....
谢谢大侠。
happyfmy 2010-08-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 geely2317 的回复:]

引用 2 楼 xiaoye2892 的回复:
一般 最后一个try catch不用的


不用的话eclipse会提示,就是rs和pst不好使...调用那个方法....
[/Quote]

我的意思是包括try catch里面的内容都可以省略
madFatso 2010-08-06
  • 打赏
  • 举报
回复
换高点的oracle jdbc驱动版本试试
geely2317 2010-08-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xiaoye2892 的回复:]
一般 最后一个try catch不用的
[/Quote]

不用的话eclipse会提示,就是rs和pst不好使...调用那个方法....
happyfmy 2010-08-06
  • 打赏
  • 举报
回复
一般 最后一个try catch不用的
kondor 2010-08-06
  • 打赏
  • 举报
回复
rs和pst好像没有isClosed方法吧
你的关闭链接写的也有问题

try {
Class.forName(driver);
conn = DriverManager.getConnection(url,username,password);
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();

while(rs.next()){
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));

}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

try {
if(rs!=null)
rs.close();
if(pst!=null)
pst.close();
if(conn!=null)
conn.close();


} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧