private 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(); } }
void runTextTest() { Statement stmt = null; try { // System.out.println("Thread :" + getName() + " Text test entered"); setProgressMinMax(0, m_nNumRuns - 1); int nProgressStep = m_nNumRuns / 10 + 1; // System.out.println("Thread :" + getName() + " before commit state"); boolean m_bTransactionsUsed = m_bTrans && m_conn.getMetaData().supportsTransactions(); // System.out.println("Thread :" + getName() + " are transactions used"); boolean bCurrentAutoCommit = m_conn.getAutoCommit(); // System.out.println("Thread :" + getName() + "got currentCommit"); m_conn.setAutoCommit(!m_bTransactionsUsed); // System.out.println("Thread :" + getName() + " commit state set"); stmt = m_conn.createStatement(); int nAccNum, nBranchNum, nTellerNum; double dDelta, dBalance; log( "Starting SQL text benchmark for " + m_nNumRuns + ((m_time > 0) ? " min.\n" : " runs\n"), 0); java.util.Random rand = new java.util.Random(m_nMaxAccount + m_nMaxBranch + m_nMaxTeller); for (int nRun = 0; (m_time > 0) ? true : nRun < m_nNumRuns; nRun++) { if (m_time > 0) { java.util.Date current = new java.util.Date(); if ((current.getTime() - m_time) > (m_nNumRuns * 60000)) break; } try { nAccNum = (int) (rand.nextFloat() * rand.nextFloat() * (m_nMaxAccount - 1)) + 1; nBranchNum = (int) (rand.nextFloat() * (m_nMaxBranch - 1)) + 1; nTellerNum = (int) (rand.nextFloat() * (m_nMaxTeller - 1)) + 1; dDelta = ((double) ((long) ((((int) (rand.nextFloat() * (m_nMaxTeller - 1)) + 1) * (rand.nextFloat() > 0.5 ? -1 : 1)) * 100))) / 100; java.util.Date startTime = new java.util.Date(); log( "UPDATE " + m_Driver.getAccountName() + " SET balance = balance + " + dDelta + " WHERE account = " + nAccNum + "\n", 2); stmt.executeUpdate( "UPDATE " + m_Driver.getAccountName() + " SET balance = balance + " + dDelta + " WHERE account = " + nAccNum); log( "SELECT balance FROM " + m_Driver.getAccountName() + " WHERE account = " + nAccNum + "\n", 2); ResultSet balanceSet = stmt.executeQuery( "SELECT balance FROM " + m_Driver.getAccountName() + " WHERE account = " + nAccNum); balanceSet.next(); dBalance = balanceSet.getDouble(1); balanceSet.close(); log( "UPDATE " + m_Driver.getTellerName() + " SET balance = balance + " + dDelta + " WHERE teller = " + nTellerNum + "\n", 2); stmt.executeUpdate( "UPDATE " + m_Driver.getTellerName() + " SET balance = balance + " + dDelta + " WHERE teller = " + nTellerNum); log( "UPDATE " + m_Driver.getBranchName() + " SET balance = balance + " + dDelta + " WHERE branch = " + nBranchNum + "\n", 2); stmt.executeUpdate( "UPDATE " + m_Driver.getBranchName() + " SET balance = balance + " + dDelta + " WHERE branch = " + nBranchNum); log( "INSERT INTO " + m_Driver.getHistoryName() + " (histid, account, teller, branch, amount, timeoftxn, filler) VALUES (" + nRun + " , " + nAccNum + " , " + nTellerNum + " , " + nBranchNum + " , " + dDelta + " , " + m_nowFunction + " , \'" + BenchPanel.strFiller.substring(0, 21) + "\')\n", 2); stmt.executeUpdate( "INSERT INTO " + m_Driver.getHistoryName() + " (histid, account, teller, branch, amount, timeoftxn, filler) VALUES (" + nRun + " , " + nAccNum + " , " + nTellerNum + " , " + nBranchNum + " , " + dDelta + " , " + m_nowFunction + " , \'" + BenchPanel.strFiller.substring(0, 21) + "\')"); if (m_bQuery) executeQuery(); // System.out.println("Done query"); if (m_bTransactionsUsed) m_conn.commit(); java.util.Date endTime = new java.util.Date(); // System.out.println("Done"); m_nTrans += 1; long diff = endTime.getTime() - startTime.getTime(); if (diff < 1000) m_nTrans1Sec += 1; else if (diff < 2000) m_nTrans2Sec += 1; m_nTimeSum += ((double) diff) / 1000; } catch (SQLException e1) { // System.err.println(e1.getMessage()); // e1.printStackTrace(); break; } if (nRun % nProgressStep == 0) setProgressValue(nRun); // yield(); } setProgressValue(m_nNumRuns - 1); m_conn.setAutoCommit(bCurrentAutoCommit); } catch (SQLException e) { // e.printStackTrace(); // JOptionPane.showMessageDialog(null, e.getMessage(), "SQL Error in Text test", // JOptionPane.ERROR_MESSAGE); log("SQLError in text test : " + e.getMessage(), 0); } finally { if (stmt != null) try { stmt.close(); } catch (SQLException e) { } } }
void runPrepareTest() { setProgressMinMax(0, m_nNumRuns - 1); int nProgressStep = m_nNumRuns / 10 + 1; try { boolean m_bTransactionsUsed = m_bTrans && m_conn.getMetaData().supportsTransactions(); boolean bCurrentAutoCommit = m_conn.getAutoCommit(); m_conn.setAutoCommit(!m_bTransactionsUsed); int nAccNum, nBranchNum, nTellerNum; double dDelta, dBalance; log( "Starting SQL prepare/execute benchmark for " + m_nNumRuns + ((m_time > 0) ? " min.\n" : " runs\n"), 0); java.util.Random rand = new java.util.Random(m_nMaxAccount + m_nMaxBranch + m_nMaxTeller); for (int nRun = 0; (m_time > 0) ? true : nRun < m_nNumRuns; nRun++) { if (m_time > 0) { java.util.Date current = new java.util.Date(); if ((current.getTime() - m_time) > (m_nNumRuns * 60000)) break; } PreparedStatement updAccStmt = null, selAccStmt = null, updTellerStmt = null, updBranchStmt = null, insHistStmt = null; try { nAccNum = (int) (rand.nextFloat() * rand.nextFloat() * (m_nMaxAccount - 1)) + 1; nBranchNum = (int) (rand.nextFloat() * (m_nMaxBranch - 1)) + 1; nTellerNum = (int) (rand.nextFloat() * (m_nMaxTeller - 1)) + 1; dDelta = ((double) ((long) ((((int) (rand.nextFloat() * (m_nMaxTeller - 1)) + 1) * (rand.nextFloat() > 0.5 ? -1 : 1)) * 100))) / 100; // prepare statements updAccStmt = m_conn.prepareStatement( "UPDATE " + m_Driver.getAccountName() + " SET balance = balance + ? WHERE account = ?"); selAccStmt = m_conn.prepareStatement( "SELECT balance FROM " + m_Driver.getAccountName() + " WHERE account = ?"); updTellerStmt = m_conn.prepareStatement( "UPDATE " + m_Driver.getTellerName() + " SET balance = balance + ? WHERE teller = ?"); updBranchStmt = m_conn.prepareStatement( "UPDATE " + m_Driver.getBranchName() + " SET balance = balance + ? WHERE branch = ?"); insHistStmt = m_conn.prepareStatement( "INSERT INTO " + m_Driver.getHistoryName() + " (histid, account, teller, branch, amount, timeoftxn, filler) VALUES (? , ? , ? , ? , ? , " + m_nowFunction + " , ?)"); // bind parameters updAccStmt.setDouble(1, dDelta); updAccStmt.setInt(2, nAccNum); // System.out.println(nAccNum); selAccStmt.setInt(1, nAccNum); updTellerStmt.setDouble(1, dDelta); updTellerStmt.setInt(2, nTellerNum); updBranchStmt.setDouble(1, dDelta); updBranchStmt.setInt(2, nBranchNum); insHistStmt.setInt(1, nRun); insHistStmt.setInt(2, nAccNum); insHistStmt.setInt(3, nTellerNum); insHistStmt.setInt(4, nBranchNum); insHistStmt.setDouble(5, dDelta); insHistStmt.setString(6, BenchPanel.strFiller.substring(0, 21)); java.util.Date startTime = new java.util.Date(); // execute statements log( "UPDATE " + m_Driver.getAccountName() + " SET balance = balance + " + dDelta + " WHERE account = " + nAccNum + "\n", 2); updAccStmt.executeUpdate(); log( "SELECT balance FROM " + m_Driver.getAccountName() + " WHERE account = " + nAccNum + "\n", 2); ResultSet balanceSet = selAccStmt.executeQuery(); balanceSet.next(); dBalance = balanceSet.getFloat(1); // if (balanceSet == null) // System.out.println("balanceSet is NULL"); // else { // String strBalance = balanceSet.getString(1); // System.out.println(balanceSet.wasNull() ? "SQL NULL" : strBalance); // } balanceSet.close(); log( "UPDATE " + m_Driver.getTellerName() + " SET balance = balance + " + dDelta + " WHERE teller = " + nTellerNum + "\n", 2); updTellerStmt.executeUpdate(); log( "UPDATE " + m_Driver.getBranchName() + " SET balance = balance + " + dDelta + " WHERE branch = " + nBranchNum + "\n", 2); updBranchStmt.executeUpdate(); log( "INSERT INTO " + m_Driver.getHistoryName() + " (histid, account, teller, branch, amount, timeoftxn, filler) VALUES (" + nRun + " , " + nAccNum + " , " + nTellerNum + " , " + nBranchNum + " , " + dDelta + " , " + m_nowFunction + " , \'" + BenchPanel.strFiller.substring(0, 21) + "\')\n", 2); insHistStmt.executeUpdate(); if (m_bQuery) executeQuery(); if (m_bTransactionsUsed) m_conn.commit(); java.util.Date endTime = new java.util.Date(); m_nTrans += 1; double diff = endTime.getTime() - startTime.getTime(); if (diff < 1000) m_nTrans1Sec += 1; else if (diff < 2000) m_nTrans2Sec += 1; m_nTimeSum += diff / 1000; } catch (SQLException e1) { // System.err.println(e1.getMessage()); break; } finally { try { if (updAccStmt != null) updAccStmt.close(); if (selAccStmt != null) selAccStmt.close(); if (updTellerStmt != null) updTellerStmt.close(); if (updBranchStmt != null) updBranchStmt.close(); if (insHistStmt != null) insHistStmt.close(); } catch (SQLException e) { } } if (nRun % nProgressStep == 0) setProgressValue(nRun); // yield(); } setProgressValue(m_nNumRuns - 1); m_conn.setAutoCommit(bCurrentAutoCommit); } catch (SQLException e) { // JOptionPane.showMessageDialog(null, e.getMessage(), "SQL Error in Text test", // JOptionPane.ERROR_MESSAGE); log("SQLError in prepare test : " + e.getMessage(), 0); } }
void runProcTest() { setProgressMinMax(0, m_nNumRuns - 1); int nProgressStep = m_nNumRuns / 10 + 1; CallableStatement stmt = null; try { boolean m_bTransactionsUsed = m_bTrans && m_conn.getMetaData().supportsTransactions(); stmt = m_conn.prepareCall("{call ODBC_BENCHMARK(?,?,?,?,?,?,?)}"); int nAccNum, nBranchNum, nTellerNum; double dDelta, dBalance; log( "Starting procedure benchmark for " + m_nNumRuns + ((m_time > 0) ? " min.\n" : " runs\n"), 0); java.util.Random rand = new java.util.Random(m_nMaxAccount + m_nMaxBranch + m_nMaxTeller); for (int nRun = 0; (m_time > 0) ? true : nRun < m_nNumRuns; nRun++) { if (m_time > 0) { java.util.Date current = new java.util.Date(); if ((current.getTime() - m_time) > (m_nNumRuns * 60000)) break; } try { nAccNum = (int) (rand.nextFloat() * rand.nextFloat() * (m_nMaxAccount - 1)) + 1; nBranchNum = (int) (rand.nextFloat() * (m_nMaxBranch - 1)) + 1; nTellerNum = (int) (rand.nextFloat() * (m_nMaxTeller - 1)) + 1; dDelta = ((double) ((long) ((((int) (rand.nextFloat() * (m_nMaxTeller - 1)) + 1) * (rand.nextFloat() > 0.5 ? -1 : 1)) * 100))) / 100; stmt.clearParameters(); stmt.setInt(1, nRun + 1); stmt.setInt(2, nAccNum); stmt.setInt(3, nTellerNum); stmt.setInt(4, nBranchNum); stmt.setFloat(5, (float) dDelta); stmt.registerOutParameter(6, Types.FLOAT); stmt.setString(7, BenchPanel.strFiller.substring(0, 22)); java.util.Date startTime = new java.util.Date(); log( "{call ODBC_BENCHMARK(" + nRun + ", " + nAccNum + "," + nTellerNum + "," + nBranchNum + "," + dDelta + ",?,\'" + BenchPanel.strFiller.substring(0, 22) + "\')}\n", 2); stmt.execute(); stmt.getFloat(6); if (m_bQuery) executeQuery(); java.util.Date endTime = new java.util.Date(); m_nTrans += 1; double diff = endTime.getTime() - startTime.getTime(); if (diff < 1000) m_nTrans1Sec += 1; else if (diff < 2000) m_nTrans2Sec += 1; m_nTimeSum += diff / 1000; } catch (SQLException e1) { // System.err.println(e1.getMessage()); // e1.printStackTrace(); break; } if (nRun % nProgressStep == 0) setProgressValue(nRun); // yield(); } setProgressValue(m_nNumRuns - 1); } catch (SQLException e) { // JOptionPane.showMessageDialog(null, e.getMessage(), "SQL Error in proc test", // JOptionPane.ERROR_MESSAGE); log("SQLError in procedure test : " + e.getMessage(), 0); } finally { if (stmt != null) try { stmt.close(); } catch (SQLException e) { } stmt = null; } }