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.Driver
url = jdbc:mysql://localhost:3306/bbs
username = test
password = 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了两次后,不是同一对像,所以找不到。
版权声明:本文为u012851188原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。