本帖最后由 geely2317 于 2010-08-06 16:38:26 编辑

解决方案 »

  1.   

    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();
            }
    }
      

  2.   

    一般 最后一个try catch不用的
      

  3.   


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

  4.   

    换高点的oracle jdbc驱动版本试试
      

  5.   


    我的意思是包括try catch里面的内容都可以省略
      

  6.   

    恩看了大侠的关闭方式,的确比原来的好多了。但是rs和pst真的有isclosed方法....
    谢谢大侠。
      

  7.   

    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();
    }
    }
    }
    }
    }