package com.wl.sm.framework.model.util;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DBUtil {
private List<Connection> list = null;
private Connection conn = null;
private Statement sta = null;
private ResultSet rs = null;
/*private void connection2DB() {
try {
Class.forName(“com.miccrosoft.sqlserver.jdbc.SQLServerDriver”);
this.conn = DriverManager.getConnection(“jdbc:sqlserver://127.0.0.1:1433;DatabaseName=schoolmis_new”, “sa”, “sa”);
System.out.println(“数据库连接成功!”);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
private Connection getConnection() {
Connection myconn = null;
try {
Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);
this.conn = DriverManager.getConnection(“jdbc:sqlserver://127.0.0.1:1433;DatabaseName = schoolmis_new”,”sa”,”sa”);
System.out.println(“数据库连接成功!”);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return myconn;
}
public DBUtil() {
this.list = new ArrayList<Connection>();
for(int i = 0;i < 5; i++) {
this.list.add(this.getConnection());
}
this.conn = this.list.get(0);
}
public int update(String sql) {
int n = -1;
try {
this.sta = this.conn.createStatement(); //Exception in thread “main” java.lang.NullPointerException
at com.wl.sm.framework.model.util.DBUtil.update(DBUtil.java:58)
at com.wl.sm.framework.model.util.DBUtilTest.main(DBUtilTest.java:9)
System.out.println(“7”);
n = this.sta.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return n;
}
public ResultSet query(String sql) {
try {
this.sta = this.conn.createStatement();
this.rs = this.sta.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.rs;
}
public void close() {
try {
if(this.rs != null) {
this.rs.close();
this.rs = null;
}
if(this.sta != null) {
this.sta.close();
this.sta = null;
}
if(this.conn != null) {
this.conn.close();
this.conn = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DBUtil {
private List<Connection> list = null;
private Connection conn = null;
private Statement sta = null;
private ResultSet rs = null;
/*private void connection2DB() {
try {
Class.forName(“com.miccrosoft.sqlserver.jdbc.SQLServerDriver”);
this.conn = DriverManager.getConnection(“jdbc:sqlserver://127.0.0.1:1433;DatabaseName=schoolmis_new”, “sa”, “sa”);
System.out.println(“数据库连接成功!”);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
private Connection getConnection() {
Connection myconn = null;
try {
Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);
this.conn = DriverManager.getConnection(“jdbc:sqlserver://127.0.0.1:1433;DatabaseName = schoolmis_new”,”sa”,”sa”);
System.out.println(“数据库连接成功!”);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return myconn;
}
public DBUtil() {
this.list = new ArrayList<Connection>();
for(int i = 0;i < 5; i++) {
this.list.add(this.getConnection());
}
this.conn = this.list.get(0);
}
public int update(String sql) {
int n = -1;
try {
this.sta = this.conn.createStatement(); //Exception in thread “main” java.lang.NullPointerException
at com.wl.sm.framework.model.util.DBUtil.update(DBUtil.java:58)
at com.wl.sm.framework.model.util.DBUtilTest.main(DBUtilTest.java:9)
System.out.println(“7”);
n = this.sta.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return n;
}
public ResultSet query(String sql) {
try {
this.sta = this.conn.createStatement();
this.rs = this.sta.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.rs;
}
public void close() {
try {
if(this.rs != null) {
this.rs.close();
this.rs = null;
}
if(this.sta != null) {
this.sta.close();
this.sta = null;
}
if(this.conn != null) {
this.conn.close();
this.conn = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
解决方案
40
空指针嘛,看看是不是你的conn空了