1.JDBC进行批处理:为了提高sql语句发送到数据库的效率,运用批处理建立sql缓存区,一次发送多条sql到数据库
成员方法:void addBatch(String sql) 添加到sql缓存区(暂时不发送)
int[] executeBatch()执行批处理命令。发送所有缓存区的sql
void clearBatch() 清空sql缓存区
用properties集合创建增强jdbc工具类
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class EXJDBCUtil {
private static String url=null;
private static String user=null;
private static String password=null;
private static String classname=null;
static {
try {
Properties prop=new Properties();
prop.load(new FileInputStream("db.properties"));
url=prop.getProperty("url");
user=prop.getProperty("user");
password=prop.getProperty("password");
classname=prop.getProperty("classname");
Class.forName(classname);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static Connection getconn(){
try {
Connection conn=DriverManager.getConnection(url, user, password);
return conn;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw new RuntimeException();
}
}
public static void close(Connection conn,Statement stmt,ResultSet rs){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
不用批处理插入数据:
private static void teststatement() {
// TODO Auto-generated method stub
Connection conn=null;
Statement stmt=null;
try {
conn=EXJDBCUtil.getconn();
stmt=conn.createStatement();
for (int i = 0; i < 2000; i++) {
String sql="INSERT INTO stu VALUES ("+i+",'a');";
stmt.execute(sql);
}
} catch (Exception e) {
// TODO: handle exception
}finally{
EXJDBCUtil.close(conn, stmt, null);
}
使用批处理插入数据:
private static void teststatementbatch() {
// TODO Auto-generated method stub
Connection conn=null;
Statement stmt=null;
try {
conn=EXJDBCUtil.getconn();
stmt=conn.createStatement();
for (int i = 0; i < 2000; i++) {
String sql="insert into stu values("+i+",'刘德华');";
stmt.addBatch(sql);
if(i%20==0){
stmt.executeBatch();
stmt.clearBatch();
}
}
} catch (Exception e) {
// TODO: handle exception
}finally{
EXJDBCUtil.close(conn, stmt, null);
}
}
stmt = conn.prepareStatement(deptSql, Statement.RETURN_GENERATED_KEYS);
import java.io.FileInputStream;
public class BlobDemo {
public static void main(String[] args) {
write();//向数据库写入文件
read();//从数据库读取文件
}
private static void read() {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try {
conn=EXJDBCUtil.getconn();
String sql="select * from stu where id=?;";//创建sql预编译语句
stmt=conn.prepareStatement(sql);//进行sql预编译
stmt.setInt(1, 1);//第一个是语句中问号的位置,第二个是对问号赋的值
rs=stmt.executeQuery();
while(rs.next()){//创建io流对象,循环读取所得文件数据
InputStream is=rs.getBinaryStream(2);
FileOutputStream fos=new FileOutputStream("b.jpg");
byte[] chs=new byte[1024];
int len;
while((len=is.read(chs))!=-1){
fos.write(chs, 0, len);
}
fos.close();//关流
is.close();
}
} catch (Exception e) {
// TODO: handle exception
}finally{
EXJDBCUtil.close(conn, stmt, rs);
}
}
private static void write() {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement stmt=null;
try {
conn=EXJDBCUtil.getconn();
String sql="insert into stu values(?,?);";
stmt=conn.prepareStatement(sql);
stmt.setInt(1, 1);
//选择输入流文件发送到数据库
stmt.setBlob(2, new FileInputStream("beyonetta.jpg"));
int count=stmt.executeUpdate();
System.out.println(count);
} catch (Exception e) {
// TODO: handle exception
}finally{
EXJDBCUtil.close(conn, stmt, null);
}
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.edu_01.EXJDBCUtil;
//模拟转账操作,一人扣钱,一人加钱,同时成功或失败
public class trabsation {
public static void main(String[] args) {
Connection conn=null;
PreparedStatement stmt=null;
String sql1="update account set balance=balance-2000 where name='james';";
String sql2="update account set balance=balance+2000 where name='weide';";
try {
conn=EXJDBCUtil.getconn();
conn.setAutoCommit(false);//关闭自动提交
stmt=conn.prepareStatement(sql1);
stmt.executeUpdate();//发送语句1
stmt=conn.prepareStatement(sql2);
stmt.executeUpdate();//发送语句2
conn.commit();//提交事务
} catch (Exception e) {
// TODO: handle exception
try {
conn.rollback();//若发生异常进行回滚
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally{//释放资源
EXJDBCUtil.close(conn, stmt, null);
}
}
}