import java.sql.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* This examples the basic components of the JDBC driver, and shows
* how even the simplest of queries can be implemented.
*
* To run this example you need to specify the "xsql xsqldb:*"
*/
public class SimpleJDBC
{
static final String user = null;
static final String password = null;
static final String url = "jdbc:extremedb:localhost";
static final int nRecords = 10000;
static int nThread = 5;
void insert_agent_sell(Connection con,int i) throws SQLException {
// con.setAutoCommit(false);
PreparedStatement stmt = con.prepareStatement("insert into unity_agent_sell (agent_id,third_id,tobuy_bookno,sell_datetime,ticket_code,user_id,play_id,agent_name,drawway,bet_code,play_ename,play_cname,term_code,order_id,sell_money,sell_status,fail_code,access_type) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
stmt.setString(1, "1989"+i);
stmt.setString(2, "---B---");
stmt.setString(3, "---C---");
stmt.setString(4, "2014/9/20");
stmt.setString(5, "---D---");
stmt.setString(6, "---E---");
stmt.setInt(7, 5);
stmt.setString(8, "---F---");
stmt.setString(9, "G");
stmt.setString(10, "---H---");
stmt.setString(11, "---I---");
stmt.setString(12, "---J---");
stmt.setString(13, "---K---");
stmt.setString(14, "-L-");
stmt.setDouble(15,129.334);
stmt.setInt(16, 5);
stmt.setString(17, "--M--");
stmt.setString(18, "N-");
stmt.execute();
stmt.close();
}
void insert_sell(Connection con,int i) throws SQLException {
PreparedStatement stmt = con.prepareStatement("insert into unity_sell_3_2014009 (ticket_code,sell_datetime,station_id,sell_term_code,valid_term_code,run_code,withdraw,print_success,play_id,bet_code,bet_num,agent_id,user_id,id_code,phone_code,bank_code,third_id,print_time,sell_amount,recharge_money,win_money,promotions_money,is_success,modify_time,extend_int,extend_string,is_more,access_type,drawway) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
stmt.setString(1, "---O---"+i);
stmt.setString(2, "---P---");
stmt.setString(3, "---Q---");
stmt.setString(4, "---R---");
stmt.setString(5, "---S---");
stmt.setInt(6, 980);
stmt.setInt(7, 13);
stmt.setInt(8, 45);
stmt.setInt(9, 78);
stmt.setString(10, "---T---");
stmt.setInt(11, 99);
stmt.setString(12, "---U---");
stmt.setString(13, "---V---");
stmt.setString(14, "W");
stmt.setString(15, "---X---");
stmt.setString(16, "---Y---");
stmt.setString(17, "--Z--");
stmt.setString(18, "--a--");
stmt.setDouble(19,129.334);
stmt.setDouble(20,30.23);
stmt.setDouble(21,76.981);
stmt.setDouble(22,972.129);
stmt.setInt(23, 987);
stmt.setString(24, "--b--");
stmt.setInt(25, 657);
stmt.setString(26, "--c--");
stmt.setString(27, "d");
stmt.setString(28, "e");
stmt.setString(29, "f");
stmt.execute();
stmt.close();
}
void insert_account_trade(Connection con,int i) throws SQLException {
PreparedStatement stmt = con.prepareStatement("insert into unity_account_trade_201400929 (trade_code,user_id,agent_id,play_id,trade_time,amount_plus1,amount_plus2,amount_plus3,amount_plus4,amount_sub1,amount_sub2,amount_sub3,amount_sub4,recharge_money,win_money,promotions_money,extend_int,third_id,proFlag) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
// System.out.printf("%d\t",i);
stmt.setString(1, i+"---g---");
stmt.setString(2, "---h---");
stmt.setString(3, "---i---");
stmt.setInt(4, 1);
stmt.setString(5, "---j---");
stmt.setDouble(6,129.334);
stmt.setDouble(7,30.23);
stmt.setDouble(8,76.981);
stmt.setDouble(9,972.129);
stmt.setDouble(10,129.334);
stmt.setDouble(11,30.23);
stmt.setDouble(12,76.981);
stmt.setDouble(13,972.129);
stmt.setDouble(14,30.23);
stmt.setDouble(15,76.981);
stmt.setDouble(16,972.129);
stmt.setInt(17, 1);
stmt.setString(18, "---k---");
stmt.setString(19, "l");
stmt.execute();
stmt.close();
}
void update_agent_sell(Connection con,int i) throws SQLException {
PreparedStatement pstmt = con.prepareStatement("update unity_agent_sell set sell_datetime=?,ticket_code=?,sell_status=?,fail_code=? where agent_id=?");
pstmt.setString(1, "2014/9/27");
pstmt.setString(2, "Changed");
pstmt.setInt(3, 77);
pstmt.setString(4, "Succ");
pstmt.setString(5, "1989"+i);
pstmt.executeUpdate();
pstmt.close();
}
void select_all_agent_sell(Connection con) throws SQLException {
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery("select * from unity_agent_sell");
System.out.println("select * from unity_agent_sell");
while (result.next()) {
for(int i=1;i<=18;i++)
{
System.out.print(result.getString(i)+"\t");
}
System.out.println();
}
}
void select_all_Sell(Connection con) throws SQLException {
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery("select * from unity_sell_3_2014009");
System.out.println("select * from unity_sell_3_2014009");
while (result.next()) {
for(int i=1;i<=18;i++)
{
System.out.print(result.getString(i)+"\t");
}
System.out.println();
}
}
void select_all_account_trade(Connection con) throws SQLException {
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery("select * from unity_account_trade_201400929");
System.out.println("select * from unity_account_trade_201400929");
while (result.next()) {
for(int i=1;i<=18;i++)
{
System.out.print(result.getString(i)+"\t");
}
System.out.println();
}
}
class pThread implements Runnable
{
Connection con; // The connection to the database
private CountDownLatch begin;
private CountDownLatch end;
int num;
public pThread(int i, CountDownLatch begin, CountDownLatch end) {
super();
this.num = i;
this.begin = begin;
this.end = end;
}
public void run()
{
try{
Class.forName("com.mcobject.jdbc.McoDriver");
}catch(Exception e){
System.out.println("Class.forName error");
}
// Connect to the database
System.out.println("Connecting to Database URL = " + url);
try{
con = DriverManager.getConnection(url, user, password);
}catch(Exception e){
System.out.println("DriverManager.getConnection error");
}
try{
con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
}catch(Exception e){
System.out.println("TRANSACTION_READ_COMMITTED error");
}
try{
int z = con.getTransactionIsolation();
System.out.println("TransactionIsolation =="+z);
}catch(Exception e){
System.out.println("TRANSACTION_READ_COMMITTED error");
}
if (con != null &&!con.equals("")) {
//do something
System.out.println("Starting for ! ");
// System.out.println("----"+Snum);
try {
try{
begin.await();
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("for start!"+num);
for(int j = 0;j
{
insert_agent_sell(con,num);
insert_sell(con,num);
insert_account_trade(con,num);
update_agent_sell(con,num);
num++;
}
System.out.println("for stop!");
} catch (SQLException e) {
e.printStackTrace();
}finally{
end.countDown();
}
}
try{
con.close();
}catch(Exception e){
System.out.println("con.close error");
}
}
}
public SimpleJDBC(String url) throws ClassNotFoundException, SQLException
{
CountDownLatch begin = new CountDownLatch(1);
CountDownLatch end = new CountDownLatch(nThread);
pThread[] pThreads = new pThread[nThread];
// StartHandlers();
for(int i=0;i
pThreads[i] = new pThread(i*nRecords,begin,end);
ExecutorService exe = Executors.newFixedThreadPool(nThread);
for(pThread p:pThreads)
exe.execute(p);
System.out.println("Race begins!");
begin.countDown();
long start = System.currentTimeMillis();
try{
end.await();
}catch (InterruptedException e) {
e.printStackTrace();
}finally{
System.out.println("Elapsed time for 3 times insert and 1 time update " + nThread*nRecords + " records: " + (System.currentTimeMillis() - start) + " milliseconds");
System.out.println("Race ends!");
}
/*************select***************/
// select_all_agent_sell();
// select_all_Sell();
// select_all_account_trade();
// Finally close the database
System.out.println("Now closing the connection");
}
/**
* This little lot starts the sample
*/
public static void main(String[] args) throws Exception
{
new SimpleJDBC("jdbc:extremedb:localhost");
}
}