作为初学者水平有限,如有错误还请批评指正!
注:
1、案例中的引用大多来自java.sql.*,不能引用为mysql下的jdbc包
2、实验使用的数据库为MySQL,连接数据库的mysql-connection-java-5-7-1.jar包可自行到MySQL官网下载,也可至我主页下载。
3、源码中用于存放登录信息的jdbc.properties文件放于src/目录下
4、在源码中我对增删改(preparedStatementUpdate(String sql, Object …args))、查(customerForQuery(String sql, Object …args))、连接数据库(getConnection())、关闭资源(closeResource())等函数进行了封装,使其模块化、通用化
5、源码中PreparedStatementTest()、PreparedStatementQueryTest()
6、代码末端Customers类用于封装查询结果数据
7、查询操作目前一次只能接受一条记录
8、阅读过程中如出现问题可参考尚硅谷宋红康老师的JDBC教程(尚硅谷JDBC核心技术视频教程(康师傅带你一站式搞定jdbc)_哔哩哔哩_bilibili)
一、该案例的运行环境与条件 1、文件目录结构 2、jdbc.properties文件该文件中等号两边不能出现空格
3、MySQL中Test数据库的customers表 二、源码解析user=root
password=abc123
url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useUnicode=true
driverClass=com.mysql.jdbc.Driver
package com.Etui3.preparedstatement.crud;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetmetaData;
import java.sql.SQLException;
import java.util.Properties;
import org.junit.Test;
public class rePractice {
// 获取数据库连接通用函数
public Connection getConnection() throws Exception{
// 1、读取登录信息
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
// 2、预编译
Properties prop = new Properties();
prop.load(is);
String user = prop.getProperty("user");
String password = prop.getProperty("password");
String url = prop.getProperty("url");
String driverClass = prop.getProperty("driverClass");
// 3、注册驱动
Class.forName(driverClass);
// 4、建立连接
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
// 关闭connection与PreparedStatement
public static void closeResource(Connection conn, PreparedStatement ps) {
try {
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(ps != null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 关闭connection、PreparedStatement、ResultSet
public static void closeResource(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(ps != null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(rs != null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 通用的增删改操作
public void preparedStatementUpdate(String sql, Object ...args) throws Exception {
// 获取连接
Connection conn = getConnection();
// 预编译SQL语句
PreparedStatement ps = conn.prepareStatement(sql);
// 填充占位符
for(int i=0; i
三、源码中设计到的类、接口(官方文档)
1、Connection接口
Connection.prepareStatement()
PreparedStatement.executeQuery()
PreparedStatement.getmetaData()
2、DriverManager 类
DriverManager.getConnection()
3、PreparedStatement类
PreparedStatement.getConnection()
4、Filed类
Field.setSuccessible()
Field.set()
5、ResultSet接口
ResultSet.next()
6、ResultSetMeatData接口
ResultSetmetaData.getColumnName()
ResultSetmetaData.getColumnCount()
7、class.getDelaredField()



