DriverManager:
管理不同厂商提供的驱动
Connection:
连接数据库服务器
Statement:
负责操作sql语句
ResultSet:
接收回收的结果集
public class Mysql_封装 {
private String driver = "com.mysql.cj.jdbc.Driver"; //mysql驱动
private String user = "root"; //用户
private String pwd = "12345678"; //密码≤≥
private String port = "3306"; //端口号
private String IP = "127.0.0.1"; //数据库服务IP
private String dbName; //库名
private String url;
private Connection connection; //2.连接数据库
private Statement statement; //3.获取statement对象
private ResultSet resultSet; //4.执行SQL语句
//使用者必须提供数据库名
public Mysql_封装 (String dbName) {
this.dbName = dbName;
this.url = "jdbc:mysql://" + this.IP + ":" + this.port + "/"
+ this.dbName + "?useUnicode=true&characterEncoding=UTF8&useSSL=false";
}
//使用者提供信息
public Mysql_封装 (String user, String pwd, String IP, String port, String dbName) {
this.user = user;
this.pwd = pwd;
this.IP = IP;
this.port = port;
this.dbName = dbName;
this.url = "jdbc:mysql://" + this.IP + ":" + this.port + "/"
+ this.dbName + "?useUnicode=true&characterEncoding=UTF8&useSSL=false";
}
//连接数据库
private boolean connDB() {
try {
//1.加载驱动
Class.forName(this.driver);
//2.连接数据库
this.connection = DriverManager.getConnection(this.url, this.user, this.pwd);
//3.获取statement对象,执行mysql语句
this.statement = this.connection.createStatement();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
//关闭的方法
private void closeDB() {
try {
if (this.statement != null) {
this.statement.close();
this.statement = null;
}
if (this.resultSet != null) {
this.resultSet.close();
this.resultSet = null;
}
if (this.connection != null && !this.connection.isClosed()) {
this.connection.close();
this.connection = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* (1)非查询操作的方法: 增 删 改
*/
public int execuUpdate(String sql) {
if (!connDB()) {
closeDB();
System.out.println("连接数据库失败");
return 0;
}
//说明连接成功
//受影响的行数
int row = 0;
try {
//4.执行SQL语句,返回值为影响的行数, executeUpdate(sql)获取 增 删 改的信息
row = this.statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeDB();
}
return row; //返回受影响的行数
}
/*
* (2)查询操作的方法
*/
public ArrayList<Map<String, Object>> execQuery(String sql) {
if (!connDB()) {
closeDB();
System.out.println("数据库连接失败");
return null;
}
//连接成功
ArrayList<Map<String, Object>> list = new ArrayList<>();
try {
//4.获取结果集 executeQuery(sql) 获取查询的信息
this.resultSet = statement.executeQuery(sql);
//获取结果集的信息,该表的列数,每列的数据类型等信息
ResultSetMetaData metaData =(ResultSetMetaData)this.resultSet.getMetaData();
//获取总列数
int count = metaData.getColumnCount();
this.resultSet.beforeFirst(); //每次从第一行取
//每次while循环取出来一行,然后将行分割成列
while(this.resultSet.next()) {
//一个map是一个记录
HashMap<String, Object> map = new HashMap<>();
for (int i = 1; i <= count; i++) {
//获取字段名做键(sid, name, sex...)
String key = metaData.getColumnName(i);
//获取值
Object value = this.resultSet.getObject(i);
map.put(key, value);
}
list.add(map);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeDB();
}
return list;
}
/*
* (3)可以执行多条sql语句
*/
public long[] execuUpdateSqls(List<String> sqls) {
if (!connDB()) {
closeDB();
System.out.println("连接数据失败");
return null;
}
if (sqls==null || sqls.size()==0) {
System.out.println("没有可执行的语句");
return null;
}
try {
for (String sql : sqls) {
this.statement.addBatch(sql); //一条
}
return this.statement.executeLargeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeDB();
}
return null;
}
public long[] execuUpdateSqls(String sqls) {
if (sqls==null || sqls.equals("")) { //第一个是没有地址 第二个是有地址没有值
System.out.println("没有执行语句");
return null;
}
//将多条语句切割开
String[] sqlArray = sqls.split(";");
List<String> list = Arrays.asList(sqlArray); //将字符串数组转化为集合
return execuUpdateSqls(list);
}
}
public class java_Mysql封装 {
public static void main(String[] args) {
Mysql_封装 sc = new Mysql_封装("javastu"); //实例化对象
ArrayList<Map<String, Object>> list = sc.execQuery("select * from st");
for (Map<String, Object> map : list) {
System.out.println("一条记录");
Set<String> set = map.keySet();
for (String key : set) {
System.out.println(key + ":" + map.get(key));
}
}
}
}