eclipse中用 jdbc连接是BasicDataSource.getConnection()一直报空指针异常
public class dbcpDateSuource {
private static  final String    ConnectionURL="jdbc:mysql://localhost:3306/wbe1?useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC&useSSL=false";
private static  final String    userName="root";

private static  final String   password="liupeng.123";

private static BasicDataSource ds;

static {
    BasicDataSource ds=new BasicDataSource();
    ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
    ds.setUrl(ConnectionURL);
    ds.setUsername(userName);
    ds.setPassword(password);

    ds.setInitialSize(5);
    ds.setMaxTotal(20);
    ds.setMinIdle(3);
}

public static   Connection getConnection() {
    try {
     return  ds.getConnection(); 
    }
    catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

    public static void close(ResultSet rs,Statement stmt,Connection con) {
        closeResultSet(rs);
        closeStatement(stmt);
        closeConnection(con);;
    }

    public static void close(Statement stmt1,Statement stmt2,Connection con) {
        closeStatement(stmt1);
        closeStatement(stmt2);
        closeConnection(con);;
    }

    private static void closeResultSet(ResultSet rs) {
        try {
            if(rs!=null)
            rs.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void closeStatement(Statement stmt) {
        try {
            if(stmt!=null)
            stmt.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void closeConnection(Connection con) {
        try {
            if(con!=null)con.close();  
        } catch (SQLException e) {
            // TODO Auto-generated catch block
        e.printStackTrace();
            } 
    }

Exception in thread "main" java.lang.NullPointerException
at com.xuexi.jdbc01.dbcpDateSuource.getConnection(dbcpDateSuource.java:39)
at com.xuexi.jdbc01.JDBCDemo01.transferAccounts(JDBCDemo01.java:317)
at com.xuexi.jdbc01.JDBCDemo01.main(JDBCDemo01.java:31)

3个回答

空指针异常,很明显的异常。
private static BasicDataSource ds;//没有实例
在静态代码块使用ds

static {
    ds=new BasicDataSource();
    ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
    ds.setUrl(ConnectionURL);
    ds.setUsername(userName);
    ds.setPassword(password);

    ds.setInitialSize(5);
    ds.setMaxTotal(20);
    ds.setMinIdle(3);
}

public static Connection getConnection() {
try {
return ds.getConnection();
}
catch (SQLException e) {
e.printStackTrace();
}
return null;
}
->
public static Connection getConnection() {
try {
return ds.getConnection();
}
catch (SQLException e) {
e.printStackTrace();
return null;
}

}

ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
改成
ds.setDriverClassName("com.mysql.jdbc.Driver");

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问