여러 트랜잭션을 처리하는 경우 addBatch, executeBatch를 이용하는 것이
효율적입니다. 참고하시구요,,, JDBC 강좌에 보시면 다른 addBatch 예제도 있으니 참고 하세요~
import java.sql.*;
public class testBatch
{
public static void main(String[] args)
{
DB db = new DB();
Connection conn=db.dbConnect(
"jdbc:jtds:sqlserver://localhost:1433/tempdb","sa","");
db.performBatch(conn);
}
}
class DB
{
public DB() {}
public Connection dbConnect(String db_connect_string,
String db_userid, String db_password)
{
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection(
db_connect_string, db_userid,db_password);
System.out.println("connected");
return conn;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public void performBatch(Connection conn)
{
String query;
Statement stmt;
try
{
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.addBatch("INSERT INTO customer " +
"VALUES('name2', 'temp2',27)");
stmt.addBatch("INSERT INTO customer " +
"VALUES('name3', 'temp3',28)");
stmt.addBatch("INSERT INTO customer " +
"VALUES('name4','temp4',29)");
int [] updateCounts = stmt.executeBatch();
conn.commit();
conn.setAutoCommit(true);
stmt.close();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
댓글 없음:
댓글 쓰기