第1个回答 2017-03-21
没学过 jdbc?
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/gymrent?characterEncoding=utf-8";
private static final String NAME = "root";
private static final String PASSWORD = "123456";
/**
* 与数据库建立连接
*
* @return Connection对象
*/
private Connection getConn() {
Connection conn = null;
try {
Class.forName(DRIVER);//注册(加载)驱动
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(URL,NAME,PASSWORD);//获得数据库连接
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 释放你所传过来的资源对象
*
* @param rs
* @param pstmt
* @param conn
*/
private void closeAll(ResultSet rs, PreparedStatement pstmt, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}