package JDBC;
import java.io.FileInputStream;
import java.sql.*;
import java.util.*;
public class ExecuteDDL {
private String driver;
private String url;
private String user;
private String pass;
Connection conn;
Statement stmt;
public void initParam(String paramFile) throws Exception
{
//使用Properties来加载属性文件
Properties props=new Properties();
props.load(new FileInputStream(paramFile));
driver=props.getProperty("driver");
url=props.getProperty("url");
driver=props.getProperty("user");
driver=props.getProperty("pass");
}
public void createTable(String sql) throws Exception
{
try
{
Class.forName(driver);
conn=DriverManager.getConnection(url,user,pass);
stmt=conn.createStatement();
stmt.executeUpdate(sql);
}
finally//关闭数据库资源
{
if(stmt!=null)
{stmt.close();}
if(conn!=null)
{conn.close();}
}
}
public static void main(String[] args)throws Exception
{
ExecuteDDL ed=new ExecuteDDL();
ed.initParam("mysql.ini");
ed.createTable("create table jdbc test"
+"(jdbc_id int auto_increament primary key,"
+"jdbc_name vachar(255),"
+"jdbc_desc text);");
System.out.println("--------------------建表成功-----------------");
}
}
报错在:
props.load(new FileInputStream(paramFile));
ed.initParam("mysql.ini");
倚天杖