- package com.util;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- /**
- * @className: ConnUtil.java
- * @classDescription: 数据库连接工具类——包含取得连接和关闭资源
- * @function:
- * @author: Wentasy
- * @createTime: 2012-9-24 上午11:51:15
- * @modifyTime:
- * @modifyReason:
- * @since: JDK 1.6
- */
- public class ConnUtil {
- public static final String url = "jdbc:mysql://XXX.XXX.XXX.XXX:3306/dbadapter";
- public static final String user = "root";
- public static final String password = "XXXXXX";
- /**
- * 得到连接
- * @return
- * @throws SQLException
- * @throws ClassNotFoundException
- */
- public static Connection establishConn() throws SQLException,ClassNotFoundException{
- Class.forName("com.mysql.jdbc.Driver");
- return DriverManager.getConnection(url, user, password);
- }
- /**
- * 关闭连接
- * @param conn
- * @throws SQLException
- */
- public static void close(Connection conn) throws SQLException{
- if(conn != null){
- conn.close();
- conn = null;
- }
- }
- /**
- * 关闭PreparedStatement
- * @param pstmt
- * @throws SQLException
- */
- public static void close(PreparedStatement pstmt) throws SQLException{
- if(pstmt != null){
- pstmt.close();
- pstmt = null;
- }
- }
- /**
- * 关闭结果集
- * @param rs
- * @throws SQLException
- */
- public static void close(ResultSet rs) throws SQLException{
- if(rs != null){
- rs.close();
- rs = null;
- }
- }
- }
作者:候鸟 出处:http://www.cnblogs.com/swite/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |