dbconfig.properties
driver com.mysql.jdbc.Driver
url jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=gb2312
username root
password root
DBConfig
public class DBConfig {
private Properties props;
private Connection con = null;
public Connection getConnection(){
try{
props = new Properties();
props.load(this.getClass().getResourceAsStream("dbconfig.properties"));
Class.forName(props.getProperty("driver"));
con = DriverManager.getConnection(props.getProperty("url"),props.getProperty("username"),props.getProperty("password"));
}catch(Exception ex){
ex.printStackTrace();
}
return con;
}
public static void main(String args[]){
DBConfig db = new DBConfig();
System.out.print(db.getConnection());
}
}