2017年7月6日(新手记错)

时间:2021-12-22 11:19:20

name(红字处)千万不能加引号,会导致出现ClassNotFound:name的错误!!

package com.tao.db;

import java.sql.DriverManager;  
import java.sql.Connection;  


  
public class MysqlConnection {  
public static final String url = "jdbc:mysql://127.0.0.1/test";  
    public static final String name = "com.mysql.jdbc.Driver";  
    public static final String user = "root";  
    public static final String password = "";  
  
private  Connection conn = null;
  
public MysqlConnection() throws Exception {
        try {  
            Class.forName(name);就是这里出错。。。
            System.out.println(url+","+user+","+password);
            this.conn = DriverManager.getConnection(url, user, password);
        }catch(Exception e){
        System.out.println("找不到驱动程序,加载失败。。。");
        throw e;
        }
}
public Connection getConnection(){
return this.conn;
}
public void close() throws Exception{
if(this.conn!=null)
try {
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
throw e;
}
}
}