package com.mysql.cluster.ndbj.examples;
import com.mysql.cluster.ndbj.*;
import java.sql.ResultSet;
//import com.mysql.cluster.ndbapi.Ndb;
//import com.mysql.cluster.ndbapi.NdbClusterConnection;
//import com.mysql.cluster.ndbapi.NdbFactory;
//import com.mysql.cluster.ndbapi.NdbOperation;
//import com.mysql.cluster.ndbapi.NdbTransaction;
import java.sql.*;
import com.mysql.cluster.ndbj.AbortOption;
import com.mysql.cluster.ndbj.ExecType;
/**
* You need to create a the following table in the "test" database:
*
* mysql-client>create table ndbj_hello
* (`id` INTEGER NOT NULL,
* `firstName` VARCHAR(255) NOT NULL,
* `lastName` VARCHAR(255) NOT NULL,
* PRIMARY KEY(id)
* ) ENGINE=ndb;
*
*
*
Run the program using the command:
* >java com.mysql.cluster.ndbj.examples.HelloWorldInsertSQL
*
*
You can test the result of the program in the "test" database:
*
* mysql-client>SELECT * FROM ndbj_hello;
*
*
* @author jdowling
*/
public class HelloWorldInsertSQL {
public static void main(String[] args) throws SQLException {
/*
if (args.length != 1) {
System.err.println("Correct Usage: prog ");
System.exit(-1);
}*/
String table_name = "ndbj_hello_world";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found");
}
org.apache.commons.dbcp.BasicDataSource ds = new org.apache.commons.dbcp.BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://evgsics11/test?relaxAutoCommit=true");
ds.setUsername("root");
ds.setPassword("");
ds.setPoolPreparedStatements(true);
Connection sqlconn = ds.getConnection();
String catalog = null;
String schemaPattern = null;
// Specify null for tableNamePattern, it should return the names
// of all the tables in the database
String tableNamePattern = null;
String[] types = { "TABLE" };
ResultSet rs = null;
rs = sqlconn.getMetaData().getTables(catalog, schemaPattern, tableNamePattern,types);
while (rs.next()) {
System.out.println("TABLE name : " + rs.getString("TABLE_NAME"));
}
System.out.println("Dropping and recreating schema");
System.out.println("Just checking -- Hasnain M Iqbal");
Statement s = sqlconn.createStatement();
System.out.println(table_name);
s.executeUpdate("DROP TABLE if exists " + table_name);
s.executeUpdate("CREATE TABLE if not exists " +
table_name +
" (id INT not null," +
" firstName varchar(255) not null, " +
" lastName varchar(255) NOT NULL," +
" dummyData VARBINARY(7522), " +
" PRIMARY KEY (id) )" +
" ENGINE=NDBCLUSTER");
try {
for(int i = 10; i <= 10010 ; i++){
byte[] data = new byte[7522];
String st = "INSERT INTO ndbj_hello_world "
+ "(id, firstName, lastName, dummyData) VALUES (?, ?, ?, ?)" ;
String sql = st;
PreparedStatement statement = null;
statement = sqlconn.prepareStatement(sql);
//(id, firstName, lastName, dummyData)
statement.setLong(1, i );
statement.setString(2, "Hasnain");
statement.setString(3, "Iqbal");
statement.setBytes(4, data);
boolean success = statement.execute();
System.out.println("Executed SQL:" + sql + " -> " + i);
statement.close();
}
System.out.println("Done");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
sqlconn.close();
}
}
}