连接Mysql的jdbc (控制层)
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DBUtil {
Connection conn = null;
PreparedStatement stmt = null;
String Driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://10.137.5.23/mcip?useUnicode=true&characterEncoding=GBK";
String user = "mcip";
String password = "mcip";
ResultSet rs = null;
//完成连接的创建
public Connection getConnection() throws Exception{
Class.forName(Driver);
if(conn == null){
conn = DriverManager.getConnection(url, user, password);
}
return conn;
}
//创建语句对象
public PreparedStatement createStatement(String sql) throws Exception{
stmt = getConnection().prepareStatement(sql);
return stmt;
}
//执行有结果集返回的方法
public ResultSet excuteQuery() throws Exception{
rs = stmt.executeQuery();
return rs;
}
//执行没有结果集返回的方法
public int excuteUpdate() throws Exception{
return stmt.executeUpdate();
}
//关闭对象
public void close(){
if(rs != null)try{rs.close();}catch(Exception e){}
if(stmt != null)try{stmt.close();}catch(Exception e){}
if(conn != null)try{conn.close();}catch(Exception e){}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DBUtil {
Connection conn = null;
PreparedStatement stmt = null;
String Driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://10.137.5.23/mcip?useUnicode=true&characterEncoding=GBK";
String user = "mcip";
String password = "mcip";
ResultSet rs = null;
//完成连接的创建
public Connection getConnection() throws Exception{
Class.forName(Driver);
if(conn == null){
conn = DriverManager.getConnection(url, user, password);
}
return conn;
}
//创建语句对象
public PreparedStatement createStatement(String sql) throws Exception{
stmt = getConnection().prepareStatement(sql);
return stmt;
}
//执行有结果集返回的方法
public ResultSet excuteQuery() throws Exception{
rs = stmt.executeQuery();
return rs;
}
//执行没有结果集返回的方法
public int excuteUpdate() throws Exception{
return stmt.executeUpdate();
}
//关闭对象
public void close(){
if(rs != null)try{rs.close();}catch(Exception e){}
if(stmt != null)try{stmt.close();}catch(Exception e){}
if(conn != null)try{conn.close();}catch(Exception e){}
}
}