JDBC 의 일괄 실행
1.데이터베이스 에 큰 파일 가 져 오기
성공 하려 면 먼저 text.txt 파일 을 src 폴 더 아래 에 두 고 MySQL 데이터베이스 에 표 mytext 를 만들어 야 합 니 다.
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import org.junit.Test;
import cn.itcast.e_tool.JDBCUtils;
public class Demo {
@Test
// mysql
// PrepareStatement
public void fun1() throws Exception{
//1
Connection conn = JDBCUtils.getConnection();
//2 sql
String sql = "insert into mytext values(null,?)";
//3 PrepareStatement
PreparedStatement ps = conn.prepareStatement(sql);
//4
// 1:
// 2:
// 3:
File f = new File("src/text.txt");
FileReader reader = new FileReader(f);
ps.setCharacterStream(1, reader, (int)f.length());
//5 sql
int result = ps.executeUpdate();
System.out.println(result);
//6
JDBCUtils.close(conn, ps, null);
}
}
2.데이터베이스 에 그림 가 져 오기
마찬가지 로 먼저 wg.png 의 그림 을 src 디 렉 터 리 에 넣 고 my blob 라 는 시 계 를 만들어 야 하 는데 선생님 께 서 는 자주 사용 하지 않 는 다 고 하 셨 습 니 다.
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import org.junit.Test;
import cn.itcast.e_tool.JDBCUtils;
public class Demo {
@Test
// mysql
// PrepareStatement
public void fun1() throws Exception{
//1
Connection conn = JDBCUtils.getConnection();
//2 sql
String sql = "insert into myblob values(null,?)";
//3 PrepareStatement
PreparedStatement ps = conn.prepareStatement(sql);
//4
// 1:
// 2:
// 3:
File f = new File("src/wg.PNG");
InputStream is = new FileInputStream(f);
ps.setBinaryStream(1, is, (int)f.length());
//5 sql
int result = ps.executeUpdate();
System.out.println(result);
//6
JDBCUtils.close(conn, ps, null);
}
}
3.JDBC 의 대량 실행
먼저 addBatch()문 구 를 통 해 모든 sql 문 구 를 추가 한 다음 에 실행 하면 됩 니 다.
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Arrays;
import org.junit.Test;
import cn.itcast.e_tool.JDBCUtils;
public class Demo {
@Test
//1 Statement sql
public void fun1() throws Exception{
//1
Connection conn = JDBCUtils.getConnection();
//2 Statement
Statement st = conn.createStatement();
//3 sql st
st.addBatch("create table t_stu ( id int primary key auto_increment , name varchar(20) )");
st.addBatch("insert into t_stu values(null,'tom')");
st.addBatch("insert into t_stu values(null,'jerry')");
st.addBatch("insert into t_stu values(null,'jack')");
st.addBatch("insert into t_stu values(null,'rose')");
//4 sql
int[] results = st.executeBatch();
System.out.println(Arrays.toString(results));
//5
JDBCUtils.close(conn, st, null);
}
@Test
//2 PrepareStatement sql
public void fun2() throws Exception{
//1
Connection conn = JDBCUtils.getConnection();
//2 sql
String sql = "insert into t_stu values(null,?)";
//3 PrepareStatement
PreparedStatement ps = conn.prepareStatement(sql);
//4 .
for(int i=0;i<100;i++){
ps.setString(1, " "+i);
ps.addBatch();
}
//5
int[] results =ps.executeBatch();
System.out.println(Arrays.toString(results));
//5
JDBCUtils.close(conn, ps, null);
}
}
JDBC 명령 을 대량으로 실행 할 때 Prepared Statement 명령 을 사용 하 는 것 은 실행 효율 이 비교적 높다.그 는 매개 변 수 를 전달 하 는 방식 이기 때문에 전체 명령 을 전달 하 는 것 보다 매개 변 수 를 전달 하 는 것 이 훨씬 빠르다.