将mysal.ini文件放在java progect文件下,不要放在ExecuteDDL.java所在的文件加下!!!
1 import java.io.*; 2 import java.sql.*; 3 import java.util.*; 4 public class ExecuteDDL 5 { 6 private String driver; 7 private String url; 8 private String user; 9 private String pass; 10 11 public void initParam(String paramFile) throws Exception 12 { 13 Properties props = new Properties(); 14 props.load(new FileInputStream(paramFile)); 15 driver = props.getProperty("driver"); 16 url = props.getProperty("url"); 17 user = props.getProperty("user"); 18 pass = props.getProperty("pass"); 19 } 20 public void createTable(String sql) throws Exception 21 { 22 Class.forName(driver); 23 try 24 ( 25 Connection conn = DriverManager.getConnection(url, user, pass); 26 Statement stmt = conn.createStatement() 27 ) 28 { 29 stmt.executeUpdate(sql); 30 } 31 } 32 public static void main(String[] args) throws Exception 33 { 34 ExecuteDDL ed = new ExecuteDDL(); 35 ed.initParam("mysql.ini"); 36 ed.createTable("create table jdbc_test" 37 + "(jdbc_id int auto_increment primary key," 38 + "jdbc_name varchar(255)," 39 + "jdbc_desc text);"); 40 System.out.println("------建表成功-----"); 41 } 42 }