BaseDao
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* BaseDao 基础的数据访问对象
*/
public class BaseDao {
private static String DRIVER = "com.mysql.jdbc.Driver";
private static String URL = "jdbc:mysql://127.0.0.1:3306/rainquality";
private static String USERNAME = "root";
private static String PASSWORD = "wangyuxin";
/***
* 获取数据库连接
* @return
*/
public static Connection getConnection() {
try {
Class.forName(DRIVER);
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 关闭链接,释放资源
* @param conn
* @param pstmt
* @param rs
*/
public static void closeAll(Connection conn,PreparedStatement pstmt,ResultSet rs) {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (null != pstmt) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}