jdbc DBUtils工具类

package jdbc.util;


import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.Properties;


public class DBUtil {


private static String driver;

private static String username;

private static String password;

private static String url;


static{

try{


String file = "jdbc/util/db.properties";

Properties map = new Properties();


InputStream in=DBUtil.class.getClassLoader()

.getResourceAsStream(file);

map.load(in);

System.out.println(map);

in.close();


driver=map.getProperty("driver");

url = map.getProperty("url");

username = map.getProperty("username");

password = map.getProperty("password");


}catch(IOException e){

e.printStackTrace();

throw new RuntimeException(e);

}


}



public static Connection getConnection(){

try{

Class.forName(driver);

Connection conn = DriverManager

.getConnection(url, username, password);

return conn;

}catch(Exception e){

e.printStackTrace();

throw new RuntimeException("木有连接",e);

}

}


public static void close(Connection conn){

if(conn!=null){

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}








猜你喜欢

转载自lambertsprite.iteye.com/blog/2293824