- package com.util;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
-
- 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";
-
-
- public static Connection establishConn() throws SQLException,ClassNotFoundException{
- Class.forName("com.mysql.jdbc.Driver");
- return DriverManager.getConnection(url, user, password);
- }
-
-
- public static void close(Connection conn) throws SQLException{
- if(conn != null){
- conn.close();
- conn = null;
- }
- }
-
-
- public static void close(PreparedStatement pstmt) throws SQLException{
- if(pstmt != null){
- pstmt.close();
- pstmt = null;
- }
- }
-
-
- public static void close(ResultSet rs) throws SQLException{
- if(rs != null){
- rs.close();
- rs = null;
- }
- }
- }
数据库连接工具类——包含取得连接和关闭资源 ConnUtil.java
原文:http://www.cnblogs.com/swite/p/5168678.html