JDBC复习2

时间:2024-10-07 13:33:02

前面复习了一些基础知识以及如何抽取一些常用的代码,接下来就结合junit4做一个增删改查的小demo

重点是这么几个步骤:1.创建连接 2.编写sql语句 3.编写sql语句的载体 4.如果是PreparedStatement的话要设置占位符 5.执行sql语句 6.其他

在码代码过程中,发现自己规范引错了,正确的应该引java.sql下的,我引成了java.mysql.jdbc包下的

此外,发现自己在创建sql载体的时候不熟练,忘记了。对于Statement的话,创建载体是 stmt = conn.createStatement()

对于PreparedStatement的话,创建载体是 pst= conn.prepareStatement();  ***注意不要加了个d,这里是prepare

1.新建web项目,引入jar包

JDBC复习2

2.创建数据库,编写sql脚本

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0; -- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; SET FOREIGN_KEY_CHECKS = 1;

3.编写工具类和测试类

package JDBC温习;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; /**
* 提供获取连接和释放资源的 方法
*/
public class JDBCUtils { /*
* 获取连接方法
*/
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=utf-8", "root", "123456");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/*
* 释放资源
*/
public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} }
}
package JDBC温习;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import org.junit.Test; /*
* 测试类
*/
public class test {
/*
* 添加用户
*/
@Test
public void add() {
Connection conn = null;
PreparedStatement pst = null; try {
// 1.获取连接
conn = (Connection) JDBCUtils.getConnection();
// 2.编写sql语句(采用PreparedStatement)
String sql = "insert into student values(?,?,?) ";
// 3.获取执行sql语句的载体
pst = conn.prepareStatement(sql);
// 4.设置占位符
pst.setInt(1, 6);
pst.setString(2, "周东");
pst.setString(3, "男");
// 5.执行插入操作
int updateRow = pst.executeUpdate();
if (updateRow > 0) {
System.out.println("插入成功");
} else {
System.out.println("插入失败");
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JDBCUtils.release(conn, pst, null);
}
} /*
* 删除用户
*/
@Test
public void deleteById() {
Connection conn = null;
Statement stmt = null;
try {
// 1. 获取连接
conn = JDBCUtils.getConnection();
// 2. 编写sql语句(采用Statement方式)
String sql = "delete from student where id=2 ";
// 3. 获取sql语句载体
stmt = conn.createStatement();
// 4. 执行sql语句
int updateRow = stmt.executeUpdate(sql);
if (updateRow > 0) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
// 这里因为我工具类提供的是preparedStatement的关闭.
JDBCUtils.release(conn, null, null);
}
} /*
* 修改用户
*/
@Test
public void updateById() {
Connection conn = null;
PreparedStatement pst = null;
try {
// 1. 获取连接
conn = JDBCUtils.getConnection();
// 2. 编写sql语句(PreparedStatement方式)
String sql = "update student set name=? , sex=? where id=? ";
// 3. 创建sql载体
pst = conn.prepareStatement(sql);
// 4. 设置占位符
pst.setString(1, "小花");
pst.setString(2, "女");
pst.setInt(3, 4);
// 5. 执行sql语句
int updateRow = pst.executeUpdate();
if (updateRow > 0) {
System.out.println("更新成功");
} else {
System.out.println("更新失败");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
JDBCUtils.release(conn, pst, null);
}
} /*
* 查询用户 PS:简单查询,根据ID查某个学生
*/
@Test
public void findById() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 1. 获取连接
conn = JDBCUtils.getConnection();
// 2.编写sql语句
String sql = "select * from student where id = 6 ";
// 3.编写sql载体
stmt = conn.createStatement();
//4.执行sql语句
rs = stmt.executeQuery(sql);
//5. 遍历
while(rs.next()) {
System.out.println(rs.getString(2)+"====="+rs.getString("sex"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
JDBCUtils.release(conn, null, rs);
}
}
}

以上是JDBC方面知识点的简单回顾