连接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
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
推荐阅读
-
Linux下远程连接MySQL数据库的方法
-
分享一个在C#中用MySql.Data.MySqlClient连接MySql的实验过程
-
Linux连接mysql报错:Access denied for user ‘root’@‘localhost’(using password: YES)的解决
-
php 连接mysql连接被重置的解决方法
-
pycharm2017实现python3.6与mysql的连接
-
php与mysql建立连接并执行SQL语句的代码
-
Linux连接mysql报错:Access denied for user ‘root’@‘localhost’(using password: YES)的解决
-
JSP使用JDBC连接MYSQL数据库的方法
-
在ASP中连接MySQL数据库,最好的通过ODBC方法
-
.net(C#数据库访问) Mysql,Sql server,Sqlite,Access四种数据库的连接方式