Class.forName(driver)出错。(解决)

来源:互联网 发布:刷新页面的js 编辑:程序博客网 时间:2024/05/17 03:08

Class.forName(“com.mysql.jdbc.Driver”);木有问题。
Class.forName(driver);则出错。
但是driver = com.mysql.jdbc.Driver
源码:

package test20160319;import java.io.FileInputStream;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;import java.util.Properties;public class ExecuteDLL {    private String driver;    private String url;    private String user;    private String pass;    public void initParam(String paraFile) throws Exception, IOException{        Properties props = new Properties();        props.load(new FileInputStream(paraFile));        driver = props.getProperty("driver");        url = props.getProperty("url");        user = props.getProperty("user");        pass = props.getProperty("pass");        System.out.println("driver:"+driver);    }    public void createTable(String sql) throws Exception{        //Class.forName("com.mysql.jdbc.Driver");        Class.forName(driver);        try(                Connection conn = DriverManager.getConnection(url,user,pass);                //Connection conn = DriverManager.getConnection(url+"?"+"user="+user+"&password="+pass);                Statement stmt = conn.createStatement()){            stmt.executeUpdate(sql);        }    }    public static void main(String[] args) throws Exception, Exception {        // TODO Auto-generated method stub        new ExecuteDLL().initParam("paramFile.ini");        new ExecuteDLL().createTable("create table jdbc_test(jdbc_id int auto_increment primary key,jdbc_name varchar(255),jdbc_desc text );");    }}

配置文件:

[JDBC]driver = com.mysql.jdbc.Driverurl = jdbc:mysql://localhost:3306/bbsusername = testpassword = test

出错的地方在:

new ExecuteDLL().initParam("paramFile.ini");        new ExecuteDLL().createTable("create table jdbc_test(jdbc_id int auto_increment primary key,jdbc_name varchar(255),jdbc_desc text );");

new了两次后,不是同一对像,所以找不到。

0 0
原创粉丝点击