Advertisement
Guest User

Untitled

a guest
Oct 16th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. package com.luxoft;
  2.  
  3. import java.io.*;
  4. import java.sql.*;
  5. import java.util.logging.Logger;
  6.  
  7. import static java.lang.System.out;
  8.  
  9. public class ClobLoader {
  10.  
  11. static int calcChars(Reader r) throws SQLException, IOException {
  12. int cnt = 0;
  13. while (-1 != r.read()) cnt++;
  14. r.close();
  15. return cnt;
  16. }
  17.  
  18. static Logger logger = Logger.getLogger("ClobLoader");
  19.  
  20. public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
  21. if (args.length==0){
  22. out.printf("Usage:\n\n");
  23. out.printf(" $ java -jar ClobLoader-x.x.x.jar jdbc-connection-string select-blob-exprnnession file-name [encoding]\n\n");
  24. out.printf(" Examples:\n");
  25. out.printf(" Update the existing CLOB from textfile with utf-8 encoding:\n");
  26. out.printf(" $ java -jar ClobLoader-x.x.x.jar \\ \n");
  27. out.printf(" \"jdbc:oracle:thin:scott/tiger@127.0.0.1:1521/XE\" \\ \n");
  28. out.printf(" \"UPDATE mytable SET clobField=? WHERE id=12345\" \\ \n");
  29. out.printf(" readme.txt \\ \n");
  30. out.printf(" utf-8 \n\n");
  31. out.printf(" Insert a new CLOB:\n");
  32. out.printf(" $ java -jar ClobLoader-x.x.x.jar \\ \n");
  33. out.printf(" \"jdbc:oracle:thin:scott/tiger@127.0.0.1:1521/XE\" \\ \n");
  34. out.printf(" \"INSERT INTO test VALUES (test_seq.nextval,?)\" \\ \n");
  35. out.printf(" readme2.txt \\ \n");
  36. out.printf(" windows-1251\n\n");
  37. out.printf(" To force quickstart you can add -XX:+UseSerialGC option\n\n");
  38. return;
  39. }
  40. String encoding = "windows-1251";
  41. if (args.length>=4){
  42. logger.info("Default encoding has been changed into '"+encoding+"'");
  43. encoding = args[3];
  44. }
  45. logger.info("Begin");
  46. Class.forName("oracle.jdbc.OracleDriver");
  47. logger.info("Get connection...");
  48. Connection conn = DriverManager.getConnection(args[0]);
  49. conn.setAutoCommit(false);
  50. String sql = args[1];
  51. String fileName = args[2];
  52. logger.info("Estimate size in chars...");
  53. // TODO: Redundant step. Should be refactored without double-reading of InputStream
  54. Reader reader = new InputStreamReader(new FileInputStream(fileName), encoding);
  55. int length = calcChars(reader);
  56. logger.info("Prepare statement '"+sql+"'");
  57. PreparedStatement pStmt = conn.prepareStatement(sql);
  58. logger.info("Bind variables...");
  59. reader = new InputStreamReader(new FileInputStream(fileName), encoding);
  60. pStmt.setCharacterStream(1, reader, length);
  61. logger.info("Exec update...");
  62. pStmt.executeUpdate();
  63. pStmt.close();
  64. logger.info("Commit");
  65. conn.commit();
  66. logger.info("Close connection");
  67. conn.close();
  68. logger.info("End");
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement