1、操作BLOB类型字段
- 插入操作
public class BlobTest {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtils.getConnection();
String sql="insert into customers(name,email,birth,photo)values(?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setObject(1,"dcd");
ps.setObject(2,"vip@qq.com");
ps.setObject(3,"2001-06-14");
FileInputStream fis = new FileInputStream(new File("JDBC/lib/p.png"));
ps.setBlob(4,fis);
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn,ps);
}
}
}
- 查询操作
@Test
public void query(){
Connection conn = null;
PreparedStatement ps = null;
ResultSet resultSet=null;
FileOutputStream fos=null;
InputStream is=null;
try {
conn = JDBCUtils.getConnection();
String sql="select id,name,email,birth,photo from customers where id=?";
ps = conn.prepareStatement(sql);
ps.setObject(1,20);
resultSet = ps.executeQuery();
if(resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String email = resultSet.getString("email");
Date birth = resultSet.getDate("birth");
Customer customer = new Customer(id, name, email, birth);
System.out.println(customer);
Blob photo = resultSet.getBlob("photo");
is = photo.getBinaryStream();
fos = new FileOutputStream("lib/p(1).png");
byte[] cbuf = new byte[1024];
int len;
while((len=is.read(cbuf))!=-1){
fos.write(cbuf,0,len);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn,ps,resultSet);
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、事务
2.1、事务的代码实现
事务的ACID属性 1、原子性:事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生 2、一致性:事务必须使数据库从一个一致性状态变为另一个一致性状态 3、隔离性:一个事务的执行不能被其他事务干扰 4、持久性:一个事务一旦被提交,它对数据库中数据的改变就是永久性的
public class ConnectionTest {
@Test
public void fun(){
Connection conn=null;
try {
conn = JDBCUtils.getConnection();
conn.setAutoCommit(false);
String sql1="update user_table set balance=balance+100 where user=?";
update(conn,sql1,"AA");
System.out.println(10/0);
String sql2="update user_table set balance=balance-100 where user=?";
update(conn,sql2,"BB");
System.out.println("转账成功");
conn.commit();
} catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
} finally {
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void update(Connection conn,String sql,Object ...args){
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
for(int i=0;i<args.length;++i){
ps.setObject(i+1,args[i]);
}
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
}
}
}
}
2.2、事务并发引发的问题
有两个事务A、B。 1、脏读:对同一个数据来说,A修改了它但没有提交,B读取的时候读到了 2、不可重复读:正常情况是可以重复读的,对于同一个数据来说,B已经读取了数据,A修改了它并且提交,此时B的事务并没有结束,再次读的时候发现发生了变化。 3、幻读:事务A读取了一个字段,然后B在该表插入了一些新的行,之后如果A再读同一个表会多出几行
public class ConnectionTest {
@Test
public void update_fun(){
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
conn.setAutoCommit(false);
String sql="update user_table set balance=? where user=?";
update(conn,sql,5000,"CC");
Thread.sleep(1500);
System.out.println("修改完成");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Test
public void query_fun(){
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
System.out.println(conn.getTransactionIsolation());
conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
conn.setAutoCommit(false);
String sql="select user,password,balance from user_table where user=?";
for (User cc : getForList(conn, User.class, sql, "CC")) {
System.out.println(cc);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void update(Connection conn,String sql,Object ...args){
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
for(int i=0;i<args.length;++i){
ps.setObject(i+1,args[i]);
}
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
}
}
}
public <T>List<T> getForList(Connection conn,Class<T> clazz,String sql,Object ...args){
PreparedStatement ps=null;
ResultSet resultSet=null;
try{
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement(sql);
for(int i=0;i<args.length;++i){
ps.setObject(i+1,args[i]);
}
resultSet = ps.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
ArrayList<T> list = new ArrayList<>();
while(resultSet.next()){
T t = clazz.newInstance();
for(int i=0;i<columnCount;++i){
Object columnValue = resultSet.getObject(i + 1);
String columnLabel = metaData.getColumnLabel(i+1);
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t,columnValue);
}
list.add(t);
}
return list;
}catch (Exception e){
System.out.println(e.getMessage());
}finally {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
3、Dao模式
Database access object 数据库访问对象 DAO包下需要有下列内容 BaseDAO:是用来存放基本的用java实现的操作代码 CustomerDao和接口实现类:是用来规范格式写的接口和实现类,每个对应的类都有一对。
BaseDAO
public abstract class BaseDAO {
public static void update(Connection conn, String sql, Object ...args){
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
for(int i=0;i<args.length;++i){
ps.setObject(i+1,args[i]);
}
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
}
}
}
public <T> T getInstance(Connection conn, Class<T> clazz, String sql, Object ...args){
PreparedStatement ps=null;
ResultSet resultSet=null;
try{
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement(sql);
for(int i=0;i<args.length;++i){
ps.setObject(i+1,args[i]);
}
resultSet = ps.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
ArrayList<T> list = new ArrayList<>();
if(resultSet.next()){
T t = clazz.newInstance();
for(int i=0;i<columnCount;++i){
Object columnValue = resultSet.getObject(i + 1);
String columnLabel = metaData.getColumnLabel(i+1);
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t,columnValue);
}
return t;
}
}catch (Exception e){
System.out.println(e.getMessage());
}finally {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
public <T> List<T> getForList(Connection conn, Class<T> clazz, String sql, Object ...args){
PreparedStatement ps=null;
ResultSet resultSet=null;
try{
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement(sql);
for(int i=0;i<args.length;++i){
ps.setObject(i+1,args[i]);
}
resultSet = ps.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
ArrayList<T> list = new ArrayList<>();
while(resultSet.next()){
T t = clazz.newInstance();
for(int i=0;i<columnCount;++i){
Object columnValue = resultSet.getObject(i + 1);
String columnLabel = metaData.getColumnLabel(i+1);
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t,columnValue);
}
list.add(t);
}
return list;
}catch (Exception e){
System.out.println(e.getMessage());
}finally {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
public <E> E getValue(Connection conn,String sql,Object ...args){
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
ps = conn.prepareStatement(sql);
for(int i=0;i<args.length;++i){
ps.setObject(i+1,args[i]);
}
resultSet = ps.executeQuery();
if(resultSet.next()){
return (E) resultSet.getObject(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn,ps,resultSet);
}
return null;
}
}
CustomerDao
public interface CustomerDao {
void insert(Connection conn, Customer cust);
void deleteById(Connection conn,int id);
void update(Connection conn,Customer cust);
Customer getCustomerById(Connection conn,int id);
List<Customer> getAll(Connection conn);
long getCount(Connection conn);
Date getMaxBirth(Connection conn);
}
CustomerDaoImpl
public class CustomerDaoImpl extends BaseDAO implements CustomerDao{
@Override
public void insert(Connection conn, Customer cust) {
String sql="insert into customers(name,email,birth)values(?,?,?)";
update(conn,sql,cust.getName(),cust.getEmail(),cust.getBirth());
}
@Override
public void deleteById(Connection conn, int id) {
String sql="delete from customers where id=?";
update(conn,sql,id);
}
@Override
public void update(Connection conn, Customer cust) {
String sql="update customers set name=?,email=?,birth=? where id=?";
update(conn,sql,cust.getName(),cust.getEmail(),cust.getBirth(),cust.getId());
}
@Override
public Customer getCustomerById(Connection conn, int id) {
String sql="select id,name,email,birth from customers where id=?";
Customer instance = getInstance(conn, Customer.class, sql, id);
return instance;
}
@Override
public List<Customer> getAll(Connection conn) {
String sql="select id,name,email,birth from customers";
List<Customer> list = getForList(conn, Customer.class, sql);
return list;
}
@Override
public long getCount(Connection conn) {
String sql="select count(*) from customers";
long value = (long)getValue(conn, sql);
return value;
}
@Override
public Date getMaxBirth(Connection conn) {
String sql="select max(birth) from customers";
return getValue(conn, sql);
}
}
Junit文件的生成方式
- 选中需要测试的类,右键选择go to
- 选择test,然后一步步向下就行了
- 写入测试代码即可
|