1.增删改通用函数
public void update(String sql, Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = DBUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
preparedStatement.execute();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeResource(connection, preparedStatement);
}
}
2.针对于某个表的查找通用方法
import org.junit.Test;
import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.*;
import java.sql.Connection;
public class SelectTest {
@Test
public void testQueryForOrder() {
String querySql = "select order_id orderId,order_name orderName,order_date orderDate from `order` where order_id = ?";
Order select = select(querySql, 1);
System.out.println(select.toString());
}
public Order select(String sql, Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = DBUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
if (resultSet.next()) {
Order order = new Order();
for (int i = 0; i < columnCount; i++) {
String columnLabel = metaData.getColumnLabel(i + 1);
Object object = resultSet.getObject(i + 1);
Field declaredField = Order.class.getDeclaredField(columnLabel);
declaredField.setAccessible(true);
declaredField.set(order, object);
}
return order;
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} finally {
DBUtil.closeResource(connection, preparedStatement, resultSet);
}
return null;
}
}
3.针对各种表的查找通用方法
import org.junit.Test;
import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class SelectTest {
@Test
public void testQueryForOrder() {
String querySql = "select order_id orderId,order_name orderName,order_date orderDate from `order` where order_id < ?";
List<Order> orders = selectAllTableList(Order.class, querySql, 4);
orders.forEach(System.out::println);
}
public Order select(String sql, Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = DBUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
if (resultSet.next()) {
Order order = new Order();
for (int i = 0; i < columnCount; i++) {
String columnLabel = metaData.getColumnLabel(i + 1);
Object object = resultSet.getObject(i + 1);
Field declaredField = Order.class.getDeclaredField(columnLabel);
declaredField.setAccessible(true);
declaredField.set(order, object);
}
return order;
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} finally {
DBUtil.closeResource(connection, preparedStatement, resultSet);
}
return null;
}
public <T> List<T> selectAllTableList(Class<T> t, String sql, Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = DBUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
List<T> ts = new ArrayList<T>();
while (resultSet.next()) {
T t1 = t.newInstance();
for (int i = 0; i < columnCount; i++) {
Object object = resultSet.getObject(i + 1);
String columnLabel = metaData.getColumnLabel(i + 1);
Field declaredField = t.getDeclaredField(columnLabel);
declaredField.setAccessible(true);
declaredField.set(t1, object);
}
ts.add(t1);
}
return ts;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} finally {
DBUtil.closeResource(connection, preparedStatement, resultSet);
}
return null;
}
public <T> T selectAllTable(Class<T> t, String sql, Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = DBUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
if (resultSet.next()) {
T t1 = t.newInstance();
for (int i = 0; i < columnCount; i++) {
Object object = resultSet.getObject(i + 1);
String columnLabel = metaData.getColumnLabel(i + 1);
Field declaredField = t.getDeclaredField(columnLabel);
declaredField.setAccessible(true);
declaredField.set(t1, object);
}
return t1;
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} finally {
DBUtil.closeResource(connection, preparedStatement, resultSet);
}
return null;
}
}
4.blob类型的添加与查询
package com.ym.study;
import org.junit.Test;
import java.io.*;
import java.sql.*;
public class BlobTest {
@Test
public void testBlob() {
}
@Test
public void insertBlob() throws SQLException, IOException, ClassNotFoundException {
Connection connection = DBUtil.getConnection();
String insertSql = "insert into customers(name,email,birth,photo) values(?,?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertSql);
File blobFile = new File("x.jpg");
FileInputStream fileInputStream = new FileInputStream(blobFile);
preparedStatement.setObject(1, "李四");
preparedStatement.setObject(2, "lisi@163.com");
preparedStatement.setObject(3, "1992-09-08");
preparedStatement.setBlob(4, fileInputStream);
preparedStatement.execute();
DBUtil.closeResource(connection, preparedStatement);
}
@Test
public void queryBlob() throws SQLException, IOException, ClassNotFoundException {
Connection connection = DBUtil.getConnection();
String querySql = "select photo from customers where id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(querySql);
preparedStatement.setObject(1, 22);
ResultSet resultSet = preparedStatement.executeQuery();
FileOutputStream fileOutputStream = null;
if (resultSet.next()) {
Blob blob = resultSet.getBlob(1);
InputStream binaryStream = blob.getBinaryStream();
fileOutputStream = new FileOutputStream("z.jpg");
byte[] bytes = new byte[1024];
int len = 0;
while ((len = binaryStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, len);
}
}
fileOutputStream.close();
DBUtil.closeResource(connection, preparedStatement, resultSet);
}
}
5.事务例子
package com.ym.study;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Transaction {
@Test
public void testTransaction() {
Connection connection = null;
try {
connection = DBUtil.getConnection();
String AASql = "update user_table set balance = balance-100 where user = ?";
System.out.println(connection.getAutoCommit() + "1");
connection.setAutoCommit(false);
System.out.println(connection.getAutoCommit() + "2");
updateOfTransaction(connection, AASql, "AA");
System.out.println(10 / 0);
System.out.println(connection.getAutoCommit() + "3");
String BBSql = "update user_table set balance = balance+100 where user = ?";
updateOfTransaction(connection, BBSql, "BB");
System.out.println(connection.getAutoCommit() + "4");
System.out.println("交易成功");
} catch (Exception e) {
try {
System.out.println("进行回滚");
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
} finally {
try {
connection.setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
}
DBUtil.closeResource(connection, null);
}
}
public void updateOfTransaction(Connection conn, String sql, Object... args) {
PreparedStatement preparedStatement = null;
try {
preparedStatement = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
preparedStatement.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeResource(null, preparedStatement);
}
}
}
6.事务的ACID属性
1.原子性(Atimicity) 原子性是指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生。 2.一致性(Consistency) 事务必须使数据库从一个一致性状态变换到另外一个一致性状态。 3.隔离性(Isolation) 事务的隔离性是指一个事务的执行不能被其他事务干扰,即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能相互干扰。 4.持久性(Durability) 持久性是指一个事务一旦被提交,它对数据库中数据的改变就是永久性的,接下来的其他操作和数据库故障不应该对其有任何影响。
7.数据库隔离性中常出现的问题
Read_uncommited 这个隔离级别解决不了脏读 1.脏读 脏读是指T1事务在读取数据时,读取到了T2事务修改了的但是未提交的数据。 Read_commited 这个隔离级别解决了脏读 2.不可重复读 不可重复读是指T1事务在读取数据时,读取到了T2事务修改了的并且提交了的数据 Repeated_Read 这个隔离级别可以解决脏读和不可重复读问题 3.幻读 幻读是指T1事务在读取数据时,读取到了T2事务添加的几条数据 Serializable 这个隔离基本可以解决脏读、不可重复读、幻读问题,但是并行化就很差了 oracle支持 Read_commited 和 Serializable 一般项目应用中,脏读是不可接受的,其他两个是可以接受的
|