package com.xiaolan.qq.tools.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class DBUtil {
static String driver;
static String url;
static String username;
static String password;
static Connection conn;
static PreparedStatement pst;
static {
try {
Properties p = new Properties();
InputStream in = DBUtil.class.getResourceAsStream("/resource/jdbc.properties");
System.out.println(in);
p.load(in);
driver = p.getProperty("jdbc.driver");
url = p.getProperty("jdbc.url");
username = p.getProperty("jdbc.username");
password = p.getProperty("jdbc.password");
System.out.println(driver);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static PreparedStatement getPst(String sql){
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
pst = conn.prepareStatement(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return pst;
}
public static void close(ResultSet rs){
try {
if(rs != null) rs.close();
if(pst != null)
pst.close();
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}