public class DBHelper {
private static final String URL="jdbc:mysql://localhost:3306/test";
private static final String USERNAME="root";
private static final String PASSWORD="123456";
private static final String DRIVERNAME="com.mysql.jdbc.Driver";
static {
try {
Class.forName(DRIVERNAME);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConn(){
try {
return DriverManager.getConnection(URL,USERNAME,PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public void close(Connection conn, Statement st, ResultSet rs){
if (conn==null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} if (st==null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
} if (rs==null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void close(Connection conn, Statement st){
close(conn,st);
}
public static void main(String[] args) {
DBHelper db=new DBHelper();
Connection conn = db.getConn();
System.out.println(conn);
}