连接Mysql的jdbc (控制层)
来源:未知 责任编辑:责任编辑 发表时间:2014-04-20 03:42 点击:次
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){}
}
}
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>