Statement stat = conn.createStatement();不运行

用jdbc链接数据库,然后查询内容输出到jsp页面上,奇怪的是,数据库可以连上,但是查询不出来结果,执行到Statement stat = conn.createStatement();这一句话没有执行。
下面是两个代码文件。
LinkSQL.java


public class LinkSQL {
    protected Statement stat = null;
    protected Connection conn = null;

    protected PreparedStatement pstmt=null;
    public void queryAllStudents()  {

    ArrayList students = new ArrayList();
    try {    // 数据库地址 + 要使用的数据库名

        // System.out.println("hsjfhuewifeu");
        String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String dbURL="jdbc:sqlserver://localhost:1433;database=A";
        //String dbURL="jdbc:sqlserver://127.0.0.1\\MES;DataBaseName=数据库2";
        Class.forName(driverName);
        String userName="a";

        String userPwd="123";
        Connection conn= DriverManager.getConnection(dbURL,userName,userPwd);
        System.out.println(conn);
        System.out.println("SQL server数据库连接成功!");
        //Statement stat=conn.createStatement();


    } catch (Exception e) {
        e.printStackTrace();
    }

}

    public void closeAll(){
        try {
//            if (rs != null) {
//                rs.close();
//            }
            if (stat != null) {
                stat.close();
            }
            if (conn != null) {
                conn.close();
            }
            if(pstmt!=null){
                pstmt.close();
            }
        }catch(Exception e) {

        }
    }


}

INSERT.java

public class INSERT extends LinkSQL {
    public int Insert(code n) {
        int row = 0;
        try {
            super.queryAllStudents();
            String sql = "SELECT * FROM book WHERE price>=" + 10 + " and price<=" + 40;
            System.out.println("!");
            Statement stat = conn.createStatement();
            System.out.println("!");
            System.out.println(sql);

            ResultSet rs=stat.executeQuery(sql);
            while (rs.next()){
                 String a1=rs.getString(1);
                //String a2=rs.getString(2);
                int a2=rs.getInt(2);
                System.out.println(n);
                n.setPrice(a2);
            n.setBookName(a1);}

        } catch (Exception e) {

        }finally {
            super.closeAll();
        }
        return row;
    }
}


img

已经搞这个搞了两天了,同样的代码室友就可以运行出来,各种包也加上了,不知道什么问题

conn是在LinkSQL 这个类里queryAllStudents这个函数里定义的变量
INSERT 类里的Insert函数使用的conn是成员变量,而成员变量是null
Connection conn= DriverManager.getConnection(dbURL,userName,userPwd);
这里把Connection删掉,不要重复定义

你这conn不要在方法声明,直接用成员变量的就行了。

img

也就是Connection conn= DriverManager.getConnection(dbURL,userName,userPwd);改成
conn= DriverManager.getConnection(dbURL,userName,userPwd);