礼悟:
好好学习多思考,尊师重道存感恩。叶见寻根三二一,江河湖海同一体。
虚怀若谷良心主,愿行无悔给最苦。读书锻炼强身心,诚劝且行且珍惜。
数据、数据,命根就在数据。云计算、AI等技术,都是以数据为基础。操作数据库一定要谨慎小心。给最苦 这里的代码,看看就好,要有自己的判断。遇到抉择,要不耻上下问,三思而后行。
javaSE:8
mysql:5.7.14
mysql-connector-java:5.1.44
MySQL-Front:5.4
os:windows7 x64
ide:MyEclipse 2017
特制的异常类
package com.jizuiku;
/**
* 这个类很重要,即完成了抛异常的动作、通过编译,又可以使数据逻辑层的接口保持简洁。
*
* @author 博客园-给最苦
* @version V17.11.08
*/
public class DaoException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
public DaoException() {
// TODO Auto-generated constructor stub
}
public DaoException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public DaoException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public DaoException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public DaoException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}
JDBCUtils类
package com.jizuiku;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
*
*
* @author 给最苦
* @version V17.11.07
*/
public final class JDBCUtils {
/**
* url格式 -> jdbc:子协议:子名称//主机名:端口号/数据库的名字?属性名=属性值&属性名=属性值
* configString变量中有多个参数,需要深入地去研究它们的具体含义
*/
private static String configString = "?useUnicode=true&characterEncoding=utf8&useSSL=true";
private static String url = "jdbc:mysql://localhost:3306/jdbcforjava" + configString;
// 本地的mysql数据库(无子名称) 端口号3306 数据库jdbcforjava
private static String user = "root";
private static String password = "";
// 工具类,直接使用,不可生对象
private JDBCUtils() {
}
// 注册驱动,这里应用的是static代码块只执行一次的特点
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
throw new ExceptionInInitializerError(e);
}
}
/**
* 获取与指定数据库的链接
*
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
/**
* 释放三种资源ResultSet PreparedStatement Connection
*
*/
public static void free(ResultSet rs, PreparedStatement ps, Connection con) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new DaoException(e.getMessage(), e);
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new DaoException(e.getMessage(), e);
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new DaoException(e.getMessage(), e);
}
}
}
}
}
数据库中的内容
测试类
package com.jizuiku;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
*
* @author 博客园-给最苦
* @version V17.11.09
*/
public class Demo {
public static void main(String[] args) {
// 注意看,这里有个坑 -> name as username
// 列名与列的别名
String sql = "select id,name as username,quantity,time,price from book";
List> result = test(sql);
System.out.println("查询到的结果集是:");
System.out.println(result);
}
public static List> test(String sql) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
ResultSetMetaData rsmd = null;
try {
// 注册驱动并建立连接
con = JDBCUtils.getConnection();
// 创建语句 并 对sql语句进行一些预处理
ps = con.prepareStatement(sql);
// 执行语句
rs = ps.executeQuery();
// 得到列的个数
rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
// 因为要存储列的名字,所以要与列的个数相同
String[] colNames = new String[colCount];
/* 装上列的名字
* getColumnLabel 别名
* getColumnLabel()方法的索引是从1开始的。给最苦 写成从0开始,就抛了异常。
*/
for (int i = 1; i <= colCount; i++) {
colNames[i-1] = rsmd.getColumnLabel(i);
}
System.out.println("存储列的别名的数组内容是:");
System.out.println(Arrays.toString(colNames));
// 这里的泛型声明也是有技巧的。
HashMap data = null;
List> datas = new ArrayList>();
// 把查询到的结果装入datas中
while(rs.next()){
data = new HashMap();
for (int i = 0; i < colCount; i++) {
// key value
data.put(colNames[i], rs.getObject(colNames[i]));
}
datas.add(data);
}
// 返回结果
return datas;
} catch (SQLException e) {
throw new DaoException(e);
} finally {
// 释放资源
JDBCUtils.free(rs, ps, con);
}
}
}
控制台输出的结果
存储列的别名的数组内容是:
[id, username, quantity, time, price]
查询到的结果集是:
[{quantity=170, price=60.00, id=2, time=2017-11-06 00:00:00.0, username=庄子}, {quantity=1020, price=99.87, id=4, time=2017-11-10 00:00:00.0, username=孟子}, {quantity=170, price=90.00, id=5, time=2017-11-03 00:00:00.0, username=伤寒杂病论}, {quantity=60, price=99.99, id=6, time=2017-11-16 00:00:00.0, username=孟子}, {quantity=190, price=110.00, id=7, time=2017-11-01 00:00:00.0, username=大学}, {quantity=200, price=40.00, id=8, time=2017-11-12 00:00:00.0, username=周易}, {quantity=195, price=40.00, id=9, time=2017-10-10 00:00:00.0, username=参同契}, {quantity=185, price=45.00, id=10, time=2017-09-09 00:00:00.0, username=金刚经}, {quantity=200, price=99.99, id=13, time=2017-01-01 00:00:00.0, username=地藏菩萨本愿经}, {quantity=130, price=99.87, id=14, time=2017-11-08 00:00:00.0, username=孟子}, {quantity=60, price=99.99, id=17, time=2017-11-16 00:00:00.0, username=孟子}, {quantity=60, price=99.99, id=18, time=2017-11-16 00:00:00.0, username=孟子}, {quantity=60, price=99.99, id=19, time=2017-11-16 00:00:00.0, username=孟子}]
注:这篇博文中有许多 小知识点,可以多看看。然后就是,操作数据库要谨慎小心,给最苦 这里的代码 看看就好。
学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。