/** * Method declaration * * @param c */ void connect(Connection c) { if (c == null) { return; } if (cConn != null) { try { cConn.close(); } catch (SQLException e) { } } cConn = c; try { dMeta = cConn.getMetaData(); sStatement = cConn.createStatement(); refreshTree(); } catch (SQLException e) { e.printStackTrace(); } }
private void insertRows(Connection connection) { // Build the SQL INSERT statement String sqlInsert = "insert into " + jtfTableName.getText() + " values ("; // Use a Scanner to read text from the file Scanner input = null; // Get file name from the text field String filename = jtfFilename.getText().trim(); try { // Create a scanner input = new Scanner(new File(filename)); // Create a statement Statement statement = connection.createStatement(); System.out.println( "Driver major version? " + connection.getMetaData().getDriverMajorVersion()); // Determine if batchUpdatesSupported is supported boolean batchUpdatesSupported = false; try { if (connection.getMetaData().supportsBatchUpdates()) { batchUpdatesSupported = true; System.out.println("batch updates supported"); } else { System.out.println( "The driver is of JDBC 2 type, but " + "does not support batch updates"); } } catch (UnsupportedOperationException ex) { System.out.println("The driver does not support JDBC 2"); } // Determine if the driver is capable of batch updates if (batchUpdatesSupported) { // Read a line and add the insert table command to the batch while (input.hasNext()) { statement.addBatch(sqlInsert + input.nextLine() + ")"); } statement.executeBatch(); jlblStatus.setText("Batch updates completed"); } else { // Read a line and execute insert table command while (input.hasNext()) { statement.executeUpdate(sqlInsert + input.nextLine() + ")"); } jlblStatus.setText("Single row update completed"); } } catch (SQLException ex) { System.out.println(ex); } catch (FileNotFoundException ex) { System.out.println("File not found: " + filename); } catch (IOException ex) { ex.printStackTrace(); } finally { if (input != null) input.close(); } }