MYSQL&JDBC空指针异常问题求解~~
public class TestMyDataSource {
/*
* 添加用户
* 使用未改造过的Connection
*/
@Test
public void testAddUser(){
Connection conn = null;
PreparedStatement pstmt = null;
//1.创建自定义连接池对象
MyDataSource datasource = new MyDataSource();
try {
//2.从池子中获取连接
conn = datasource.getConnection();
String sql = "insert into tbl_user values(null,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "吕布");
pstmt.setString(2, "貂蝉");
int rows = pstmt.executeUpdate();
if(rows > 0) {
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
datasource.backConnection(conn);
}
}
public class MyDataSource implements DataSource{
//1.创建一个连接,用于存储Connection对象
private static LinkedList<Connection> pool &#