/**
*
*/
package com.salon.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* @author Admin
*
*/
public class DbUtil {
private static String driverName ;//= "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static String url ;//= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=meable_lshhb";
private static String user ;//= "lshhb";
private static String password ;//= "lshhb";
static {
InputStream is = DbUtil.class.getResourceAsStream("/database.properties");
Properties props = new Properties();
try {
props.load(is);
// System.out.println("**************************************");
driverName = props.getProperty("driverName") ;
url = props.getProperty("url") ;
user = props.getProperty("user") ;
password = props.getProperty("password") ;
// System.out.println("driverName"+driverName);
// System.out.println("url"+url);
// System.out.println("user"+user);
// System.out.println("password"+password);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
public static void main(String[] args) {
System.out.println("driverName = " +driverName);
Connection conn = DbUtil.getConnection() ;
System.out.println("conn = " +conn);
}
*/
public static Connection getConnection() {
Connection conn = null ;
try {
Class.forName(driverName) ;
try {
// System.out.println("*************");
conn = DriverManager.getConnection(url, user, password) ;
} catch (SQLException e) {
System.out.println("数据库连接失败!");
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn ;
}
public static void closeConnection(Connection conn) {
if(conn == null) {
try {
conn.close() ;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeStatement(Statement state) {
if(state == null) {
try {
state.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeResultSet(ResultSet rs) {
if(rs == null) {
try {
rs.close() ;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closePreparedStatement(PreparedStatement pstmt) {
if(pstmt == null) {
try {
pstmt.close() ;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}