创建 JDBC 应用程序

huangapple go评论77阅读模式
英文:

Creating JDBC Application

问题

我有这段代码:

try {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection(
        "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8", "root", "Icdjoil100");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select * from t_user");
    while (rs.next())
        //System.out.println(rs.getInt(0) + "  " + rs.getString(1));
    con.close();
} catch (Exception e) {
    e.printStackTrace();
}

但我遇到了这个错误:

java.sql.SQLException: 在 ResultSet 关闭后不允许进行操作
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
    at com.mysql.jdbc.ResultSet.checkClosed(ResultSet.java:666)
    at com.mysql.jdbc.ResultSet.next(ResultSet.java:7274)
    at Test.main(Test.java:19)
英文:

I have this piece of code:

try{
			Class.forName("com.mysql.jdbc.Driver");
			Connection con= DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8","root","Icdjoil100");
			Statement stmt=con.createStatement();
			ResultSet rs=stmt.executeQuery("select * from t_user");
			while(rs.next())
				//System.out.println(rs.getInt(0)+"  "+rs.getString(1));
			con.close();
		}catch(Exception e){
			e.printStackTrace();
		}

but I have this error:

java.sql.SQLException: Operation not allowed after ResultSet closed
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
	at com.mysql.jdbc.ResultSet.checkClosed(ResultSet.java:666)
	at com.mysql.jdbc.ResultSet.next(ResultSet.java:7274)
	at Test.main(Test.java:19)

答案1

得分: 1

由于您将打印语句注释掉了,您的循环现在正在关闭连接(以及其所有相关资源)。您的代码,未被注释掉的那一行如下:

while(rs.next())
    con.close();
英文:

Since you commented out your print statement, your loop is now closing the connection (and all of its dependent resources). Your code, without the commented out line:

while(rs.next())
    con.close();

答案2

得分: 1

应该是这样的:

while(rs.next()){
    System.out.println(rs.getInt(0)+"  "+rs.getString(1));
}
con.close();

你漏掉了 {}。

英文:

It should be something like :

while(rs.next()){
    System.out.println(rs.getInt(0)+"  "+rs.getString(1));
}
con.close();

You are missing {}

答案3

得分: 1

不必手动关闭连接,最好利用JDK 7的功能,在try语句后自动关闭资源。链接提供了关于try-with-resources的参考。

Class.forName("com.mysql.jdbc.Driver");

try (
    Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8", "root", "Icdjoil100");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select * from t_user")
) {
    while (rs.next()) {
        //System.out.println(rs.getInt(0) + "  " + rs.getString(1));
    }
}

即使您不想使用JDK 7的功能,最好也在finally块中关闭资源,以便在出现异常时仍然可以正确关闭资源。

Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8", "root", "Icdjoil100");
    stmt = con.createStatement();
    rs = stmt.executeQuery("select * from t_user");
    while (rs.next()) {
        System.out.println(rs.getInt(0) + "  " + rs.getString(1));
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException e) {
        }
    }
    if (stmt != null) {
        try {
            stmt.close();
        } catch (SQLException e) {
        }
    }
    if (con != null) {
        try {
            con.close();
        } catch (SQLException e) {
        }
    }
}
英文:

Instead of manually closing connection its better to use benefit of jdk 7 to automatically close resources after try. Link for reference for try-with-resources.

Class.forName("com.mysql.jdbc.Driver");


  try (
        Connection con= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8","root","Icdjoil100");
        Statement stmt=con.createStatement();
        ResultSet rs=stmt.executeQuery("select * from t_user")) {
              while(rs.next())
            //System.out.println(rs.getInt(0)+"  "+rs.getString(1));

}

Even if you don't want to use benefit of jdk 7, its better to close resolurces in finally block so that if there is any exception, it would still be closed correctly.

    Connection con = null;
    Statement stmt= null;
    ResultSet rs = null;
    try{
            Class.forName("com.mysql.jdbc.Driver");
            con= DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8","root","Icdjoil100");
            stmt=con.createStatement();
            rs=stmt.executeQuery("select * from t_user");
            while(rs.next())
                System.out.println(rs.getInt(0)+"  "+rs.getString(1));
            
        }catch(Exception e){
            e.printStackTrace();
        } finally {
       if (rs != null) {
          try {
             rs.close();
           } catch (SQLException e) {  }
         }
      if (ps != null) {
         try {
             ps.close();
         } catch (SQLException e) {  }
      }
      if (conn != null) {
         try {
           conn.close();
      } catch (SQLException e) {  }
   }

 }

huangapple
  • 本文由 发表于 2020年9月26日 22:18:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64078729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定