一、普通版本
由于使用的mysql版本为8.0.22,所以对应的mysql驱动为com.mysql.cj.jdbc.Driver,注意根据不同的mysql版本选择不同的驱动。
jdbc.properties文件内容如下:
user=root
password=cg
url=jdbc:mysql://localhost:3306/girls?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
driverClass=com.mysql.cj.jdbc.Driver
Eclipse代码如下:
package com.atguigu3.preparedstatement;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
import org.junit.Test;
public class PreparedStatementUpdaeTest {
@Test
public void testInsert() {
Connection conn = null;
PreparedStatement ps = null;
try {
// 1.读取配置文件中的4个基本信息
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("pa