The following examples show how to modify Java code to make use of 5.10.x (and above) JDBC API.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class executebatch { publicstatic void main(String[] args) throws Exception { final String JDBC_DRIVER = "com.asterdata.ncluster.Driver"; final String DB_URL = "jdbc:ncluster://153.65.178.150:2406/beehive"; final String userName = "db_superuser"; final String password = "db_superuser"; Connection con; Statement stmt; ResultSet rs; try { Class.forName(JDBC_DRIVER); con = DriverManager.getConnection(DB_URL,userName,password); // Start a transaction con.setAutoCommit(false); stmt = con.createStatement(); stmt.addBatch("UPDATE tst SET id = 2"); stmt.addBatch("insert into tst values(3)"); // Submit the batch of commands for this statement to the database stmt.executeBatch(); // Commit the transaction // con.commit(); // Close the existing to be safe before opening a new one stmt.close(); // Print out the Employees stmt = con.createStatement(); rs = stmt.executeQuery("SELECT * FROM tst"); // Loop through and print the employee number, job, and hiredate while (rs.next()) { int id = rs.getInt("ID"); System.out.println(id + ":"); } con.close(); } catch (SQLException ex) { ex.printStackTrace(); } } }
import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; import java.sql.PreparedStatement; public class testPrepare1 { public static void main(String args[]) throws SQLException, ClassNotFoundException{ final String JDBC_DRIVER = "com.asterdata.ncluster.Driver"; final String DB_URL = "jdbc:ncluster://153.65.178.150:2406/beehive"; final String userName = "db_superuser"; final String password = "db_superuser"; Statement stmt = null; Connection conn = null; Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL,userName,password); System.out.println("**********************************************"); int i = 0; String tempTableQuery = "create temp dimension table tmp_tbl6 as select id from tst; "; String selectQuery = "select 1 union all select 2 union all select 3 union all select 4 union all select 5"; conn = DriverManager.getConnection(DB_URL, userName, password); try { conn.setAutoCommit(false); /* Remove following line, HOLD_CURSORS_OVER_COMMIT is not supported, don’t try to set it */ //conn.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT); PreparedStatement selectTotal = conn.prepareStatement(selectQuery); selectTotal.setFetchSize(1); System.out.println("**********************************************"); System.out.println("Fetch Size before executing query is "+selectTotal.getFetchSize()); System.out.println("**********************************************"); ResultSet rs = selectTotal.executeQuery(); System.out.println("------created resultset --------------------------"); /* Remove following line, don’t issue a commit before fetch from the result set. */ /* The result set will be cleared after the transaction committed */ //conn.commit(); while (rs.next()) { System.out.println("----------------------------------------------"); System.out.println("Fetch Size during executing query is "+selectTotal.getFetchSize()); System.out.println("Inside resultset loop: i = "+ i+ "value fetched = "+rs.getInt(1)); System.out.println("----------------------------------------------"); i++; } } catch (Exception e) { System.out.println("Exception is:" + e); e.printStackTrace(); } finally { conn.close(); } }//main }