欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

连接Mysql的jdbc (控制层)_MySQL

程序员文章站 2022-05-01 12:36:47
...
bitsCN.com 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){}
}
} bitsCN.com
相关标签: jdbc null