1. 程式人生 > >自己寫的資料庫的工具類

自己寫的資料庫的工具類

package com.zfy.s05;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DButils {
public static String driver;
public static String url;
public static String user;
public static String pwd;
static{
try {
Properties find = new Properties();
find.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(“find.properties”));
driver = find.getProperty(“mysqlDriver”);
url = find.getProperty(“mysqlUrl”);
user = find.getProperty(“mysqlUser”);
pwd = find.getProperty(“mysqlPwd”);
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
Connection connection = null;
try {
connection = DriverManager.getConnection(url,user,pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
public static Statement getStatement(Connection connection){
Statement statement=null;
try {
statement = connection.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return statement;
}
public static PreparedStatement getPreparedStatement(Connection connection,String sql){
PreparedStatement prepareStatement = null;
try {
prepareStatement = connection.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return prepareStatement;
}

}
find.properties裡的內容

mysqlDriver=com.mysql.jdbc.Driver
mysqlUrl=jdbc:mysql://localhost:3306/自己的資料庫名字
mysqlUser=資料庫使用者名稱
mysqlPwd=資料庫密碼