Advertisement
Guest User

Untitled

a guest
Dec 29th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. package year2016.tax8949.database2;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7. import java.util.List;
  8. import java.util.Properties;
  9. import java.util.logging.Logger;
  10.  
  11. public class Form89492016TableInserter {
  12.  
  13. private static final Logger log
  14. = Logger.getLogger( "Form89492016TableInserter" );
  15.  
  16. protected static final String USERNAME = "xxxx";
  17.  
  18. protected static final String PASSWORD = "xxxxx";
  19.  
  20. protected static final String INSTANCE_CONNECTION_NAME
  21. = "xxxx:us-central1:xxxxx";
  22.  
  23. protected static final String DB_NAME = "xxxxxx";
  24.  
  25. protected static final String IP_ADDRESS = "xxx.1xx.2xx.4x";
  26.  
  27. protected static final String SQL_INSERT
  28. = "INSERT into Forms1099B ( orderNumber,acctId,qty,secDesc,dateAcq,dateSold,salesPrice,cost,basisAdj,washAdj,nomineeAdj,otherAdj,term,basisRep,rep1099B,tranType,dateAcqVar,covered,symbol,expired ) VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )";
  29.  
  30.  
  31. // Whether connecting from Google App Engine
  32. private boolean fromGAE = false;
  33.  
  34. // JDBC connection
  35. private Connection conn = null;
  36.  
  37. public Form89492016TableInserter( boolean fromGAE ) {
  38. this.fromGAE = fromGAE;
  39. }
  40.  
  41. public void insertBatch( List<Forms1099BRecordBean> records ) {
  42.  
  43. int batchSize = 500;
  44.  
  45. insertBatchUsingSize( records, batchSize );
  46. }
  47.  
  48. public void insertBatchUsingSize( List<Forms1099BRecordBean> records,
  49. int batchSize ) {
  50.  
  51. try {
  52.  
  53. initializeConnection( );
  54.  
  55. doInsertions( records, batchSize );
  56.  
  57. closeConnection( );
  58.  
  59. }
  60. catch( SQLException e ) {
  61. log.severe( e.getMessage( ) );
  62. }
  63.  
  64. }
  65.  
  66. public void closeConnection( ) throws SQLException {
  67.  
  68. if ( conn != null ) {
  69. conn.close( );
  70. }
  71.  
  72. }
  73.  
  74. public void initializeConnection( ) throws SQLException {
  75.  
  76. String driverName
  77. = ( fromGAE ) ? "com.mysql.jdbc.GoogleDriver" :
  78. "com.mysql.jdbc.Driver";
  79.  
  80. try {
  81. Class.forName( driverName );
  82. }
  83. catch( ClassNotFoundException e ) {
  84. log.severe( e.getMessage( ) );
  85. return;
  86. }
  87.  
  88. if ( fromGAE ) {
  89.  
  90. String connectionString
  91. = String.format(
  92. "jdbc:google:mysql://%s/%s?user=root&password=%s",
  93. INSTANCE_CONNECTION_NAME,
  94. DB_NAME,
  95. PASSWORD );
  96.  
  97. conn = DriverManager.getConnection( connectionString );
  98.  
  99. }
  100. else {
  101.  
  102. String url = String.format( "jdbc:mysql://%s:3306/%s",
  103. IP_ADDRESS,
  104. DB_NAME );
  105.  
  106. Properties props = new Properties();
  107. props.setProperty( "user", USERNAME );
  108. props.setProperty( "password", PASSWORD );
  109. props.setProperty( "rewriteBatchedStatements", "true" );
  110.  
  111. conn = DriverManager.getConnection( url, props );
  112.  
  113. }
  114.  
  115. }
  116.  
  117. private void doInsertions( List<Forms1099BRecordBean> records,
  118. int batchSize ) throws SQLException {
  119.  
  120. try ( PreparedStatement stmt = conn.prepareStatement( SQL_INSERT ) ) {
  121.  
  122. for (int i = 0; i < records.size( ); i++) {
  123.  
  124. Forms1099BRecordBean item = records.get( i );
  125.  
  126. stmt.setString( 1, item.getOrderNumber() );
  127. stmt.setString( 2, item.getAcctId() );
  128. stmt.setString( 3, item.getQty() );
  129. stmt.setString( 4, item.getSecDesc() );
  130. stmt.setDate( 5, item.getDateAcq() );
  131. stmt.setDate( 6, item.getDateSold() );
  132. stmt.setBigDecimal( 7, item.getSalesPrice() );
  133. stmt.setBigDecimal( 8, item.getCost() );
  134. stmt.setBigDecimal( 9, item.getBasisAdj() );
  135. stmt.setBigDecimal( 10, item.getWashAdj() );
  136. stmt.setBigDecimal( 11, item.getNomineeAdj() );
  137. stmt.setBigDecimal( 12, item.getOtherAdj() );
  138. stmt.setString( 13, item.getTerm() );
  139. stmt.setString( 14, item.getBasisRep() );
  140. stmt.setString( 15, item.getRep1099B() );
  141. stmt.setString( 16, item.getTranType() );
  142. stmt.setString( 17, item.getDateAcqVar() );
  143. stmt.setString( 18, item.getCovered() );
  144. stmt.setString( 19, item.getSymbol() );
  145. stmt.setString( 20, item.getExpired() );
  146.  
  147. stmt.addBatch( );
  148.  
  149. // Execute every N items.
  150. if ( (i + 1) % batchSize == 0 ) {
  151. stmt.executeBatch( );
  152. }
  153.  
  154. }
  155.  
  156. stmt.executeBatch( );
  157.  
  158. }
  159.  
  160. }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement