package com.ata.dao;
import java.sql.Connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
* 连接数据库类
*/
public class DBUtil {
public static Connection getConn(){
Connection conn=null;
String url="jdbc:mysql://localhost:3306/ezu?useUnicode=true&characterEncoding=GBK";
String driver="com.mysql.jdbc.Driver";
String user="root";
String pwd="123";
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,user,pwd);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public static Statement getStmt(){
Statement stmt=null;
try{
stmt=getConn().createStatement();
}catch(Exception e){
e.printStackTrace();
}
return stmt;
}
public static ResultSet getRs(String sql){
ResultSet rs=null;
try{
rs=getStmt().executeQuery(sql);
}catch(Exception e){
e.printStackTrace();
}
return rs;
}
public static void close(ResultSet rs){
Connection conn=getConn();
Statement stmt=getStmt();
try{
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void close(){
Connection conn=getConn();
Statement stmt=getStmt();
try{
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void close(Connection conn,Statement stmt){
try{
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
*
* @param conn
* @param sql
* @param returnGeneratedKeys
* @param returnGeneratedKeys
* @return
*/
public static PreparedStatement getPsmt(Connection conn,String sql, int returnGeneratedKeys){
PreparedStatement ps = null;
if( null != conn){
try {
ps = conn.prepareStatement(sql,returnGeneratedKeys);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return ps;
}
public static PreparedStatement getPsmt(Connection conn,String sql){
PreparedStatement ps = null;
if( null != conn){
try {
ps = conn.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return ps;
}
}
import java.sql.Connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
* 连接数据库类
*/
public class DBUtil {
public static Connection getConn(){
Connection conn=null;
String url="jdbc:mysql://localhost:3306/ezu?useUnicode=true&characterEncoding=GBK";
String driver="com.mysql.jdbc.Driver";
String user="root";
String pwd="123";
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,user,pwd);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public static Statement getStmt(){
Statement stmt=null;
try{
stmt=getConn().createStatement();
}catch(Exception e){
e.printStackTrace();
}
return stmt;
}
public static ResultSet getRs(String sql){
ResultSet rs=null;
try{
rs=getStmt().executeQuery(sql);
}catch(Exception e){
e.printStackTrace();
}
return rs;
}
public static void close(ResultSet rs){
Connection conn=getConn();
Statement stmt=getStmt();
try{
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void close(){
Connection conn=getConn();
Statement stmt=getStmt();
try{
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void close(Connection conn,Statement stmt){
try{
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
*
* @param conn
* @param sql
* @param returnGeneratedKeys
* @param returnGeneratedKeys
* @return
*/
public static PreparedStatement getPsmt(Connection conn,String sql, int returnGeneratedKeys){
PreparedStatement ps = null;
if( null != conn){
try {
ps = conn.prepareStatement(sql,returnGeneratedKeys);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return ps;
}
public static PreparedStatement getPsmt(Connection conn,String sql){
PreparedStatement ps = null;
if( null != conn){
try {
ps = conn.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return ps;
}
}