一 JDBC配置
- 上MySQL官网 下载驱动程序JAR文件(下载 independent版本最好,注意:里面包含示例),并添加到ClassPath中
- 启动MySQL服务器
- 注册驱动器类(两种方式1 Class.forName("com.mysql.jdbc.Driver") 2 System.setProperty("jdbc.driver","com.mysql.jdbc.Driver:org.postgresql.driver");//多个驱动之间用冒号分开)
- 利用JDBC URL获得Connection Connection conn = DriverManager.getConnection("jdbc:mysql://" + HOST + ":" + PORT + "/" + DATABASE, USER, PASS);
- 获得Statement stat = connection.createStatement();
例程
package com.lmk.test.mysqltest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.Scanner;
import com.lmk.test.InfoUtil;
public class MySQLTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MySQLTest test = new MySQLTest();
try{
Connection connection = test.getConnection();
Statement stat;
try {
stat = connection.createStatement();
test.excute(stat);
} finally{
connection.close();
}
} catch (SQLException e){
e.printStackTrace();
}
}
private void excute(Statement stat) {
// TODO Auto-generated method stub
String query = null;
try {
FileInputStream in = new FileInputStream("sql.txt");
Scanner scanner = new Scanner(in);
while(scanner.hasNextLine()){
query = scanner.nextLine();
try {
stat.execute(query);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Connection getConnection() {
// TODO Auto-generated method stub
FileInputStream in = null;
String driver = "com.mysql.jdbc.Driver";
String url ="jdbc:mysql://localhost/lmk";
String username = "root";
String password = "root";
Properties prop = new Properties();
try {
in = new FileInputStream("mysql.properties");
prop.load(in);
driver= prop.getProperty("driver");
// InfoUtil.info(prop.getProperty("driver"));
url = prop.getProperty("url");
username = prop.getProperty("username");
password = prop.getProperty("password");
in.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
}
try {
Class.forName(driver).newInstance();//注册驱动器类;
// System.setProperty("jdbc.driver","com.mysql.jdbc.Driver:org.postgresql.driver");//多个驱动之间用冒号分开
File file = new File("driver.log");
PrintWriter writer = new PrintWriter(file);
DriverManager.setLogWriter(writer);//设置由 DriverManager 和所有驱动程序使用的日志/追踪 PrintWriter 对象
return DriverManager.getConnection(url+"?user="+username+"&password="+password);//使用数据库URL返回一个Connection对象
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}