实际项目中,经常需要对数据进行批量处理。[@more@]
下面这个例子,是从tab分隔的csv文件里读取数据,把数据写入hostname上的DBNAME的USER_MASTER里。import java.io.*;import java.sql.*;import oracle.jdbc.*;public class Batch2 {private static String sDBConnect = "jdbc:oracle:thin:@hostname:1521:DBNAME";private static String sDBUser = "user";private static String sDBPassword = "password";private static String sPath = "C:Batch";private static String sFileName = "csvfile.txt";//file name divide by tprivate static int iMax=10000;//max countprivate static int iCol=4;//column numberpublic static void main(String[] args) { do_Batch_User_Master(); System.out.println("Over."); } public static void do_Batch_User_Master(){ String [][]values=new String[iMax][iCol]; int i=do_readfile(values); do_Batch_User_Master(values,i); } public static int do_readfile(String [][]values){ int i = 0; String value[]; try { FileReader in = new FileReader(sPath+sFileName); BufferedReader br = new BufferedReader(in); String line; while ((line = br.readLine()) != null) { value = (line+"t0").split("t"); if (do_check_have(value,values,i)) continue;values[i] = value; i++; } br.close(); in.close(); } catch (IOException e) { System.out.println(e); }return i; } public static boolean do_check_have(String []value,String [][]values,int icnt){ boolean bret = false; for ( int i=0;i < icnt ;i++ ) { if(values[i][0].equals(value[0])){//double checked bret = true; break; } } return bret; } public static void do_Batch_User_Master(String [][]values,int icnt){ Connection con = null; PreparedStatement pstm = null; Statementstm = null; ResultSetrst = null; StringBuffer sb =null; try { DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); con = DriverManager.getConnection(sDBConnect,sDBUser,sDBPassword); // disable autocommit con.setAutoCommit(false); sb = new StringBuffer(); sb.append("INSERT INTO USER_MASTER (USER_ID,PASSWORD,NAME,DEL_FLG) "); sb.append("VALUES (?,?,?,?)"); // PrepareStatement pstm = con.prepareStatement( sb.toString()); for( int i=0;i
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341