jdbc.properties
url=jdbc:mysql://localhost:3306/db4?serverTimezone=Asia/Shanghai user=root password=0000 driver=com.mysql.cj.jdbc.Driver
工具类
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
static {
//读取资源文件
try {
//1.创建Properties集合类。
Properties pro = new Properties();
//2.加载文件(动态获取文件路径)
//获取src路径下的文件的方式--> ClassLoader类加载器
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();
pro.load(new FileReader(path));
//3.获取数据,赋值
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
//4.注册驱动
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection () throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static void close (Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close (Statement stmt1, Statement stmt2, Connection conn) {
if (stmt1 != null) {
try {
stmt1.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stmt2 != null) {
try {
stmt2.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close (ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
通过一个简单的练习,了解Jdbc工具类
import cn.itcast.util.JDBCUtils;
import java.sql.*;
import java.util.Scanner;
public class JdbcDemo4 {
public static void main (String[] args) {
//1.键盘录入,接受用户名和密码
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = sc.next();
System.out.println("请输入密码:");
String password = sc.next();
//2.调用方法
boolean flag = new JdbcDemo4().login(name, password);
//3.判断
if (flag){
System.out.println("登录成功");
}else{
System.out.println("用户名或密码错误");
}
}
//登录方法
public boolean login(String username,String password){
if (username==null||password==null){
return false;
}
//连接数据库判断是否登录成
Connection conn =null;
ResultSet rs =null;
PreparedStatement pstmt =null;
try {
//1.调用getConnection获取连接
conn = JDBCUtils.getConnection();
//2.定义sql语句
String sql = "select * from user where username = ? and password=?";
//3.获取执行sql的对象
pstmt = conn.prepareStatement(sql);
//给?赋值
pstmt.setString(1,username);
pstmt.setString(2,password);
//4.执行查询,不需要传递sql
rs = pstmt.executeQuery();
//5.判断
return rs.next();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtils.close(rs,pstmt,conn);
}
return false;
}
}
简单的事务回滚
import cn.itcast.util.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JdbcDemo5 {
public static void main (String[] args) {
Connection conn = null;
PreparedStatement prep1 = null;
PreparedStatement prep2 = null;
try {
//1.调用getConnection方法实现连接
conn = JDBCUtils.getConnection();
//开启事务
conn.setAutoCommit(false);
//2.定义sql语句
String sql1 = "update user set balance=balance+? where id=?";
String sql2 = "update user set balance=balance-? where id=?";
//3.获取执行sql对象
prep1 = conn.prepareStatement(sql1);
prep2 = conn.prepareStatement(sql2);
//4.给?赋值
prep1.setDouble(1, 500);
prep1.setDouble(2, 1);
prep2.setDouble(1, 500);
prep2.setDouble(2, 2);
//5.执行sql
prep1.executeUpdate();
//手动制造异常
int i=3/0;
prep2.executeUpdate();
//提交事务
conn.commit();
} catch (Exception throwables) {
try {
//事务的回滚
if (conn!=null){
conn.rollback();
}
} catch (SQLException e) {
e.printStackTrace();
}
throwables.printStackTrace();
} finally {
JDBCUtils.close(prep1, prep2, conn);
}
}
}
druid.properties
driverClassName=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/db3?serverTimezone=GMT username=root password=0000 # 初始化连接数量 initialSize=5 # 最大连接数 maxActive=10 # 超时时间 maxWait=3000
Druid工具类
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
//1.定义成员变量
private static DataSource ds;
static {
try {
//1.加载配置文件
Properties pro = new Properties();
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//2.获取连接池对象
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
public static void close(Statement stmt,Connection conn){
close(null,stmt,conn);
}
public static void close(ResultSet rs, Statement stmt, Connection conn){
if (rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stmt!=null){
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn!=null){
try {
//归还连接
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static DataSource getDataSource(){
return ds;
}
}
import cn.itcat.domain.Emp;
import cn.itcat.utils.JDBCUtils;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public class JdbcTemplateDemo2 {
//1.导入jar包
//2.创建JdbcTemplate对象 获取连接
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
@Test
public void getUpdate () {
//3.定义sql语句
String sql="update emp set salary=? where id=?";
//4.执行sql
int count = template.update(sql, 10000,1001);
System.out.println(count);
}
@Test
public void getInsert(){
//3.定义sql语句
String sql = "insert into emp(id,ename,dept_id) values(?,?,?)";
//4.执行sql
int count = template.update(sql, 1015,"郭靖", 10);
System.out.println(count);
}
@Test
public void getDelete(){
//3.定义sql语句
String sql="delete from emp where id=?";
//4.执行sql
int count = template.update(sql, 1015);
System.out.println(count);
}
//查询id为1001的记录,将其封装为Map集合
//注意:查询的结果集长度只能是1
@Test
public void getMap(){
//3.定义sql语句
String sql="select * from emp where id=?";
//4.执行sql
Map map = template.queryForMap(sql, 1001);
System.out.println(map);
}
//查询所有记录,将其封装为List
@Test
public void getList(){
//3.定义sql语句
String sql="select * from emp ";
//4.执行sql
List



