MySQL建立一个连接工具类
public class DBUtil {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs", "root", "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static PreparedStatement prepareStmt(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static ResultSet executeQuery(Statement stmt, String sql) {
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void close(Connection conn, Statement stmt,
PreparedStatement preStatement, ResultSet rs) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if (preStatement != null) {
try {
preStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
preStatement = null;
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}
}
最新文章
- [LeetCode] Combination Sum IV 组合之和之四
- 《Learning Highcharts》中文翻译
- .NET Core全新路线图(译)
- Java系列:JVM指令详解(下)(zz)
- ionic2 页面加载时图片添加的问题
- Beaglebone Back学习七(URAT串口测试)
- 转:尝试用Gearman实现分布式处理(PHP)
- Css的三大机制(特性):特殊性、继承、层叠详解
- CentOS用gparted实现无损调整磁盘分区大小
- 程序员眼中的UML
- ArcGIS网络分析之Silverlight客户端最近设施点分析(四)
- HDU-1495 非常可乐 (嵌套结构体-广搜 对比 一般广搜)
- c#中数组的总结
- C++与C的区别
- paymob QB冲值接口
- HANA 与 Oracle 12c哪一个更快
- InnoDB表空间、段、区
- Strandbeest mechanism and Leg mechanism
- ABP 邮箱设置
- JsonCpp使用方法详解