ps=conn.prepareStatement(sql)这句话怎么会抛指针异常呢
下面是工具类packagecom.tall.jdbc;importjava.sql.Connection;importjava.sql.DriverManager;imp...
下面是工具类
package com.tall.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class DBUtils {
//数据库连接地址
private static String URL;
//用户名
private static String USERNAME;
//密码
private static String PASSWORD;
//驱动程序块
private static String DRIVER;
private static ResourceBundle rb=ResourceBundle.getBundle("com.tall.jdbc.db-config");
private DBUtils(){
}
static{
URL=rb.getString("jdbc.url");
USERNAME=rb.getString("jdbc.username");
PASSWORD=rb.getString("jdbc.password");
DRIVER=rb.getString("jdbc.driver");
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection(){
Connection conn=null;
try {
DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("连接失败!");
}
return conn;
}
public static void close(ResultSet rs,Statement state,Connection conn){
if(rs!=null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(state!=null)
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
下面是测试类
package com.tall.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test {
public static void findAll(){
Connection conn=DBUtils.getConnection();
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select id,name,age,description from person;";
Person p=null;
try{
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
p=new Person();
p.setId(rs.getInt(1));
p.setName(rs.getString(2));
p.setAge(rs.getInt(3));
p.setDescription(rs.getString(4));
System.out.println(p);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DBUtils.close(rs, ps, conn);
}
}
public static void main(String[] args) {
findAll();
}
} 展开
package com.tall.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class DBUtils {
//数据库连接地址
private static String URL;
//用户名
private static String USERNAME;
//密码
private static String PASSWORD;
//驱动程序块
private static String DRIVER;
private static ResourceBundle rb=ResourceBundle.getBundle("com.tall.jdbc.db-config");
private DBUtils(){
}
static{
URL=rb.getString("jdbc.url");
USERNAME=rb.getString("jdbc.username");
PASSWORD=rb.getString("jdbc.password");
DRIVER=rb.getString("jdbc.driver");
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection(){
Connection conn=null;
try {
DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("连接失败!");
}
return conn;
}
public static void close(ResultSet rs,Statement state,Connection conn){
if(rs!=null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(state!=null)
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
下面是测试类
package com.tall.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test {
public static void findAll(){
Connection conn=DBUtils.getConnection();
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select id,name,age,description from person;";
Person p=null;
try{
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
p=new Person();
p.setId(rs.getInt(1));
p.setName(rs.getString(2));
p.setAge(rs.getInt(3));
p.setDescription(rs.getString(4));
System.out.println(p);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DBUtils.close(rs, ps, conn);
}
}
public static void main(String[] args) {
findAll();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询