日期:2014-05-16 浏览次数:20465 次
package cn.csdn.web.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JdbcUtil {
/*单例模式 创建Connection对象*/
private static Connection conn =null;
/*URL地址*/
private static final String URL="jdbc:mysql://localhost:3306/3g?user=root&password=root&useUnicode=true&characterEncoding=UTF8";
/*获取Connection对象的静态方法*/
public static Connection getConn(){
if(conn==null){
/*准备驱动程序*/
/*创建连接对象*/
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL);
} catch (Exception e) {
System.out.println("conn创建失败");
e.printStackTrace();
}
}
return conn;
}
public static void release( ResultSet rs,PreparedStatement pstmt ){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}