提交 c8b744ae 编写于 作者: Hello 码上秃's avatar Hello 码上秃

JDBC学习代码

上级
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GoogleJavaFormatSettings">
<option name="enabled" value="false" />
</component>
</project>
\ No newline at end of file
<component name="libraryTable">
<library name="c3p0-0.9.1.2">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/c3p0-0.9.1.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="commons-dbcp2-2.8.0">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/commons-dbcp2-2.8.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="commons-dbutils-1.3">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/commons-dbutils-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="commons-logging-1.2">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/commons-logging-1.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="commons-pool2-2.2">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/commons-pool2-2.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="druid-1.1.10">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/druid-1.1.10.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="mysql-connector-java-8.0.25">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/mysql-connector-java-8.0.25.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/JDBC.iml" filepath="$PROJECT_DIR$/JDBC.iml" />
<module fileurl="file://$PROJECT_DIR$/jdbc_1/jdbc_1.iml" filepath="$PROJECT_DIR$/jdbc_1/jdbc_1.iml" />
<module fileurl="file://$PROJECT_DIR$/jdbc_2/jdbc_2.iml" filepath="$PROJECT_DIR$/jdbc_2/jdbc_2.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
123.jpg

409.7 KB

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="mysql-connector-java-8.0.25" level="project" />
<orderEntry type="library" name="c3p0-0.9.1.2" level="project" />
<orderEntry type="library" name="commons-pool2-2.2" level="project" />
<orderEntry type="library" name="commons-dbcp2-2.8.0" level="project" />
<orderEntry type="library" name="commons-logging-1.2" level="project" />
<orderEntry type="library" name="druid-1.1.10" level="project" />
<orderEntry type="library" name="commons-dbutils-1.3" level="project" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="mysql-connector-java-8.0.25" level="project" />
</component>
</module>
\ No newline at end of file
package com.atguigu1.connection;
import org.junit.Test;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Properties;
/**
* @Author: ZZP
* @Description:
*
* @Date: Created in 20:07 2021/6/15
* @Version: 1.0
**/
public class ConnectionTest {
// 方式一
@Test
public void testConnection1() throws Exception {
// 获取 Driver 实现类对象
Driver driver = new com.mysql.cj.jdbc.Driver();
// jdbc:mysql:协议
// localhost:ip地址
// 3306:默认mysql的端口号
// test:test数据库
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
// 将用户名和密码封装在Properties中
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "719238");
Connection conn = driver.connect(url, info);
System.out.println(conn);
}
// 方式二:对方式一的迭代:在如下的程序中不出现第三方的api,使得程序具有更好的可移植性
@Test
public void testConnection2() throws Exception {
// 1.获取 Driver 实现类对象,使用反射实现
Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) clazz.newInstance();
// 2.提供要连接的数据库
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
// 3.提供连接需要的用户名和密码
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "719238");
// 4.获取连接
Connection conn = driver.connect(url, info);
System.out.println(conn);
}
// 方式三:使用 DriverManager 替换 Driver
@Test
public void testConnection3() throws Exception {
// 1.获取 Driver 实现类对象,使用反射实现
Class clazz = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) clazz.newInstance();
// 2.提供另外三个连接的基本信息
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
String user = "root";
String password = "719238";
// 注册驱动
DriverManager.registerDriver(driver);
// 3.获取链接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
// 方式四:相较于方式三可以省略 注册驱动 这一步操作
@Test
public void testConnection4() throws Exception {
// 1.提供另外三个连接的基本信息
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
String user = "root";
String password = "719238";
// 2.加载 Driver 实现类对象,使用反射实现
Class.forName("com.mysql.jdbc.Driver");
// 3.获取链接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
// 方式五:将数据库连接需要的四个基本信息声明在配置文件中,通过读取配置文件,获取连接
/*
此种方式的好处:
1. 实现了数据与代码的分离。
2. 如果需要修改配置文件信息,可避免程序重新打包
*/
@Test
public void testConnection5() throws Exception {
// 读取配置文件中的四个基本信息
InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
// 2.加载驱动
Class.forName(driverClass);
// 3.获取链接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
}
package com.atguigu2.statement.crud;
import com.atguigu3.util.JDBCUtils;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Scanner;
/**
* @Author: ZZP
* @Description:
* 演示 PreparedStatement 替换 Statement ,解决 SQL 注入问题
*
* 除了解决 Statement 的拼串、sql问题之外,PreparedStatement 还有哪些好处呢?
* 1.PreparedStatement 操作 Blob 的数据,而 Statement 做不到
* 2.PreparedStatement 可以实现更高效的批量操作
*
* @Date: Created in 16:18 2021/6/17
* @Version: 1.0
**/
public class PreparedStatementTest {
public static void main(String[] args) {
testLogin();
}
public static void testLogin() {
// 使用Statement的弊端:需要拼写sql语句,并且存在SQL注入的问题
Scanner scan = new Scanner(System.in);
System.out.print("用户名:");
String userName = scan.nextLine();
System.out.print("密 码:");
String password = scan.nextLine();
//SELECT user,password FROM user_table WHERE USER = '1' or ' AND PASSWORD = '=1 or '1' = '1';
String sql = "SELECT user,password FROM user_table WHERE user = ? and password = ?";
User user = getInstance(User.class, sql, userName, password);
if (user != null) {
System.out.println("登陆成功!");
} else {
System.out.println("用户名或密码错误!");
}
}
/**
* @Author: ZZP
* @Date:2021/6/17
* @Description: 针对于不同表的通用的查询操作,返回表中的一条记录
*/
public static <T> T getInstance(Class<T> clazz, String sql, Object... args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
conn = JDBCUtils.getConnection();
// 2.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 3.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 4.执行并返回结果集
resultSet = ps.executeQuery();
// 获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsmd = resultSet.getMetaData();
// 通过 ResultSetMetaData 获取结果集中的列数
int columnCount = rsmd.getColumnCount();
// 5.处理结果集
if (resultSet.next()) {
T t = clazz.newInstance();
// 处理结果集一行数据中的每一个列
for (int i = 0; i < columnCount; i++) {
// 获取列值
Object columnValue = resultSet.getObject(i + 1);
// 获取每个列的列名 getColumnName()
// 获取每个列的别名 getColumnLabel()
String columnLabel = rsmd.getColumnLabel(i + 1);
// 给 columnName 对象指定的某个属性,赋值为 columnValue : 通过反射
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t, columnValue);
}
return t;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBCUtils.closeResource(conn, ps, resultSet);
}
return null;
}
}
package com.atguigu2.statement.crud;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.Scanner;
public class StatementTest {
public static void main(String[] args) {
testLogin();
}
public static void testLogin() {
// 使用Statement的弊端:需要拼写sql语句,并且存在SQL注入的问题
Scanner scan = new Scanner(System.in);
System.out.print("用户名:");
String userName = scan.nextLine();
System.out.print("密 码:");
String password = scan.nextLine();
//SELECT user,password FROM user_table WHERE USER = '1' or ' AND PASSWORD = '=1 or '1' = '1';
String sql = "SELECT user,password FROM user_table WHERE user = '" + userName + "' AND password = '" + password + "'";
User user = get(sql, User.class);
if (user != null) {
System.out.println("登陆成功!");
} else {
System.out.println("用户名或密码错误!");
}
}
// 使用Statement实现对数据表的查询操作
public static <T> T get(String sql, Class<T> clazz) {
T t = null;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
// 1.加载配置文件
InputStream is = StatementTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
// 2.读取配置信息
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
// 3.加载驱动
Class.forName(driverClass);
// 4.获取连接
conn = DriverManager.getConnection(url, user, password);
st = conn.createStatement();
rs = st.executeQuery(sql);
// 获取结果集的元数据
ResultSetMetaData rsmd = rs.getMetaData();
// 获取结果集的列数
int columnCount = rsmd.getColumnCount();
if (rs.next()) {
t = clazz.newInstance();
for (int i = 0; i < columnCount; i++) {
// //1. 获取列的名称
// String columnName = rsmd.getColumnName(i+1);
// 1. 获取列的别名
String columnName = rsmd.getColumnLabel(i + 1);
// 2. 根据列名获取对应数据表中的数据
Object columnVal = rs.getObject(columnName);
// 3. 将数据表中得到的数据,封装进对象
Field field = clazz.getDeclaredField(columnName);
field.setAccessible(true);
field.set(t, columnVal);
}
return t;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return null;
}
}
package com.atguigu2.statement.crud;
public class User {
private String user;
private String password;
public User() {
}
public User(String user, String password) {
super();
this.user = user;
this.password = password;
}
@Override
public String toString() {
return "User [user=" + user + ", password=" + password + "]";
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.atguigu3.bean;
import java.sql.Date;
/**
* @Author: ZZP
* @Description:
* ORM编程思想 (object relation mapping)
* 一个数据表对应一个 java 类
* 表中的一条记录对应 java 类的一个对象
* 表中的一个字段对应 java 类的一个属性
*
* @Date: Created in 12:38 2021/6/16
* @Version: 1.0
**/
public class Customer {
private int id;
private String name;
private String email;
private Date birth;
public Customer() {
}
public Customer(int id, String name, String email, Date birth) {
this.id = id;
this.name = name;
this.email = email;
this.birth = birth;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", birth=" + birth +
'}';
}
}
package com.atguigu3.bean;
import java.sql.Date;
/**
* @Author: ZZP
* @Description:
*
* @Date: Created in 15:43 2021/6/16
* @Version: 1.0
**/
public class Order {
private int orderId;
private String orderName;
private Date orderDate;
public Order() {
}
public Order(int orderId, String orderName, Date orderDate) {
this.orderId = orderId;
this.orderName = orderName;
this.orderDate = orderDate;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
@Override
public String toString() {
return "Order{" +
"orderId=" + orderId +
", orderName='" + orderName + '\'' +
", orderDate=" + orderDate +
'}';
}
}
package com.atguigu3.preparedstatement.curd;
import com.atguigu3.bean.Customer;
import com.atguigu3.util.JDBCUtils;
import java.lang.reflect.Field;
import java.sql.*;
/**
* @Author: ZZP
* @Description:
* 针对于 Customers 表的查询操作
*
* @Date: Created in 12:25 2021/6/16
* @Version: 1.0
**/
public class CustomerForQuery {
public static void main(String[] args) {
testQuery1();
System.out.println("----------------------------------------------------------");
String sql = "select id, name, email, birth from customers where id = ?";
Customer customer = queryForCustomer(sql, 1);
System.out.println(customer);
}
/**
* @Author: ZZP
* @Date:2021/6/16
* @Description: 针对于 customers 表的通用查询操作
*/
public static Customer queryForCustomer(String sql, Object... args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
// 1.获取数据库的连接
conn = JDBCUtils.getConnection();
// 2.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 3.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 4.执行并返回结果集
resultSet = ps.executeQuery();
// 获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsmd = resultSet.getMetaData();
// 通过 ResultSetMetaData 获取结果集中的列数
int columnCount = rsmd.getColumnCount();
// 5.处理结果集
if (resultSet.next()) {
Customer customer = new Customer();
// 处理结果集一行数据中的每一个列
for (int i = 0; i < columnCount; i++) {
// 获取列值
Object columnValue = resultSet.getObject(i + 1);
// 获取每个列的列名
String columnName = rsmd.getColumnName(i + 1);
// 给 columnName 对象指定的某个属性,赋值为 columnValue : 通过反射
Field field = Customer.class.getDeclaredField(columnName);
field.setAccessible(true);
field.set(customer, columnValue);
}
return customer;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBCUtils.closeResource(conn, ps, resultSet);
}
return null;
}
/**
* @Author: ZZP
* @Date:2021/6/16
* @Description: 针对于 customers 表的查询操作
*/
// 查询操作
public static void testQuery1() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
// 1.获取数据库的连接
conn = JDBCUtils.getConnection();
// 2.预编译 sql 语句,返回 PreparedStatement 的实例
String sql = "select id, name, email, birth from customers where id = ?";
ps = conn.prepareStatement(sql);
// 3.填充占位符
ps.setInt(1, 13);
// 4.执行并返回结果集
resultSet = ps.executeQuery();
// 5.处理结果集
if (resultSet.next()) { // 判断结果集的下一条是否有数据,如果有数据返回true,并且指针下移,如果没有数据返回false,并且指针不会下移
// 获取当前这条数据的各个字段值
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
String email = resultSet.getString(3);
Date birth = resultSet.getDate(4);
// 方式一
//Object[] data = new Object[]{id, name, email, birth};
// 方式二:将数据封装为一个对象
Customer customer = new Customer(id, name, email, birth);
System.out.println(customer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 6.关闭资源
JDBCUtils.closeResource(conn, ps, resultSet);
}
}
}
package com.atguigu3.preparedstatement.curd;
import com.atguigu3.bean.Order;
import com.atguigu3.util.JDBCUtils;
import java.lang.reflect.Field;
import java.sql.*;
/**
* @Author: ZZP
* @Description:
* 针对于 Order 表的通用的查询操作
*
* @Date: Created in 15:40 2021/6/16
* @Version: 1.0
**/
public class OrderForQuery {
public static void main(String[] args) {
testQuery();
System.out.println("----------------------------------------------------------");
String sql = "select order_id orderId, order_name orderName, order_date orderDate from `order` where order_id = ?";
Order order = orderForQuery(sql, 1);
System.out.println(order);
}
/**
* @Author: ZZP
* @Date:2021/6/17
* @Description: 针对于 order 表的查询操作
*/
public static Order orderForQuery(String sql, Object... args) {
/**
* 针对于表的字段名与类的属性名不相同的情况:
* 1.必须声明sql时,使用类的属性名来命名字段的别名
* 2.使用 ResultSetMetaData 时,需要使用 getColumnLabel() 来替换 getColumnName() ,获取列的别名
*
* 说明:如果 sql 中没有给字段起别名,getColumnLabel() 获取的就是列名
*/
Connection conn = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
conn = JDBCUtils.getConnection();
// 2.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 3.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 4.执行并返回结果集
resultSet = ps.executeQuery();
// 获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsmd = resultSet.getMetaData();
// 通过 ResultSetMetaData 获取结果集中的列数
int columnCount = rsmd.getColumnCount();
// 5.处理结果集
if (resultSet.next()) {
Order order = new Order();
// 处理结果集一行数据中的每一个列
for (int i = 0; i < columnCount; i++) {
// 获取列值
Object columnValue = resultSet.getObject(i + 1);
// 获取每个列的列名 getColumnName()
// 获取每个列的别名 getColumnLabel()
String columnLabel = rsmd.getColumnLabel(i + 1);
// 给 columnName 对象指定的某个属性,赋值为 columnValue : 通过反射
Field field = Order.class.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(order, columnValue);
}
return order;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBCUtils.closeResource(conn, ps, resultSet);
}
return null;
}
public static void testQuery() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
String sql = "select order_id, order_name, order_date from `order` where order_id = ?";
ps = conn.prepareStatement(sql);
ps.setObject(1, 1);
rs = ps.executeQuery();
if (rs.next()) {
int id = (int) rs.getObject(1);
String name = (String) rs.getObject(2);
Date date = (Date) rs.getObject(3);
Order order = new Order(id, name, date);
System.out.println(order);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps, rs);
}
}
}
package com.atguigu3.preparedstatement.curd;
import com.atguigu3.bean.Customer;
import com.atguigu3.bean.Order;
import com.atguigu3.util.JDBCUtils;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: ZZP
* @Description:
* 使用 PreparedStatement 实现针对于不同表的通用的查询操作
*
* @Date: Created in 15:27 2021/6/17
* @Version: 1.0
**/
public class PreparedStatementQueryTest {
public static void main(String[] args) {
String sql = "select order_id orderId, order_name orderName, order_date orderDate from `order` where order_id = ?";
Order order = getInstance(Order.class, sql, 1);
System.out.println(order);
System.out.println("--------------------------------------------------------------------------");
String sql1 = "select id, name, email, birth from customers where id = ?";
Customer customer = getInstance(Customer.class, sql1, 13);
System.out.println(customer);
System.out.println("--------------------------------------------------------------------------");
String sql2 = "select id, name, email, birth from customers where id < ?";
List<Customer> customerList = getForList(Customer.class, sql2, 13);
customerList.forEach(System.out::println);
}
/**
* @Author: ZZP
* @Date:2021/6/17
* @Description: 针对于不同表的通用的查询操作,返回表中的多条记录
*/
public static <T> List<T> getForList(Class<T> clazz, String sql, Object... args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
conn = JDBCUtils.getConnection();
// 2.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 3.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 4.执行并返回结果集
resultSet = ps.executeQuery();
// 获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsmd = resultSet.getMetaData();
// 通过 ResultSetMetaData 获取结果集中的列数
int columnCount = rsmd.getColumnCount();
// 创建集合对象
ArrayList<T> list = new ArrayList<T>();
// 5.处理结果集
while (resultSet.next()) {
T t = clazz.newInstance();
// 处理结果集一行数据中的每一个列:给 t 对象指定的属性赋值
for (int i = 0; i < columnCount; i++) {
// 获取列值
Object columnValue = resultSet.getObject(i + 1);
// 获取每个列的列名 getColumnName()
// 获取每个列的别名 getColumnLabel()
String columnLabel = rsmd.getColumnLabel(i + 1);
// 给 columnName 对象指定的某个属性,赋值为 columnValue : 通过反射
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t, columnValue);
}
list.add(t);
}
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBCUtils.closeResource(conn, ps, resultSet);
}
return null;
}
/**
* @Author: ZZP
* @Date:2021/6/17
* @Description: 针对于不同表的通用的查询操作,返回表中的一条记录
*/
public static <T> T getInstance(Class<T> clazz, String sql, Object... args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
conn = JDBCUtils.getConnection();
// 2.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 3.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 4.执行并返回结果集
resultSet = ps.executeQuery();
// 获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsmd = resultSet.getMetaData();
// 通过 ResultSetMetaData 获取结果集中的列数
int columnCount = rsmd.getColumnCount();
// 5.处理结果集
if (resultSet.next()) {
T t = clazz.newInstance();
// 处理结果集一行数据中的每一个列
for (int i = 0; i < columnCount; i++) {
// 获取列值
Object columnValue = resultSet.getObject(i + 1);
// 获取每个列的列名 getColumnName()
// 获取每个列的别名 getColumnLabel()
String columnLabel = rsmd.getColumnLabel(i + 1);
// 给 columnName 对象指定的某个属性,赋值为 columnValue : 通过反射
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t, columnValue);
}
return t;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBCUtils.closeResource(conn, ps, resultSet);
}
return null;
}
}
package com.atguigu3.preparedstatement.curd;
import com.atguigu1.connection.ConnectionTest;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Properties;
/**
* @Author: ZZP
* @Description:
* 使用 PreparedStatement 来替换 Statement,实现对数据表的增删改查操作
*
* 增删改
* 查
*
* @Date: Created in 9:25 2021/6/16
* @Version: 1.0
**/
public class PreparedStatementUpdateTest {
// 向 customer 表中添加一条记录
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
try {
// 1.读取配置文件中的四个基本信息
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
// 2.加载驱动
Class.forName(driverClass);
// 3.获取链接
conn = DriverManager.getConnection(url, user, password);
// 4.预编译 sql 语句,返回 PreparedStatement 的实例
String sql = "insert into customers(name ,email,birth) value (?,?,?)";
ps = conn.prepareStatement(sql);
// 5.填充占位符
ps.setString(1, "哪吒");
ps.setString(2, "nezha@qq.com");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = sdf.parse("1000-01-01");
ps.setDate(3, new Date(date.getTime()));
// 6.执行操作
ps.execute();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} finally {
// 7.关闭资源
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
\ No newline at end of file
package com.atguigu3.preparedstatement.curd;
import com.atguigu3.util.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
/**
* @Author: ZZP
* @Description:
* 使用 PreparedStatement 来替换 Statement,实现对数据表的增删改查操作
*
* 增删改
* 查
*
* @Date: Created in 10:34 2021/6/16
* @Version: 1.0
**/
public class PreparedStatementUpdateTest1 {
// 修改 customer 表中的一条记录
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
try {
// 1.获取数据库的连接
conn = JDBCUtils.getConnection();
// 2.预编译 sql 语句,返回 PreparedStatement 的实例
String sql = "update customers set name = ? where id = ?";
ps = conn.prepareStatement(sql);
// 3.填充占位符
ps.setString(1, "莫扎特");
ps.setInt(2, 18);
// 4.执行操作
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5.关闭资源
JDBCUtils.closeResource(conn, ps);
}
}
}
package com.atguigu3.preparedstatement.curd;
import com.atguigu3.util.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
/**
* @Author: ZZP
* @Description:
* 使用 PreparedStatement 来替换 Statement,实现对数据表的增删改查操作
*
* 增删改
* 查
*
* @Date: Created in 10:34 2021/6/16
* @Version: 1.0
**/
public class PreparedStatementUpdateTest2 {
//删除 customer 表中的一条记录
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
try {
// 1.获取数据库的连接
conn = JDBCUtils.getConnection();
// 2.预编译 sql 语句,返回 PreparedStatement 的实例
String sql = "delete from customers where id = ?";
ps = conn.prepareStatement(sql);
// 3.填充占位符
ps.setInt(1, 19);
// 4.执行操作
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5.关闭资源
JDBCUtils.closeResource(conn, ps);
}
}
}
package com.atguigu3.preparedstatement.curd;
import com.atguigu3.util.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
/**
* @Author: ZZP
* @Description:
* 使用 PreparedStatement 来替换 Statement,实现对数据表的增删改查操作
*
* 增删改
* 查
*
* @Date: Created in 10:34 2021/6/16
* @Version: 1.0
**/
public class PreparedStatementUpdateTest3 {
public static void main(String[] args) {
String sql = "delete from customers where id = ?";
update(sql, 3);
}
// 通用的增删改操作
public static void update(String sql, Object... args) { // sql中的占位符的个数与可变形参的长度相同
Connection conn = null;
PreparedStatement ps = null;
try {
// 1.获取数据库的连接
conn = JDBCUtils.getConnection();
// 2.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 3.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 4.执行操作
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5.关闭资源
JDBCUtils.closeResource(conn, ps);
}
}
}
package com.atguigu3.util;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* @Author: ZZP
* @Description:
* 操作数据库的工具类
*
* @Date: Created in 10:09 2021/6/16
* @Version: 1.0
**/
public class JDBCUtils {
/**
* @Author: ZZP
* @Date:2021/6/16
* @Description: 获取数据库的连接
**/
public static Connection getConnection() throws Exception {
// 读取配置文件中的四个基本信息
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
// 2.加载驱动
Class.forName(driverClass);
// 3.获取链接
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
/**
* @Author: ZZP
* @Date:2021/6/16
* @Description: 关闭连接和 Statement 的操作
*/
public static void closeResource(Connection conn, Statement ps){
// 7.关闭资源
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
/**
* @Author: ZZP
* @Date:2021/6/16
* @Description: 关闭连接和 Statement 和 ResultSet 的操作
*/
public static void closeResource(Connection conn, Statement ps, ResultSet rs){
// 7.关闭资源
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
package com.atguigu4.exercise;
/**
* @Author: ZZP
* @Description:
*
* @Date: Created in 20:53 2021/6/17
* @Version: 1.0
**/
public class Student {
/*
FlowID:
Type:
IDCard:
ExamCard:
StudentName:
Location:
Grade:
*/
private int FlowID;
private int Type;
private String IDCard;
private String ExamCard;
private String StudentName;
private String Location;
private int Grade;
public Student() {
}
public Student(int flowID, int type, String IDCard, String examCard, String studentName, String location, int grade) {
FlowID = flowID;
Type = type;
this.IDCard = IDCard;
ExamCard = examCard;
StudentName = studentName;
Location = location;
Grade = grade;
}
public int getFlowID() {
return FlowID;
}
public void setFlowID(int flowID) {
FlowID = flowID;
}
public int getType() {
return Type;
}
public void setType(int type) {
Type = type;
}
public String getIDCard() {
return IDCard;
}
public void setIDCard(String IDCard) {
this.IDCard = IDCard;
}
public String getExamCard() {
return ExamCard;
}
public void setExamCard(String examCard) {
ExamCard = examCard;
}
public String getStudentName() {
return StudentName;
}
public void setStudentName(String studentName) {
StudentName = studentName;
}
public String getLocation() {
return Location;
}
public void setLocation(String location) {
Location = location;
}
public int getGrade() {
return Grade;
}
public void setGrade(int grade) {
Grade = grade;
}
@Override
public String toString() {
System.out.println("=============查询结果=============");
return "流水号:" + FlowID +
"\n四级 / 六级:" + Type +
"\n身份证号:" + IDCard +
"\n准考证号:" + ExamCard +
"\n学生姓名:" + StudentName +
"\n区域:" + Location +
"\n成绩:" + Grade;
}
}
package com.atguigu4.exercise;
import com.atguigu3.util.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;
/**
* @Author: ZZP
* @Description:
* 练习题1:从控制台向数据库的表customers中插入一条数据,表结构如下:
*
* @Date: Created in 20:14 2021/6/17
* @Version: 1.0
**/
public class exercise1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = scanner.next();
System.out.println("请输入email:");
String email = scanner.next();
System.out.println("请输入生日日期:");
String birth = scanner.next();
String sql = "insert into customers(name, email, birth)values(?,?,?)";
int update = update(sql, name, email, birth);// 1992-09-08
if (update > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
}
// 通用的增删改操作
public static int update(String sql, Object... args) { // sql中的占位符的个数与可变形参的长度相同
Connection conn = null;
PreparedStatement ps = null;
try {
// 1.获取数据库的连接
conn = JDBCUtils.getConnection();
// 2.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 3.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 4.执行操作
/**
* ps.execute():
* 如果执行的是查询操作,有返回值,则此方法返回 true
* 如果执行的是增、删、改、查操作,没有返回值,则此方法返回 false
*/
// 方式一:
//ps.execute();
// 方式二:
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5.关闭资源
JDBCUtils.closeResource(conn, ps);
}
return 0;
}
}
package com.atguigu4.exercise;
import com.atguigu3.util.JDBCUtils;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Scanner;
/**
* @Author: ZZP
* @Description:
*
* @Date: Created in 20:35 2021/6/17
* @Version: 1.0
**/
public class exercise2 {
public static void main(String[] args) {
//testInsert();
//queryWithIDCardOrExamCard();
//testDeleteByExamCard();
testDeleteByExamCard1();
}
// 完成学生信息的删除功能
public static void testDeleteByExamCard() {
while (true) {
System.out.println("请输入准考证号:");
Scanner scanner = new Scanner(System.in);
String ExamCard = scanner.next();
// 查询指定准考证号的学生
String sql = "select FlowID, Type,IDCard, ExamCard,StudentName, Location,Grade from examstudent where ExamCard = ?";
Student student = getInstance(Student.class, sql, ExamCard);
if (student == null) {
System.out.println("查无此人!请重新输入:");
} else {
String sql1 = "delete from examstudent where ExamCard = ?";
int deleteCount = update(sql1, ExamCard);
if (deleteCount > 0) {
System.out.println("删除成功!");
break;
} else {
System.out.println("删除失败!");
}
}
}
}
// 完成学生信息的删除功能
public static void testDeleteByExamCard1() {
while (true) {
System.out.println("请输入准考证号:");
Scanner scanner = new Scanner(System.in);
String ExamCard = scanner.next();
String sql = "delete from examstudent where ExamCard = ?";
int deleteCount = update(sql, ExamCard);
if (deleteCount > 0) {
System.out.println("删除成功!");
break;
} else {
System.out.println("查无此人!请重新输入:");
}
}
}
// 输入身份证号或准考证号可以查询到学生的基本信息。
public static void queryWithIDCardOrExamCard() {
System.out.println("请输入你要查询的类型:");
System.out.println("a.准考证号");
System.out.println("b.身份证号");
Scanner scanner = new Scanner(System.in);
String selection = scanner.next();
if ("a".equalsIgnoreCase(selection)) {
System.out.println("请输入准考证号:");
String ExamCard = scanner.next();
String sql = "select FlowID, Type,IDCard, ExamCard,StudentName, Location,Grade from examstudent where ExamCard = ?";
Student student = getInstance(Student.class, sql, ExamCard);
if (student != null) {
System.out.println(student);
} else {
System.out.println("输入的准考证号有误!");
}
} else if ("b".equalsIgnoreCase(selection)) {
System.out.println("请输入身份证号:");
System.out.println("请输入身份证号:");
String IDCard = scanner.next();
String sql = "select FlowID, Type,IDCard, ExamCard,StudentName, Location,Grade from examstudent where ExamCard = ?";
Student student = getInstance(Student.class, sql, IDCard);
if (student != null) {
System.out.println(student);
} else {
System.out.println("输入的身份证号有误!");
}
} else {
System.out.println("您的输入有误!请重新进入程序。");
}
}
// 插入一个新的student 信息
/*
Type:
IDCard:
ExamCard:
StudentName:
Location:
Grade:
信息录入成功!
*/
public static void testInsert() {
Scanner scanner = new Scanner(System.in);
System.out.print("四级 / 六级:");
int Type = scanner.nextInt();
System.out.print("身份证号:");
String IDCard = scanner.next();
System.out.print("准考证号:");
String ExamCard = scanner.next();
System.out.print("学生姓名:");
String StudentName = scanner.next();
System.out.print("所在城市:");
String Location = scanner.next();
System.out.print("学生成绩:");
int Grade = scanner.nextInt();
String sql = "insert into examstudent(Type, IDCard, ExamCard, StudentName, Location,Grade)values(?,?,?,?,?,?)";
int update = update(sql, Type, IDCard, ExamCard, StudentName, Location, Grade);
if (update > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
}
/**
* @Author: ZZP
* @Date:2021/6/17
* @Description: 通用的增删改操作
*/
public static int update(String sql, Object... args) { // sql中的占位符的个数与可变形参的长度相同
Connection conn = null;
PreparedStatement ps = null;
try {
// 1.获取数据库的连接
conn = JDBCUtils.getConnection();
// 2.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 3.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 4.执行操作
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5.关闭资源
JDBCUtils.closeResource(conn, ps);
}
return 0;
}
/**
* @Author: ZZP
* @Date:2021/6/17
* @Description: 针对于不同表的通用的查询操作,返回表中的一条记录
*/
public static <T> T getInstance(Class<T> clazz, String sql, Object... args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
conn = JDBCUtils.getConnection();
// 2.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 3.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 4.执行并返回结果集
resultSet = ps.executeQuery();
// 获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsmd = resultSet.getMetaData();
// 通过 ResultSetMetaData 获取结果集中的列数
int columnCount = rsmd.getColumnCount();
// 5.处理结果集
if (resultSet.next()) {
T t = clazz.newInstance();
// 处理结果集一行数据中的每一个列
for (int i = 0; i < columnCount; i++) {
// 获取列值
Object columnValue = resultSet.getObject(i + 1);
// 获取每个列的列名 getColumnName()
// 获取每个列的别名 getColumnLabel()
String columnLabel = rsmd.getColumnLabel(i + 1);
// 给 columnName 对象指定的某个属性,赋值为 columnValue : 通过反射
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t, columnValue);
}
return t;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBCUtils.closeResource(conn, ps, resultSet);
}
return null;
}
}
package com.atguigu5.blob;
import com.atguigu3.bean.Customer;
import com.atguigu3.util.JDBCUtils;
import java.io.*;
import java.sql.*;
/**
* @Author: ZZP
* @Description:
* 测试使用 PreparedStatement 操作 Blob 类型的数据
*
* @Date: Created in 21:43 2021/6/17
* @Version: 1.0
**/
public class BlobTest {
public static void main(String[] args) {
//int insert = testInsert();
//if (insert > 0) {
// System.out.println("插入成功!");
//} else {
// System.out.println("插入失败!");
//}
testQuery();
}
// 向数据表 customers 中插入 Blob 类型的字段
public static int testInsert() {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtils.getConnection();
String sql = "insert into customers(name, email, birth, photo)values(?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setObject(1, "张浩宇");
ps.setObject(2, "zhang@qq.com");
ps.setObject(3, "1992-01-01");
FileInputStream is = new FileInputStream(new File("123.jpg"));
ps.setObject(4, is);
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps);
}
return 0;
}
// 查询数据表 customers 中的 Blob 类型的字段
public static void testQuery() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
InputStream is = null;
FileOutputStream fos = null;
try {
conn = JDBCUtils.getConnection();
String sql = "select id, name, email, birth, photo from customers where id = ?";
ps = conn.prepareStatement(sql);
ps.setObject(1, 22);
rs = ps.executeQuery();
is = null;
fos = null;
if (rs.next()) {
// 方式一:
//int id = rs.getInt(1);
//String name = rs.getString(2);
//String email = rs.getString(3);
//Date birth = rs.getDate(4);
//Object photo = rs.getObject(5);
// 方式一:
int id = rs.getInt("id");
String name = rs.getString("name");
String email = rs.getString("email");
Date birth = rs.getDate("birth");
Customer cust = new Customer(id, name, email, birth);
System.out.println(cust);
// 将 Blob 类型的字段下载下来,以文件的方式保存到本地
Blob photo = rs.getBlob("photo");
is = photo.getBinaryStream();
fos = new FileOutputStream("zhuyin.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps, rs);
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.atguigu5.blob;
import com.atguigu3.util.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
/**
* @Author: ZZP
* @Description:
* 使用 PreparedStatement 实现批量数据的操作
*
* update / delete 本身就具有批量操作的效果
* 此时的批量操作,主要指的是批量插入。使用 PreparedStatement 如何实现更高效的批量插入
*
* 题目:向 goods 表中插入 20000 条数据
* 方式一:使用 Statement
*
*
* @Date: Created in 15:20 2021/6/18
* @Version: 1.0
**/
public class InsertTest {
public static void main(String[] args) {
//testInsert1();
//testInsert2();
testInsert3();
}
/**
* @Author: ZZP
* @Date:2021/6/18
* @Description: 批量插入的方式二:使用 PreparedStatement
*/
public static void testInsert1() {
Connection conn = null;
PreparedStatement ps = null;
try {
long start = System.currentTimeMillis();
conn = JDBCUtils.getConnection();
String sql = "insert into goods(name)value(?)";
ps = conn.prepareStatement(sql);
for (int i = 1; i <= 20000; i++) {
ps.setObject(1,"name_" + i);
ps.execute();
}
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn,ps);
}
}
/**
* @Author: ZZP
* @Date:2021/6/18
* @Description:
* 批量插入的方式三:
* 1.addBatch()、executeBatch()、clearBatch()
* 2.mysql服务器默认是关闭批处理的,我们需要通过一个参数,让mysql开启批处理的支持。 ?rewriteBatchedStatements=true 写在配置文件的url后面
* 3.使用更新的mysql 驱动:mysql-connector-java-5.1.37-bin.jar
*/
public static void testInsert2() {
Connection conn = null;
PreparedStatement ps = null;
try {
long start = System.currentTimeMillis();
conn = JDBCUtils.getConnection();
String sql = "insert into goods(name)value(?)";
ps = conn.prepareStatement(sql);
for (int i = 1; i <= 20000; i++) {
ps.setObject(1,"name_" + i);
// 1."攒" sql
ps.addBatch();
if (i % 500 == 0) {
// 2.执行
ps.executeBatch();
// 3.清空batch
ps.clearBatch();
}
}
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn,ps);
}
}
/**
* @Author: ZZP
* @Date:2021/6/18
* @Description: 批量插入的方式四:设置不允许自动提交数据
*/
public static void testInsert3() {
Connection conn = null;
PreparedStatement ps = null;
try {
long start = System.currentTimeMillis();
conn = JDBCUtils.getConnection();
// 设置不允许自动提交数据
conn.setAutoCommit(false);
String sql = "insert into goods(name)value(?)";
ps = conn.prepareStatement(sql);
for (int i = 1; i <= 20000; i++) {
ps.setObject(1,"name_" + i);
// 1."攒" sql
ps.addBatch();
if (i % 500 == 0) {
// 2.执行 batch
ps.executeBatch();
// 3.清空 batch
ps.clearBatch();
}
}
// 提交数据
conn.commit();
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn,ps);
}
}
}
#获取基本信息
user=root
password=719238
url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=UTC&rewriteBatchedStatements=true
driverClass=com.mysql.cj.jdbc.Driver
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="mysql-connector-java-8.0.25" level="project" />
<orderEntry type="library" name="c3p0-0.9.1.2" level="project" />
<orderEntry type="library" name="commons-dbcp2-2.8.0" level="project" />
<orderEntry type="library" name="commons-pool2-2.2" level="project" />
<orderEntry type="library" name="commons-logging-1.2" level="project" />
<orderEntry type="library" name="druid-1.1.10" level="project" />
<orderEntry type="library" name="commons-dbutils-1.3" level="project" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="helloC3P0">
<!-- 通过获取连接的四个基本信息 -->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true</property>
<property name="user">root</property>
<property name="password">719238</property>
<!-- 进行数据库连接池管理的基本信息 -->
<!-- 当数据库连接池中的连接数不够时,C3P0 一次性向数据库服务器申请的连接数 -->
<property name="acquireIncrement">5</property>
<!-- C3P0 数据库连接池中初始化的连接数 -->
<property name="initialPoolSize">10</property>
<!-- C3P0 数据库连接池中维护的最少的连接数 -->
<property name="minPoolSize">10</property>
<!-- C3P0 数据库连接池中维护的最多的连接数 -->
<property name="maxPoolSize">100</property>
<!-- C3P0 数据库连接池中维护的 Statement 的最大个数 -->
<property name="maxStatements">50</property>
<!-- 每个连接中可以使用最多的 Statement 的个数 -->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
package com.atguigu1.transaction;
import com.atguigu1.util.JDBCUtils;
import org.junit.Test;
import java.sql.Connection;
/**
* @Author: ZZP
* @Description:
*
* @Date: Created in 20:19 2021/6/18
* @Version: 1.0
**/
public class ConnectionTest {
@Test
public void testGetConnection() throws Exception {
Connection coon = JDBCUtils.getConnection();
System.out.println(coon);
}
}
package com.atguigu1.transaction;
import com.atguigu1.util.JDBCUtils;
import org.junit.Test;
import java.lang.reflect.Field;
import java.sql.*;
/**
* @Author: ZZP
* @Date: Created in 20:33 2021/6/18
* @Version: 1.0
* @Description:
* 1.什么叫数据库事务?
* 事物:一组逻辑操作单元,使数据从一种状态变到另外一种状态
* > 一组逻辑单元:一个或多个 DML 操作
*
* 2.事务处理(事务操作)原则:
* 保证所有事务都作为一个工作单元来执行,即使出现了故障,都不能改变这种执行方式。
* 当在一个事务中执行多个操作时,要么所有的事务都被提交(commit),那么这些修改就永久地保存下来;
* 要么数据库管理系统将放弃所作的所有修改,整个事务回滚(rollback)到最初状态。
*
* 3.数据一旦提交,就不可回滚
*
* 4.哪些操作会导致数据的自动提交?
* > DDL 操作一旦执行,都会提交
* > DML 默认情况下,一旦执行,就会自动提交
* > 我们可以通过 set autocommit = false; 的方式取消 DML 操作的自动提交
* > 默认在关闭连接时,会自动的提交数据
**/
public class TransactionTest {
public static void main(String[] args) {
//testUpdate();
//testUpdateWithTx();
}
/**
* @Author: ZZP
* @Date:2021/6/18
* @Version: 1.0
* @Description:
* 针对于数据表 user_table来说:
* AA 用户给 BB 用户转账 100
*
* update user_table set balance = balance - 100 where user = 'AA'
* update user_table set balance = balance + 100 where user = 'BB'
*/
public static void testUpdate() {
String sql1 = "update user_table set balance = balance - 100 where user = ?";
update(sql1, "AA");
// 模拟网络异常
System.out.println(10 / 0);
String sql2 = "update user_table set balance = balance + 100 where user = ?";
update(sql2, "BB");
System.out.println("转账成功!");
}
/**
* @Author: ZZP
* @Date:2021/6/18
* @Version: 1.0 - 未考虑数据库事物情况下的转账操作
* @Description: 通用的增删改操作
*/
public static int update(String sql, Object... args) { // sql中的占位符的个数与可变形参的长度相同
Connection conn = null;
PreparedStatement ps = null;
try {
// 1.获取数据库的连接
conn = JDBCUtils.getConnection();
// 2.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 3.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 4.执行操作
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5.关闭资源
JDBCUtils.closeResource(conn, ps);
}
return 0;
}
/**
* @Author: ZZP
* @Date:2021/6/18
* @Version: 2.0
* @Description:
* 针对于数据表 user_table来说:
* AA 用户给 BB 用户转账 100
*
* update user_table set balance = balance - 100 where user = 'AA'
* update user_table set balance = balance + 100 where user = 'BB'
*/
public static void testUpdateWithTx() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
// 取消数据的自动提交功能
conn.setAutoCommit(false);
String sql1 = "update user_table set balance = balance - 100 where user = ?";
update(conn, sql1, "AA");
// 模拟网络异常
System.out.println(10 / 0);
String sql2 = "update user_table set balance = balance + 100 where user = ?";
update(conn, sql2, "BB");
System.out.println("转账成功!");
// 提交数据
conn.commit();
} catch (Exception e) {
e.printStackTrace();
// 回滚数据
try {
conn.rollback();
System.out.println("转账失败!");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
} finally {
try {
// 修改其为自动提交数据
// 主要针对于使用数据库连接池的使用
conn.setAutoCommit(true);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
JDBCUtils.closeResource(conn, null);
}
}
/**
* @Author: ZZP
* @Date:2021/6/18
* @Version: 2.0 - 考虑数据库事物情况下的转账操作
* @Description:
*/
public static int update(Connection conn, String sql, Object... args) { // sql中的占位符的个数与可变形参的长度相同
PreparedStatement ps = null;
try {
// 1.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 2.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 3.执行操作
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 4.关闭资源
JDBCUtils.closeResource(null, ps);
}
return 0;
}
//******************************************************************************************************************
@Test
public void test() {
Connection connection = null;
try {
connection = JDBCUtils.getConnection();
System.out.println(connection.getTransactionIsolation());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
@Test
public void testTransactionSelect() throws Exception {
Connection conn = JDBCUtils.getConnection();
// 设置当前的隔离级别
conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
// 获取当前的隔离级别
System.out.println(conn.getTransactionIsolation());
// 取消自动提交数据
conn.setAutoCommit(false);
String sql = "select user, password, balance from user_table where user = ?";
User user = getInstance(conn, User.class, sql, "CC");
System.out.println(user);
}
@Test
public void testTransactionUpdate() throws Exception {
Connection conn = JDBCUtils.getConnection();
// 取消自动提交数据
conn.setAutoCommit(false);
String sql = "update user_table set balance = ? where user = ?";
update(conn, sql, 5000, "CC");
Thread.sleep(15000);
System.out.println("修改结束");
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 2.0 - 考虑上事务
* @Description: 针对于不同表的通用的查询操作,返回表中的一条记录
*/
public static <T> T getInstance(Connection conn, Class<T> clazz, String sql, Object... args) {
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
// 1.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 2.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 3.执行并返回结果集
resultSet = ps.executeQuery();
// 获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsmd = resultSet.getMetaData();
// 通过 ResultSetMetaData 获取结果集中的列数
int columnCount = rsmd.getColumnCount();
// 4.处理结果集
if (resultSet.next()) {
T t = clazz.newInstance();
// 处理结果集一行数据中的每一个列
for (int i = 0; i < columnCount; i++) {
// 获取列值
Object columnValue = resultSet.getObject(i + 1);
// 获取每个列的列名 getColumnName()
// 获取每个列的别名 getColumnLabel()
String columnLabel = rsmd.getColumnLabel(i + 1);
// 给 columnName 对象指定的某个属性,赋值为 columnValue : 通过反射
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t, columnValue);
}
return t;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBCUtils.closeResource(null, ps, resultSet);
}
return null;
}
}
package com.atguigu1.transaction;
/**
* @Author: ZZP
* @Date: Created in 9:54 2021/6/19
* @Version: 1.0
* @Description:
**/
public class User {
private String user;
private String password;
private int balance;
public User() {
}
public User(String user, String password, int balance) {
this.user = user;
this.password = password;
this.balance = balance;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
@Override
public String toString() {
return "user:" + user +
"\npassword:" + password +
"\nbalance:" + balance
;
}
}
package com.atguigu1.util;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* @Author: ZZP
* @Description:
* 操作数据库的工具类
*
* @Date: Created in 10:09 2021/6/16
* @Version: 1.0
**/
public class JDBCUtils {
/**
* @Author: ZZP
* @Date:2021/6/16
* @Description: 获取数据库的连接
**/
public static Connection getConnection() throws Exception {
// 读取配置文件中的四个基本信息
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
// 2.加载驱动
Class.forName(driverClass);
// 3.获取链接
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
/**
* @Author: ZZP
* @Date:2021/6/16
* @Description: 关闭连接和 Statement 的操作
*/
public static void closeResource(Connection conn, Statement ps){
// 7.关闭资源
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
/**
* @Author: ZZP
* @Date:2021/6/16
* @Description: 关闭连接和 Statement 和 ResultSet 的操作
*/
public static void closeResource(Connection conn, Statement ps, ResultSet rs){
// 7.关闭资源
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
package com.atguigu2.bean;
import java.sql.Date;
/**
* @Author: ZZP
* @Description:
* ORM编程思想 (object relation mapping)
* 一个数据表对应一个 java 类
* 表中的一条记录对应 java 类的一个对象
* 表中的一个字段对应 java 类的一个属性
*
* @Date: Created in 12:38 2021/6/16
* @Version: 1.0
**/
public class Customer {
private int id;
private String name;
private String email;
private Date birth;
public Customer() {
}
public Customer(int id, String name, String email, Date birth) {
this.id = id;
this.name = name;
this.email = email;
this.birth = birth;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", birth=" + birth +
'}';
}
}
package com.atguigu2.bean;
import java.sql.Date;
/**
* @Author: ZZP
* @Description:
*
* @Date: Created in 15:43 2021/6/16
* @Version: 1.0
**/
public class Order {
private int orderId;
private String orderName;
private Date orderDate;
public Order() {
}
public Order(int orderId, String orderName, Date orderDate) {
this.orderId = orderId;
this.orderName = orderName;
this.orderDate = orderDate;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
@Override
public String toString() {
return "Order{" +
"orderId=" + orderId +
", orderName='" + orderName + '\'' +
", orderDate=" + orderDate +
'}';
}
}
package com.atguigu2.dao;
import com.atguigu1.util.JDBCUtils;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: ZZP
* @Date: Created in 10:42 2021/6/19
* @Version: 1.0
* @Description:
* DAO: data(base) access object
* 封装了针对于数据表的通用操作
**/
public abstract class BaseDAO {
/**
* @Author: ZZP
* @Date:2021/6/18
* @Version: 2.0 - 考虑数据库事务
* @Description: 通用的增删改操作
*/
public int update(Connection conn, String sql, Object... args) { // sql中的占位符的个数与可变形参的长度相同
PreparedStatement ps = null;
try {
// 1.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 2.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 3.执行操作
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 4.关闭资源
JDBCUtils.closeResource(null, ps);
}
return 0;
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 2.0 - 考虑上事务
* @Description: 针对于不同表的通用的查询操作,返回表中的一条记录
*/
public <T> T getInstance(Connection conn, Class<T> clazz, String sql, Object... args) {
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
// 1.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 2.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 3.执行并返回结果集
resultSet = ps.executeQuery();
// 获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsmd = resultSet.getMetaData();
// 通过 ResultSetMetaData 获取结果集中的列数
int columnCount = rsmd.getColumnCount();
// 4.处理结果集
if (resultSet.next()) {
T t = clazz.newInstance();
// 处理结果集一行数据中的每一个列
for (int i = 0; i < columnCount; i++) {
// 获取列值
Object columnValue = resultSet.getObject(i + 1);
// 获取每个列的列名 getColumnName()
// 获取每个列的别名 getColumnLabel()
String columnLabel = rsmd.getColumnLabel(i + 1);
// 给 columnName 对象指定的某个属性,赋值为 columnValue : 通过反射
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t, columnValue);
}
return t;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBCUtils.closeResource(null, ps, resultSet);
}
return null;
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 2.0 - 考虑上事务
* @Description: 针对于不同表的通用的查询操作,返回表中的多条记录
*/
public <T> List<T> getForList(Connection conn, Class<T> clazz, String sql, Object... args) {
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
// 1.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 2.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 3.执行并返回结果集
resultSet = ps.executeQuery();
// 获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsmd = resultSet.getMetaData();
// 通过 ResultSetMetaData 获取结果集中的列数
int columnCount = rsmd.getColumnCount();
// 创建集合对象
ArrayList<T> list = new ArrayList<T>();
// 4.处理结果集
while (resultSet.next()) {
T t = clazz.newInstance();
// 处理结果集一行数据中的每一个列:给 t 对象指定的属性赋值
for (int i = 0; i < columnCount; i++) {
// 获取列值
Object columnValue = resultSet.getObject(i + 1);
// 获取每个列的列名 getColumnName()
// 获取每个列的别名 getColumnLabel()
String columnLabel = rsmd.getColumnLabel(i + 1);
// 给 columnName 对象指定的某个属性,赋值为 columnValue : 通过反射
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t, columnValue);
}
list.add(t);
}
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBCUtils.closeResource(null, ps, resultSet);
}
return null;
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0 - 考虑上事务
* @Description: 查询特殊值的通用的方法
*/
public <E> E getValue(Connection conn, String sql, Object... args) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 1.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 2.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 3.执行并返回结果集
rs = ps.executeQuery();
if (rs.next()) {
return (E) rs.getObject(1);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.closeResource(null, ps, rs);
}
return null;
}
}
package com.atguigu2.dao;
import com.atguigu2.bean.Customer;
import java.sql.Connection;
import java.sql.Date;
import java.util.List;
/**
* @Author: ZZP
* @Date: Created in 10:59 2021/6/19
* @Version: 1.0
* @Description: 此接口用于规范针对于 customers 表的常用操作
**/
public interface CustomerDAO {
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 将 cust 对象添加到数据库中
*/
void insert(Connection conn, Customer cust);
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 根据指定的 id 删除表中的一条记录
*/
void deleteByTd(Connection conn, int id);
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 针对于内存中的 cust 对象,去修改数据表中的记录
*/
void update(Connection conn, Customer cust);
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 根据指定的 id 查询得到对应的 Customer 对象
*/
Customer getCustomerById(Connection conn, int id);
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 查询表中的所有记录构成的集合
*/
List<Customer> getAll(Connection conn);
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 返回数据表中数据的条目数
*/
Long getCount(Connection conn);
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 返回数据表中的最大生日数
*/
Date getMaxBirth(Connection conn);
}
package com.atguigu2.dao;
import com.atguigu2.bean.Customer;
import java.sql.Connection;
import java.sql.Date;
import java.util.List;
/**
* @Author: ZZP
* @Date: Created in 11:28 2021/6/19
* @Version: 1.0
* @Description:
**/
public class CustomerDAOImpl extends BaseDAO implements CustomerDAO {
@Override
public void insert(Connection conn, Customer cust) {
String sql = "insert into customers(name,email,birth)values(?,?,?)";
update(conn, sql, cust.getName(), cust.getEmail(), cust.getBirth());
}
@Override
public void deleteByTd(Connection conn, int id) {
String sql = "delete from customers where id = ?";
update(conn, sql, id);
}
@Override
public void update(Connection conn, Customer cust) {
String sql = "update customers set name = ?,email = ?,birth = ? where id = ?";
update(conn, sql, cust.getName(), cust.getEmail(), cust.getBirth(), cust.getId());
}
@Override
public Customer getCustomerById(Connection conn, int id) {
String sql = "select id,name,email,birth from customers where id = ?";
Customer customer = getInstance(conn, Customer.class, sql, id);
return customer;
}
@Override
public List<Customer> getAll(Connection conn) {
String sql = "select id,name,email,birth from customers";
List<Customer> list = getForList(conn, Customer.class, sql);
return list;
}
@Override
public Long getCount(Connection conn) {
String sql = "select count(*) from customers";
return getValue(conn, sql);
}
@Override
public Date getMaxBirth(Connection conn) {
String sql = "select max(birth) from customers";
return getValue(conn, sql);
}
}
package com.atguigu2.dao.junit;
import com.atguigu1.util.JDBCUtils;
import com.atguigu2.bean.Customer;
import com.atguigu2.dao.CustomerDAOImpl;
import org.junit.Test;
import java.sql.Connection;
import java.sql.Date;
import java.util.List;
/**
* @Author: ZZP
* @Date: Created in 11:52 2021/6/19
* @Version: 1.0
* @Description:
**/
public class CustomerDAOImplTest {
private CustomerDAOImpl dao = new CustomerDAOImpl();
@Test
public void insert() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
Customer cust = new Customer(1, "王小虎", "wangxiaohu@qq.com", new Date(546123168456L));
dao.insert(conn, cust);
System.out.println("添加成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
@Test
public void deleteByTd() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
dao.deleteByTd(conn, 21);
System.out.println("删除成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
@Test
public void update() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
Customer cust = new Customer(18, "贝多芬", "beiduofen@qq.com", new Date(1654613186464L));
dao.update(conn, cust);
System.out.println("修改成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
@Test
public void getCustomerById() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
Customer cust = dao.getCustomerById(conn, 18);
System.out.println(cust);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
@Test
public void getAll() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
List<Customer> list = dao.getAll(conn);
list.forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
@Test
public void getCount() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
Long count = dao.getCount(conn);
System.out.println("表中的记录数为:" + count);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
@Test
public void getMaxBirth() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
Date maxBirth = dao.getMaxBirth(conn);
System.out.println("最大的生日为:" + maxBirth);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
}
package com.atguigu3.dao;
import com.atguigu1.util.JDBCUtils;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: ZZP
* @Date: Created in 10:42 2021/6/19
* @Version: 1.0
* @Description:
* DAO: data(base) access object
* 封装了针对于数据表的通用操作
**/
public abstract class BaseDAO<T> {
private Class<T> clazz = null;
{
// 获取当前 BaseDAO 的子类继承的父类中的泛型
Type genericSuperclass = this.getClass().getGenericSuperclass();
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
Type[] typeArguments = parameterizedType.getActualTypeArguments(); // 获取父类的泛型
clazz = (Class<T>) typeArguments[0];// 泛型的第一个参数
}
/**
* @Author: ZZP
* @Date:2021/6/18
* @Version: 2.0 - 考虑数据库事务
* @Description: 通用的增删改操作
*/
public int update(Connection conn, String sql, Object... args) { // sql中的占位符的个数与可变形参的长度相同
PreparedStatement ps = null;
try {
// 1.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 2.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 3.执行操作
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 4.关闭资源
JDBCUtils.closeResource(null, ps);
}
return 0;
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 2.0 - 考虑上事务
* @Description: 针对于不同表的通用的查询操作,返回表中的一条记录
*/
public T getInstance(Connection conn, String sql, Object... args) {
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
// 1.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 2.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 3.执行并返回结果集
resultSet = ps.executeQuery();
// 获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsmd = resultSet.getMetaData();
// 通过 ResultSetMetaData 获取结果集中的列数
int columnCount = rsmd.getColumnCount();
// 4.处理结果集
if (resultSet.next()) {
T t = clazz.newInstance();
// 处理结果集一行数据中的每一个列
for (int i = 0; i < columnCount; i++) {
// 获取列值
Object columnValue = resultSet.getObject(i + 1);
// 获取每个列的列名 getColumnName()
// 获取每个列的别名 getColumnLabel()
String columnLabel = rsmd.getColumnLabel(i + 1);
// 给 columnName 对象指定的某个属性,赋值为 columnValue : 通过反射
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t, columnValue);
}
return t;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBCUtils.closeResource(null, ps, resultSet);
}
return null;
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 2.0 - 考虑上事务
* @Description: 针对于不同表的通用的查询操作,返回表中的多条记录
*/
public List<T> getForList(Connection conn, String sql, Object... args) {
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
// 1.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 2.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 3.执行并返回结果集
resultSet = ps.executeQuery();
// 获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsmd = resultSet.getMetaData();
// 通过 ResultSetMetaData 获取结果集中的列数
int columnCount = rsmd.getColumnCount();
// 创建集合对象
ArrayList<T> list = new ArrayList<T>();
// 4.处理结果集
while (resultSet.next()) {
T t = clazz.newInstance();
// 处理结果集一行数据中的每一个列:给 t 对象指定的属性赋值
for (int i = 0; i < columnCount; i++) {
// 获取列值
Object columnValue = resultSet.getObject(i + 1);
// 获取每个列的列名 getColumnName()
// 获取每个列的别名 getColumnLabel()
String columnLabel = rsmd.getColumnLabel(i + 1);
// 给 columnName 对象指定的某个属性,赋值为 columnValue : 通过反射
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t, columnValue);
}
list.add(t);
}
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBCUtils.closeResource(null, ps, resultSet);
}
return null;
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0 - 考虑上事务
* @Description: 查询特殊值的通用的方法
*/
public <E> E getValue(Connection conn, String sql, Object... args) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 1.预编译 sql 语句,返回 PreparedStatement 的实例
ps = conn.prepareStatement(sql);
// 2.填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 3.执行并返回结果集
rs = ps.executeQuery();
if (rs.next()) {
return (E) rs.getObject(1);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.closeResource(null, ps, rs);
}
return null;
}
}
package com.atguigu3.dao;
import com.atguigu2.bean.Customer;
import java.sql.Connection;
import java.sql.Date;
import java.util.List;
/**
* @Author: ZZP
* @Date: Created in 10:59 2021/6/19
* @Version: 1.0
* @Description: 此接口用于规范针对于 customers 表的常用操作
**/
public interface CustomerDAO {
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 将 cust 对象添加到数据库中
*/
void insert(Connection conn, Customer cust);
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 根据指定的 id 删除表中的一条记录
*/
void deleteByTd(Connection conn, int id);
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 针对于内存中的 cust 对象,去修改数据表中的记录
*/
void update(Connection conn, Customer cust);
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 根据指定的 id 查询得到对应的 Customer 对象
*/
Customer getCustomerById(Connection conn, int id);
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 查询表中的所有记录构成的集合
*/
List<Customer> getAll(Connection conn);
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 返回数据表中数据的条目数
*/
Long getCount(Connection conn);
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 返回数据表中的最大生日数
*/
Date getMaxBirth(Connection conn);
}
package com.atguigu3.dao;
import com.atguigu2.bean.Customer;
import java.sql.Connection;
import java.sql.Date;
import java.util.List;
/**
* @Author: ZZP
* @Date: Created in 11:28 2021/6/19
* @Version: 1.0
* @Description:
**/
public class CustomerDAOImpl extends BaseDAO<Customer> implements CustomerDAO {
@Override
public void insert(Connection conn, Customer cust) {
String sql = "insert into customers(name,email,birth)values(?,?,?)";
update(conn, sql, cust.getName(), cust.getEmail(), cust.getBirth());
}
@Override
public void deleteByTd(Connection conn, int id) {
String sql = "delete from customers where id = ?";
update(conn, sql, id);
}
@Override
public void update(Connection conn, Customer cust) {
String sql = "update customers set name = ?,email = ?,birth = ? where id = ?";
update(conn, sql, cust.getName(), cust.getEmail(), cust.getBirth(), cust.getId());
}
@Override
public Customer getCustomerById(Connection conn, int id) {
String sql = "select id,name,email,birth from customers where id = ?";
Customer customer = getInstance(conn, sql, id);
return customer;
}
@Override
public List<Customer> getAll(Connection conn) {
String sql = "select id,name,email,birth from customers";
List<Customer> list = getForList(conn, sql);
return list;
}
@Override
public Long getCount(Connection conn) {
String sql = "select count(*) from customers";
return getValue(conn, sql);
}
@Override
public Date getMaxBirth(Connection conn) {
String sql = "select max(birth) from customers";
return getValue(conn, sql);
}
}
package com.atguigu3.dao.junit;
import com.atguigu4.util.JDBCUtils;
import com.atguigu2.bean.Customer;
import com.atguigu3.dao.CustomerDAOImpl;
import org.junit.Test;
import java.sql.Connection;
import java.sql.Date;
import java.util.List;
/**
* @Author: ZZP
* @Date: Created in 11:52 2021/6/19
* @Version: 1.0
* @Description:
**/
public class CustomerDAOImplTest {
private CustomerDAOImpl dao = new CustomerDAOImpl();
@Test
public void insert() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
Customer cust = new Customer(1, "王小虎", "wangxiaohu@qq.com", new Date(546123168456L));
dao.insert(conn, cust);
System.out.println("添加成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
@Test
public void deleteByTd() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
dao.deleteByTd(conn, 21);
System.out.println("删除成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
@Test
public void update() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
Customer cust = new Customer(18, "贝多芬", "beiduofen@qq.com", new Date(1654613186464L));
dao.update(conn, cust);
System.out.println("修改成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
@Test
public void getCustomerById() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection3();
Customer cust = dao.getCustomerById(conn, 18);
System.out.println(cust);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
@Test
public void getAll() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
List<Customer> list = dao.getAll(conn);
list.forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
@Test
public void getCount() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
Long count = dao.getCount(conn);
System.out.println("表中的记录数为:" + count);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
@Test
public void getMaxBirth() {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
Date maxBirth = dao.getMaxBirth(conn);
System.out.println("最大的生日为:" + maxBirth);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
}
package com.atguigu4.connection;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
import com.mchange.v2.c3p0.cfg.C3P0Config;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
/**
* @Author: ZZP
* @Date: Created in 15:26 2021/6/19
* @Version: 1.0
* @Description:
**/
public class C3P0Test {
// 方式一:
@Test
public void testGetConnection() throws Exception {
// 获取 C3P0 数据库连接池
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass("com.mysql.cj.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=UTC&rewriteBatchedStatements=true");
cpds.setUser("root");
cpds.setPassword("719238");
// 通过设置相关的参数,对数据库连接池进行管理
// 设置初始时数据库连接池中的连接数
cpds.setInitialPoolSize(10);
Connection conn = cpds.getConnection();
System.out.println(conn);
// 销毁 C3P0 数据库连接池
DataSources.destroy(cpds);
}
// 方式二:使用配置文件
@Test
public void testGetConnection1() throws SQLException {
ComboPooledDataSource cpds = new ComboPooledDataSource("helloC3P0");
Connection conn = cpds.getConnection();
System.out.println(cpds);
}
}
package com.atguigu4.connection;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
/**
* @Author: ZZP
* @Date: Created in 16:50 2021/6/19
* @Version: 1.0
* @Description: 测试 DBCP 的数据库连接池技术
**/
public class DBCPTest {
// 方式一
@Test
public void testGetConnection() throws SQLException {
// 创建 DBCP 的数据库连接池
BasicDataSource source = new BasicDataSource();
// 设置基本信息
source.setDriverClassName("com.mysql.cj.jdbc.Driver");
source.setUrl("jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=UTC&rewriteBatchedStatements=true");
source.setUsername("root");
source.setPassword("719238");
// 设置其他涉及数据库连接池管理的相关属性
source.setInitialSize(10);
source.setMaxTotal(10);
Connection conn = source.getConnection();
System.out.println(conn);
}
// 方式二:使用配置文件
@Test
public void testGetConnection1() throws Exception {
Properties pros = new Properties();
// 方式一:
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");
// 方式二:
//FileInputStream fs = new FileInputStream(new File("src/dbcp.properties"));
pros.load(is);
BasicDataSource source = BasicDataSourceFactory.createDataSource(pros);
Connection conn = source.getConnection();
System.out.println(conn);
}
}
package com.atguigu4.connection;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
/**
* @Author: ZZP
* @Date: Created in 18:52 2021/6/19
* @Version: 1.0
* @Description:
**/
public class DruidTest {
@Test
public void getConnection() throws Exception {
Properties pros = new Properties();
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
pros.load(is);
DataSource source = DruidDataSourceFactory.createDataSource(pros);
Connection conn = source.getConnection();
}
}
package com.atguigu4.util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* @Author: ZZP
* @Date: Created in 16:38 2021/6/19
* @Version: 1.0
* @Description:
**/
public class JDBCUtils {
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 使用 Druid 数据库连接池技术
*/
private static DataSource source1;
static {
try {
Properties pros = new Properties();
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
pros.load(is);
source1 = DruidDataSourceFactory.createDataSource(pros);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection3() throws SQLException {
Connection conn = source1.getConnection();
return conn;
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 使用 C3P0 的数据库连接池技术
*/
// C3P0 数据库连接池只需提供一个即可
private static ComboPooledDataSource cpds = new ComboPooledDataSource("helloC3P0");
public static Connection getConnection1() throws SQLException {
Connection conn = cpds.getConnection();
return conn;
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 使用 DBCP 的数据库连接池技术
*/
// 创建一个 DBCP 数据库连接池
private static BasicDataSource source;
static {
try {
Properties pros = new Properties();
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");
pros.load(is);
source = BasicDataSourceFactory.createDataSource(pros);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection2() throws Exception {
Connection conn = source.getConnection();
return conn;
}
/**
* @Author: ZZP
* @Date:2021/6/16
* @Description: 获取数据库的连接
**/
public static Connection getConnection() throws Exception {
// 读取配置文件中的四个基本信息
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
// 2.加载驱动
Class.forName(driverClass);
// 3.获取链接
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
/**
* @Author: ZZP
* @Date:2021/6/16
* @Description: 关闭连接和 Statement 的操作
*/
public static void closeResource(Connection conn, Statement ps) {
// 7.关闭资源
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
/**
* @Author: ZZP
* @Date:2021/6/16
* @Description: 关闭连接和 Statement 和 ResultSet 的操作
*/
public static void closeResource(Connection conn, Statement ps, ResultSet rs) {
// 7.关闭资源
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 使用 dbutils。jar 中提供的 DbUtils 工具类,实现资源的关闭
*/
public static void closeResource1(Connection conn, Statement ps, ResultSet rs) {
//try {
// DbUtils.close(conn);
//} catch (SQLException throwables) {
// throwables.printStackTrace();
//}
//
//try {
// DbUtils.close(ps);
//} catch (SQLException throwables) {
// throwables.printStackTrace();
//}
//
//try {
// DbUtils.close(rs);
//} catch (SQLException throwables) {
// throwables.printStackTrace();
//}
DbUtils.closeQuietly(conn, ps, rs);
}
}
package com.atguigu5.dbutils;
import com.atguigu2.bean.Customer;
import com.atguigu4.util.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.*;
import org.junit.Test;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
/**
* @Author: ZZP
* @Date: Created in 19:17 2021/6/19
* @Version: 1.0
* @Description: commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,封装了针对于数据库的增删改查操作
**/
public class QueryRunnerTest {
@Test
public void testInsert() throws SQLException {
Connection conn = null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCUtils.getConnection3();
String sql = "insert into customers(name ,email,birth) value (?,?,?)";
int insertCount = runner.update(conn, sql, "蔡徐坤", "caixukun@qq.com", "1997-01-01");
System.out.println("添加了 " + insertCount + " 条记录");
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: BeanHandler:是 ResultSetHandler 接口的实现类,用于封装表中的一条记录
*/
@Test
public void testQuery1() {
Connection conn = null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCUtils.getConnection3();
String sql = "select id, name, email, birth from customers where id = ?";
BeanHandler<Customer> handler = new BeanHandler<>(Customer.class);
Customer customer = runner.query(conn, sql, handler, 23);
System.out.println(customer);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: BeanListHandler:是 ResultSetHandler 接口的实现类,用于封装表中的多条记录构成的集合
*/
@Test
public void testQuery2() {
Connection conn = null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCUtils.getConnection3();
String sql = "select id, name, email, birth from customers where id < ?";
BeanListHandler<Customer> listHandler = new BeanListHandler<Customer>(Customer.class);
List<Customer> customerList = runner.query(conn, sql, listHandler, 23);
customerList.forEach(System.out::println);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: MapHandler:是 ResultSetHandler 接口的实现类,对应表中的一条记录。将字段及相应字段的值作为 map 中的 key 和 value
*/
@Test
public void testQuery3() {
Connection conn = null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCUtils.getConnection3();
String sql = "select id, name, email, birth from customers where id = ?";
MapHandler mapHandler = new MapHandler();
Map<String, Object> customerMap = runner.query(conn, sql, mapHandler, 23);
System.out.println(customerMap);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: MapHandler:是 ResultSetHandler 接口的实现类,对应表中的多条记录。将字段及相应字段的值作为 map 中的 key 和 value。将这些 map 添加到 List 中
*/
@Test
public void testQuery4() {
Connection conn = null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCUtils.getConnection3();
String sql = "select id, name, email, birth from customers where id < ?";
MapListHandler mapListHandler = new MapListHandler();
List<Map<String, Object>> customerMapList = runner.query(conn, sql, mapListHandler, 23);
customerMapList.forEach(System.out::println);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: ScalarHandler:用于查询特殊值
*/
@Test
public void testQuery5() {
Connection conn = null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCUtils.getConnection3();
String sql = "select count(*) from customers";
ScalarHandler scalarHandler = new ScalarHandler();
Object customerCount = runner.query(conn, sql, scalarHandler);
System.out.println(customerCount);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: 自定义 ResultSetHandler 的实现类
*/
@Test
public void testQuery7() {
Connection conn = null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCUtils.getConnection3();
String sql = "select max(birth) from customers";
ScalarHandler scalarHandler = new ScalarHandler();
Object maxBirth = runner.query(conn, sql, scalarHandler);
System.out.println(maxBirth);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
/**
* @Author: ZZP
* @Date:2021/6/19
* @Version: 1.0
* @Description: ScalarHandler:用于查询特殊值
*/
@Test
public void testQuery6() {
Connection conn = null;
try {
QueryRunner runner = new QueryRunner();
conn = JDBCUtils.getConnection3();
String sql = "select id, name, email, birth from customers where id = ?";
ResultSetHandler handler = new ResultSetHandler<Customer>() {
@Override
public Customer handle(ResultSet resultSet) throws SQLException {
//return new Customer(12,"成龙","jacky@qq.com",new Date(54635313153153L));
if (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String email = resultSet.getString("email");
Date birth = resultSet.getDate("birth");
Customer customer = new Customer(id, name, email, birth);
return customer;
}
return null;
}
};
Object customer = runner.query(conn, sql, handler, 23);
System.out.println(customer);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
}
}
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=UTC&rewriteBatchedStatements=true
username=root
password=719238
initialSize=10
maxTotal=10
\ No newline at end of file
url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=UTC&rewriteBatchedStatements=true
username=root
password=719238
driverClassName=com.mysql.cj.jdbc.Driver
initialSize=10
maxActive=10
\ No newline at end of file
#获取基本信息
user=root
password=719238
url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=UTC&rewriteBatchedStatements=true
driverClass=com.mysql.cj.jdbc.Driver
\ No newline at end of file
文件已添加
文件已添加
#获取基本信息
user=root
password=719238
url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=UTC&rewriteBatchedStatements=true
driverClass=com.mysql.cj.jdbc.Driver
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="helloC3P0">
<!-- 通过获取连接的四个基本信息 -->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true</property>
<property name="user">root</property>
<property name="password">719238</property>
<!-- 进行数据库连接池管理的基本信息 -->
<!-- 当数据库连接池中的连接数不够时,C3P0 一次性向数据库服务器申请的连接数 -->
<property name="acquireIncrement">5</property>
<!-- C3P0 数据库连接池中初始化的连接数 -->
<property name="initialPoolSize">10</property>
<!-- C3P0 数据库连接池中维护的最少的连接数 -->
<property name="minPoolSize">10</property>
<!-- C3P0 数据库连接池中维护的最多的连接数 -->
<property name="maxPoolSize">100</property>
<!-- C3P0 数据库连接池中维护的 Statement 的最大个数 -->
<property name="maxStatements">50</property>
<!-- 每个连接中可以使用最多的 Statement 的个数 -->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册