工具类的第二种写法(jdbc中获取连…
2016-05-12 00:17
316 查看
package jdbc.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
private static final String URL =
"jdbc:mysql://localhost:3306/school";
private static final String USER = "root";
private static final String PASSWORD =
"root";
public static Connection getConnection() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn =
DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException
e) {
// TODO
Auto-generated catch block
e.printStackTrace();
} catch (SQLException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void closeResource3(Connection
conn, Statement stmt,
ResultSet rs)
{
try {
if (rs !=
null) {
rs.close();
}
} catch (SQLException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
} finally {
try {
if
(stmt != null) {
stmt.close();
}
} catch
(SQLException e) {
e.printStackTrace();
} finally
{
try
{
if
(conn != null) {
conn.close();
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
private static final String URL =
"jdbc:mysql://localhost:3306/school";
private static final String USER = "root";
private static final String PASSWORD =
"root";
public static Connection getConnection() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn =
DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException
e) {
// TODO
Auto-generated catch block
e.printStackTrace();
} catch (SQLException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void closeResource3(Connection
conn, Statement stmt,
ResultSet rs)
{
try {
if (rs !=
null) {
rs.close();
}
} catch (SQLException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
} finally {
try {
if
(stmt != null) {
stmt.close();
}
} catch
(SQLException e) {
e.printStackTrace();
} finally
{
try
{
if
(conn != null) {
conn.close();
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
相关文章推荐
- 可以将获得连接对象和关闭资源打包…
- mysql数据库实现CURD操作
- mysql基本功能
- tomcat的目录结构
- 149@365
- 133@365   可看懂???
- 130@365   the romance of coders
- 2014年04月26日
- 开启Linux系统路由转发功能 实现多网段电脑共享上网
- CentOS系统NAT共享上网
- 2016-04-04-Ejabberd-configure-db-postgresql
- X Window System配置
- 深度下降法轨迹-七月算法(julyedu.com)4 月机器学习算法班学习笔记
- 107@365  Html网页设计…
- 106@365大型互联网企业面试题整理&…
- HTML 开发规范
- CentOS 6.6下双网卡共享上网配置
- 技术培训 | 大数据分析处理与用户画像实践
- java中有那些关键字?
- Leetcode 104. Maximum Depth of Binary Tree