Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.luxoft;
- import java.io.*;
- import java.sql.*;
- import java.util.logging.Logger;
- import static java.lang.System.out;
- public class ClobLoader {
- static int calcChars(Reader r) throws SQLException, IOException {
- int cnt = 0;
- while (-1 != r.read()) cnt++;
- r.close();
- return cnt;
- }
- static Logger logger = Logger.getLogger("ClobLoader");
- public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
- if (args.length==0){
- out.printf("Usage:\n\n");
- out.printf(" $ java -jar ClobLoader-x.x.x.jar jdbc-connection-string select-blob-exprnnession file-name [encoding]\n\n");
- out.printf(" Examples:\n");
- out.printf(" Update the existing CLOB from textfile with utf-8 encoding:\n");
- out.printf(" $ java -jar ClobLoader-x.x.x.jar \\ \n");
- out.printf(" \"jdbc:oracle:thin:scott/tiger@127.0.0.1:1521/XE\" \\ \n");
- out.printf(" \"UPDATE mytable SET clobField=? WHERE id=12345\" \\ \n");
- out.printf(" readme.txt \\ \n");
- out.printf(" utf-8 \n\n");
- out.printf(" Insert a new CLOB:\n");
- out.printf(" $ java -jar ClobLoader-x.x.x.jar \\ \n");
- out.printf(" \"jdbc:oracle:thin:scott/tiger@127.0.0.1:1521/XE\" \\ \n");
- out.printf(" \"INSERT INTO test VALUES (test_seq.nextval,?)\" \\ \n");
- out.printf(" readme2.txt \\ \n");
- out.printf(" windows-1251\n\n");
- out.printf(" To force quickstart you can add -XX:+UseSerialGC option\n\n");
- return;
- }
- String encoding = "windows-1251";
- if (args.length>=4){
- logger.info("Default encoding has been changed into '"+encoding+"'");
- encoding = args[3];
- }
- logger.info("Begin");
- Class.forName("oracle.jdbc.OracleDriver");
- logger.info("Get connection...");
- Connection conn = DriverManager.getConnection(args[0]);
- conn.setAutoCommit(false);
- String sql = args[1];
- String fileName = args[2];
- logger.info("Estimate size in chars...");
- // TODO: Redundant step. Should be refactored without double-reading of InputStream
- Reader reader = new InputStreamReader(new FileInputStream(fileName), encoding);
- int length = calcChars(reader);
- logger.info("Prepare statement '"+sql+"'");
- PreparedStatement pStmt = conn.prepareStatement(sql);
- logger.info("Bind variables...");
- reader = new InputStreamReader(new FileInputStream(fileName), encoding);
- pStmt.setCharacterStream(1, reader, length);
- logger.info("Exec update...");
- pStmt.executeUpdate();
- pStmt.close();
- logger.info("Commit");
- conn.commit();
- logger.info("Close connection");
- conn.close();
- logger.info("End");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement