在url中加上:
url=jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8
/**
* @ClassName : JDBCUtils
* @Description : 操作数据库的工具类
* @Author : Yemulin
* @Date : 2020/06/02 19:22
* @Version : 1.0
*/
public class JDBCUtils {
/**
* @Description : 获取数据库的连接
* @Param : []
* @author : Yemulin
* @date : 2020/6/2 19:32
* @return : java.sql.Connection
* @throws :
*/
public static Connection getConnection() throws Exception {
//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("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
//2.加载驱动
Class.forName(driverClass);
//3.获取连接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
return connection;
}
/**
* @Description : 关闭连接和Statement的操作
* @Param : [connection, ps]
* @author : Yemulin
* @date : 2020/6/3 17:26
* @return : void
* @throws :
*/
public static void closeResource(Connection connection, Statement ps){
try {
if (ps != null )
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @Description : 关闭资源操作
* @Param : [connection, ps, rs]
* @author : Yemulin
* @date : 2020/6/7 16:13
* @return : void
* @throws :
*/
public static void closeResource(Connection connection, Statement ps, ResultSet rs){
try {
if (ps != null )
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public class PreparedStatementUpdateTest {
@Test
public void testConmmonUpdate(){
// String sql = "delete from customers where id = ?";
// update(sql,3);
String sql = "update `order` set order_name = ? where order_id = ?";
update(sql,"DD","2");
}
//通用的增删改操作
public void update(String sql, Object ...args) {//sql中占位符的个数与可变形参的长度相同
Connection conn = null;
PreparedStatement ps = null;
try {
//1.获取数据库的连接
conn = JDBCUtils.getConnection();
//2.预编译sql语句,返回PrepareStatement的实例
ps = conn.prepareStatement(sql);
//3.填充占位符
for (int i = 0 ; i < args.length ; i++){
ps.setObject(i + 1,args[i]);//小心参数声明错误
}
//4.执行
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
//5.资源的关闭
JDBCUtils.closeResource(conn,ps);
}
}
@Test
public void testQueryForCustomers() throws Exception {
String sql = "select id,name,birth,email from customers where id = ?";
Customer customer = queryForCustomers(sql, 13);
System.out.println(customer);
sql = "select name,email from customers where name = ?";
Customer customer1 = queryForCustomers(sql, "周杰伦");
System.out.println(customer1);
}
/**
* @Description : 针对于customers表的通用的查询操作
* @Param : []
* @author : Yemulin
* @date : 2020/6/7 16:26
* @return : void
* @throws :
*/
public Customer queryForCustomers(String sql,Object ...args) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++){
ps.setObject(i + 1 ,args[i]);
}
rs = ps.executeQuery();
//获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsMetaData = rs.getMetaData();
//通过ResultSetMetaData获取结果集中的列数
int columnCount = rsMetaData.getColumnCount();
if (rs.next()){
Customer cust = new Customer();
//处理结果集一行数据中的每一个列
for (int i = 0 ; i < columnCount ; i++){
//获取列值
Object columnValue = rs.getObject(i + 1);
//获取每个列的列名
String columnName = rsMetaData.getColumnName(i + 1);
//给cust对象指定的columnName属性,赋值为columnValue:通过反射
Field field = Customer.class.getDeclaredField(columnName);
field.setAccessible(true);
field.set(cust, columnValue);
}
return cust;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps, rs);
}
return null;
}
/**
* @ClassName : OrderForQuery
* @Description : 针对于Order表的通用的查询操作
* @Author : Yemulin
* @Date : 2020/06/13 15:31
* @Version : 1.0
*/
public class OrderForQuery {
/*
针对于表的字段名与类的属性名不相同的情况:
1.必须声明sql时,使用类的属性名来命名字段的别名
2.使用ResultSetMetaData时,需要使用getColumnLabel()来替换getColumnName()
获取列的别名。
说明:如果sql中没有给字段起别名,getColumnLabel()获取的就是列名
*/
@Test
public void testOrderForQuery(){
String sql = "select order_id orderId,order_name orderName,order_date orderDate from `order` where order_id = ?";
Order order = orderForQuery(sql, 1);
System.out.println(order);
}
/**
* @Description : 通用的针对于Order表的查询操作
* @Param : []
* @author : Yemulin
* @date : 2020/6/25 14:46
* @return : jdbc3bean.Order
* @throws :
*/
public Order orderForQuery(String sql, Object...args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++){
ps.setObject(i + 1, args[i]);
}
//执行,获取结果集
rs = ps.executeQuery();
//获取结果集的元数据
ResultSetMetaData rsmd = rs.getMetaData();
//获取列数
int columnCount = rsmd.getColumnCount();
if (rs.next()){
Order order = new Order();
for (int i = 0; i < columnCount; i++){
//获取每个列的列值:通过ResultSet
Object columnValue = rs.getObject(i + 1);
//通过ResultSetMetaData
//获取每个列的列名:getColumnName() --不推荐
//获取每个列的别名:getColumnLabel()
// String columnName = rsmd.getColumnName(i + 1);
String columnLabel = rsmd.getColumnLabel(i + 1);
//通过反射,将对象指定名columnName的属性赋值为指定的值columnValue
Field field = Order.class.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(order, columnValue);
}
return order;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps, rs);
}
return null;
}
}