使用JDBC处理大数据
TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB
对于MySQL中的Text类型,可调用如下方法设置
Stringpath = classLoader();
Filef = new File(path);
PreparedStatement.setCharacterStream(index,reader, length);
//注意length长度须设置,并且设置为int型,利用File获取
reader= resultSet.
getCharacterStream(i);
reader= resultSet.getClob(i).getCharacterStream();
strings = resultSet.getString(i);
使用JDBC处理二进制数据
PreparedStatement.
setBinaryStream(i,
inputStream,length);
InputStreamin
= resultSet.getBinaryStream(i);
InputStreamin
= resultSet.getBlob(i).getBinaryStream();
使用JDBC进行批处理
Connectionconn = null;
Statementst =
null;
ResultSetrs =
null;
try {
conn= JdbcUtil.getConnection();
Stringsql1 = "insert into user(name,password,email,birthday)
values('kkk','123','abc@sina.com','1978-08-08')";
Stringsql2 = "update user set password='123456' where id=3";
st = conn.createStatement();
st.addBatch(sql1);
//把SQL语句加入到批命令中
st.addBatch(sql2);
//把SQL语句加入到批命令中
st.executeBatch();
}finally{
JdbcUtil.free(conn,st,
rs);
}
Insert into user(name,password) values(‘aa’,’111’);
Insertinto user(name,password) values(‘bb’,’222’);
Insertinto user(name,password) values(‘cc’,’333’);
Insertinto user(name,password) values(‘dd’,’444’);
conn= JdbcUtil.getConnection();
Stringsql ="insert into user(name,password,email,birthday)values(?,?,?,?)";
st = conn.prepareStatement(sql);
for(inti=0;i<50000;i++){
st.setString(1,"aaa"+
i);
st.setString(2,"123" +
i);
st.setString(3,"aaa"+
i +"@sina.com");
st.setDate(4,newDate(1980, 10, 10));
st.addBatch();
if(i%1000==0){
st.executeBatch();
st.clearBatch();
}
}
st.executeBatch();
获得数据库自动生成的主键
Connectionconn = JdbcUtil.getConnection();
Stringsql ="insert into user(name,password,email,birthday)
values('abc','123','abc@sina.com','1978-08-08')";
PreparedStatementst =conn.
prepareStatement(sql,Statement.RETURN_GENERATED_KEYS );
st.executeUpdate();
ResultSetrs =
st.getGeneratedKeys();
//得到插入行的主键
if(rs.next())
System.out.println(rs.getObject(1));
JDBC调用存储过程
得到CallableStatement
,并调用存储过程:
CallableStatementcStmt =
conn.prepareCall("{calldemoSp(?,?)}");
cStmt.registerOutParameter(2,
Types.VARCHAR);
cStmt.setString(1,"abcdefg");
cStmt.execute();
System.out.println(cStmt.getString(2));
事务
updatefrom account set money=money+100 where name=‘b’;
update from account set money=money-100 where name=‘a’;
开启事务
使用事务
演示银行转帐案例
update from account setmoney=money-100 where name=‘a’;
update from account set money=money+100 wherename=‘b’;
conn.setSavepoint();
//回滚后必须要提交
事务的特性(ACID)
原子性是指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生。
事务必须使数据库从一个一致性状态变换到另外一个一致性状态。
事务的隔离性是多个用户并发访问数据库时,数据库为每一个用户开启的事务,不能被其他事务的操作数据所干扰,多个并发事务之间要相互隔离。
持久性是指一个事务一旦被提交,它对数据库中数据的改变就是永久性的,接下来即使数据库发生故障也不应该对其有任何影响。
事务的隔离级别
事务的隔离性
这是非常危险的,假设A向B转帐100元,对应sql语句如下所示
1.update account setmoney=money+100 while name=‘b’;
2.updateaccount set money=money-100 while name=‘a’;
当第1条sql执行完,第2条还没执行(A未提交时),如果此时B查询自己的帐户,就会发现自己多了100元钱。如果A等B走后再回滚,B就会损失100元。
例如银行想查询A帐户余额,第一次查询A帐户为200元,此时A向帐户内存了100元并提交了,银行接着又进行了一次查询,此时A帐户为300元了。银行两次查询不一致,可能就会很困惑,不知道哪次查询是准的。
设置事务隔离级别
查询当前事务隔离级别
JDBC
level)用于设置隔离级别