#--------------------mysql localhost----------------------------#
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/lin
jdbc.username=root
jdbc.password=linxing123
#--------------------oracle localhost----------------------------#
#jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
#jdbc.username=root
#jdbc.password=root
#--------------------sqlserver localhost----------------------------#
#jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
#jdbc.url=jdbc:sqlserver://192.168.199.112;database=mydb;user=test1;password=123456";
#jdbc.username=test1
#jdbc.password=123456
==========================================================
package demo0605;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* @author:Avery
* @description:jdbc封装
* @date:2017年6月6日下午5:23:16
*/
public class DBUtity {
private static Properties properties=new Properties();
private static String driver=null;
private static String url=null;
private static String user=null;
private static String pwd=null;
static{
try{
properties.load(
DBUtity.class.getClassLoader().
getResourceAsStream("resource/jdbc.properties"));
driver=properties.getProperty("jdbc.driverClassName");
url=properties.getProperty("jdbc.url");
user=properties.getProperty("jdbc.username");
pwd=properties.getProperty("jdbc.password");
Class.forName(driver);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException();
}
}
public static Connection openConnection() throws SQLException{
return DriverManager.getConnection(url,user,pwd);
}
public static void closeConnection(Connection conn){
if(conn!=null){
try{
conn.close();
}catch(SQLException e){
System.out.println("关闭连接");
}
}
}
}