Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving duplicate Java code
    primarykey
    data
    text
    <p>I have a class written in ~1000 lines of Java code that migrates one database over to another. It has 19 methods for each table being copied over, and 1 which load's the special JDBC driver for Filemaker.</p> <p>I'm looking at this and can't help thinking I've been really stupid here, and just let my urge to call a few methods from a onActionPerformed govern the 'design'. I trimmed out what I could, but it still looks a lot for what it's essentially doing.</p> <p>So here's the gist:</p> <pre><code>public static void companyInsert() throws SQLException{ // This loads the JDBC drivers, gets the connections, turns off auto commit and returns a createStatement() query = classname.Connect(); try { results = query.executeQuery("SELECT * from table"); // Iterate through the results and print them to standard output System.out.println("Starting Customers"); stmt = con.prepareStatement("INSERT ignore INTO table (idone, idtwo) values (?, ?)"); while (results.next()) { String fname = results.getString("field 1"); String lname = results.getString("field 2"); // System.out.println("Found user \"" + fname + " " + lname + "\""); stmt.setString(1, fname); stmt.setString(2, lname); stmt.addBatch(); } // submit the batch for execution int[] updateCounts = stmt.executeBatch(); System.out.println("Update count: " + updateCounts); con.commit(); stmt.close(); System.out.println("Completed Customers"); } catch (BatchUpdateException be) { //handle batch update exception int[] counts = be.getUpdateCounts(); for (int i = 0; i &lt; counts.length; i++) { System.out.println("Statement["+i+"] :"+counts[i]); } con.rollback(); }catch (SQLException e) { //handle SQL exception con.rollback(); } } </code></pre> <p>There's 19 of these methods, with only the sql parts changing (there are also more fields being set for other records, with some also being retrieved as dates). Hope someone has some great idea - if I could just tear out that catch section (this is always the same) then that would be enough!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload