JAVA JDBC编程 DBUtil 报错 Exception in thread "main" java.lang.NullPointerException

Exception in thread “main” java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at DBUtil.getConnection(DBUtil.java:29)
at DBUtil.main(DBUtil.java:104)

DBUtil 29行:Class.forName(driver);

DBUtil 104行:System.out.println(db.getConnection());

错误原因:
在mysql.properties 每项的前面加上了jdbc.
jdbc.driver…去掉了就好了

//mysql.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/student0010?useUnicode=true&characterEncoding=utf8&useSSL=false
user=root
password=123456

在这里插入图片描述

贴上其他代码:

//DBUtil.java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DBUtil {
	
	private Connection conn;
	private PreparedStatement preStmt;
	private ResultSet rs;

	public static Connection getConnection() throws SQLException {
		Properties p=new Properties();
		File file=new File("config"+File.separator+"mysql.properties");
		try {
			p.load(new FileInputStream(file));
			String user=p.getProperty("user");
			String password=p.getProperty("password");
			String driver=p.getProperty("driver");
			String url=p.getProperty("url");
			
			Class.forName(driver);
			
			return DriverManager.getConnection(url, user, password);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SQLException("加载配置文件失败!");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SQLException("数据库驱动程序未找到!");
		}
		
	}
	
	
	public void closeAll() {
		try {
			if(null!=rs)
				rs.close();
			if(null!=preStmt)
				preStmt.close();
			if(null!=conn)
				conn.close();
			
		}catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public ResultSet executeQuery(String sql, Object...params)throws Exception
	{
		conn=this.getConnection();
		if(null==sql)
			return null;
		
		preStmt=conn.prepareStatement(sql);
		if(null!=params && params.length>0) {
			for(int i=0;i<params.length;i++) {
				preStmt.setObject(i+1, params[i]);
			}
		}
		
		rs=preStmt.executeQuery();
		
		return rs;
	}
	
	public int executeUpdate(String sql, Object...params)throws SQLException
	{
		
		int result=0;
		
		
		
		if(null==sql)
			return 0;
		
		conn=this.getConnection();
		preStmt=conn.prepareStatement(sql);
		
		if(null!=params && params.length>0) {
			for(int i=0;i<params.length;i++) {
				preStmt.setObject(i+1, params[i]);
			}
		}
		
		result=preStmt.executeUpdate();
		
		return result;
	}
	
	public static void main(String[] args) throws Exception {
		DBUtil db=new DBUtil();
        System.out.println(db.getConnection());
	}
}

书上说要写config.java,我没写也可以

//config.java
import java.io.FileInputStream;
import java.util.Properties;

public class config {
	private static Properties p=null;
	static {
		try {
			p=new Properties();
			p.load(new FileInputStream("config/mysql.properties"));
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	public static String getValue(String key) {
		return p.get(key).toString();
	}
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值