package com.tedu.jdbc;
import org.junit.Test;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* 通过jdbc来实现对数据库的crud
*/
public class JdbcDemo {
/**
* 通过Driver来获取连接各方法
*/
public static Connection getConnection() throws Exception {
// 通过类加载器来加载配置文件
InputStream res = JdbcDemo.class.getClassLoader().getResourceAsStream("JDBC.properties");
// 创建一个属性对象properties
Properties pro = new Properties();
// 通过这个Pro来加载流到内存
pro.load(res);
// 可以通过Pro获取配置文件中的值了
// 注册驱动
Class.forName(pro.getProperty("driverClass"));
// 通过DriverManger来获取连接
Connection conn = DriverManager.getConnection(pro.getProperty("url"), pro.getProperty("username"), pro.getProperty("password"));
return conn;
}
/**
* 关闭连接的方法
*/
public static void closeConnection(Connection conn, Statement sta, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
rs = null;
}
try {
if (sta != null) {
sta.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
sta = null;
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn = null;
}
}
@Test
public void testInsert() throws Exception {
// 获取连接
Connection conn = getConnection();
// 获取传输器对象--Pre 用预编译的,
String sql = "insert into student(id,name,class) values(?,?,?)";
PreparedStatement pre = conn.prepareStatement(sql);
pre.setInt(1, 4);
pre.setString(2, "逆流");
pre.setString(3, "6班");
// 执行sql
int rows = pre.executeUpdate();
System.out.println(rows);
// 关闭流
closeConnection(conn, pre, null);
}
/**
* 通用的增加,删除,修改方法
*/
public static void update(String sql, Object... obj) {
Connection conn = null;
PreparedStatement pre = null;
try {
conn = getConnection();
// 预编译SQL
pre = conn.prepareStatement(sql);
// 循环设置参数
for (int i = 0; i < obj.length; i++) {
pre.setObject(i + 1, obj[i]);
}
// 执行sql语句
pre.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeConnection(conn, pre, null);
}
}
/**
* 查询方法
*/
@Test
public void testFind() {
Connection conn = null;
PreparedStatement pre = null;
ResultSet rs = null;
try {
// 获取连接
conn = getConnection();
// 定义sql
String sql = "select * from student where id = ?";
// 获取传输器对象执行SQL
pre = conn.prepareStatement(sql);
pre.setInt(1, 1);
// 开始执行获取结果集
rs = pre.executeQuery();
// 遍历获取结果
if (rs.next()) { // next:判断有没有结果集,同是指针下移一行,从表头移到数据行
rs.getObject(1);
rs.getObject(2);
// 在这里可以将获取到的值封装到对象中
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 通用的查询方法
*/
@Test
public void search(String sql, Object... obj) {
Connection conn = null;
PreparedStatement pre = null;
ResultSet rs = null;
try {
// 获取连接
conn = getConnection();
// 获取传输器对象执行SQL
pre = conn.prepareStatement(sql);
// 设置值
for (int i = 0; i < obj.length; i++) {
pre.setObject(i + 1, obj[i]);
}
// 执行查询获取结果集
rs = pre.executeQuery();
// 获取结果集的表头信息
ResultSetMetaData metaData = pre.getMetaData();
// 获取结果集中有几列
int columns = metaData.getColumnCount();
List<Student> studentList = new ArrayList<>();
while (rs.next()) {
// 这里的next有连个作用,1.返回是否有值,2.将指针从表头下移一行,要与集合中的遍历区别
Student student = new Student();
for (int i = 0; i < columns; i++) {
// 获取列名,后面要通过这个列名反射找到对象的属性进行封装
// 获取值
Object columnValue = rs.getObject(i + 1);
// 这里要注意--getColumnName是获取数据库中的列名,如果取了别名要用getColumnLabal()
String columnName = metaData.getColumnName(i + 1);
// 通过反射将值设置进对象
Field field = Student.class.getDeclaredField(columnName);
field.setAccessible(true);
field.set(student, columnValue);
}
studentList.add(student);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
closeConnection(conn, pre, null);
}
}
}
JDBC实现CRUD
最新推荐文章于 2022-11-19 08:52:02 发布