package com.util;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class jdbcUtil {
/**
* 连接的数据库地址 jdbc:mysql://主机名:端口号?字符编码
* 其中:jdbc: mysql://localhost/openemm?这个代表数据库链接地址,
* useUnicode=yes&characterEncoding=UTF-8这个代表允许用户自己设定数据库编码,而且设置成UTF-8
**/
static String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
/**
* 无法事先知道使用者将加载什么类,而必须让使用者指定类名称以加载类
* 可以使用 Class 的静态 forName() 方法实现动态加载类。
**/
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//封装数据库的连接
public static Connection getConnection()
{
Connection con = null;
try {
con = DriverManager.getConnection(url,"root","123456");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return con;
}
//统一的增删改
//executeUpdate 的返回值是一个整数(int),指示受影响的行数(即更新计数)。
public static int executeUpdate(String sql,Object... params)
{
Connection con = getConnection();
PreparedStatement pstmt = null;
int j = 0;
try {
// 表示预编译的SQL语句的对象,其中SQL语句被预编译并且存储在PreparedStatement对象中。然后可以使用此对象高效地多次执行该语句。
pstmt = con.prepareStatement(sql);
//如果有占位符 进行填充
if (params != null)
{
for (int i = 0;i < params.length;i++)
{
//索引从1开始 i+ 1 数组的内容依旧从0开始添加
pstmt.setObject(i + 1,params[i]);
}
}
//执行sql语句
j = pstmt.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return j;
}
//通一的查询
// 这个方法被用来执行 SELECT 语句,但也只能执行查询语句,执行后返回代表查询结果的ResultSet对象。
public static <T> List<T> executeQuery(String sql, Class<T> clz, Object... params)
{
List<T> list = new ArrayList<T>();
Connection con = getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = con.prepareStatement(sql);
if (params != null)
{
for (int i = 0;i < params.length;i++)
{
pstmt.setObject(i + 1,params[i]);
}
}
rs = pstmt.executeQuery();
while (rs.next())
{
T t = clz.newInstance();
Field[] fields = clz.getDeclaredFields();
for (Field field :fields)
{
field.setAccessible(true);
field.set(t,rs.getObject(field.getName()));
}
list.add(t);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return list;
}
public static void close(Connection con, PreparedStatement pstmt)
{
try {
if (con != null)
{
con.close();
}
if (pstmt != null)
{
pstmt.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void close(Connection con, PreparedStatement pstmt, ResultSet rs)
{
try {
if (rs != null)
{
rs.close();
}
close(con,pstmt);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
executeUpdate() 详解
executeUpdate() 这是 PreparedStatement 接口中的方法 executeUpdate(String sql) 这是 PreparedStatement 从父接口 Statement 中继承过来的方法
executeUpdate() 中执行 SQL 语句需要在创建 PerparedStatement 时通过 Connection 的 prepareStatement(String sql) 方法中写出,因为 PerparedStatement 中的 SQL 语句数据库需要进行预编译和缓存,因此要在创建 PerparedStatement 对象时给出 SQL 语句。
而 executeUpdate(String sql) 是 Statement 中的方法,参数中的 SQL 语句只是提交给数据库去执行,并不需要预编译。