adding test scripts
[csql.git] / test / jdbc / Threads / connect.java
blobb01378e492fd5793d58de175c845ff96a33cfa8e
1 /*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
7 /**
9 * @author bijaya
12 import java.sql.*;
14 public class connect implements Runnable
16 Connection con;
17 PreparedStatement stmt;
18 ResultSet rs;
19 Thread th;
20 int thid;
21 int totalrecord;
22 int type;
23 connect(int i,int ty ,int record)
25 System.out.println("thread "+ i);
26 thid=i;
27 totalrecord=record;
28 type=ty;
29 th=new Thread(this);
30 th.start();
32 public void run()
34 if(0==type) insertTest();
35 else if(1==type) selectTest();
36 else if(2==type) updateTest();
37 else if(3==type) deleteTest();
38 else selectTest1();
41 public void insertTest()
43 try{
44 Class.forName("csql.jdbc.JdbcSqlDriver");
45 con = DriverManager.getConnection("jdbc:csql:","root","manager");
46 stmt = con.prepareStatement("insert into emp values (?, 'SECOND');");
47 long count = 0, recordCount=0;
48 int ret = 0;
49 long start = 0,end = 0,curr = 0;
50 long tot = 0;
51 for(int i=0; i<totalrecord; i++){
52 stmt.setInt(1, totalrecord*thid+i);
53 start = System.nanoTime();
54 try{
55 ret = stmt.executeUpdate();
56 }catch(Exception e1)
58 try{
59 ret = stmt.executeUpdate();
60 }catch(Exception e)
62 // System.out.println("Exception in Test=2: "+e);
63 ret = stmt.executeUpdate();
67 if(ret!=1) break;
68 end = System.nanoTime();
69 curr = end - start;
70 tot = tot + curr;
71 count++;
73 stmt.close();
74 long val = tot/totalrecord;
75 //System.out.println("Thread id "+this.thid+" Total rec: "+count+" Avgtime :"+val);
76 System.out.println("Total Record Insert: "+count);
77 Thread.sleep(1000);
78 con.close();
79 }catch(Exception e) {
80 System.out.println("Exception in Test: "+e+"Thread NO"+thid);
81 e.printStackTrace();
85 public void selectTest()
87 try{
88 Class.forName("csql.jdbc.JdbcSqlDriver");
89 con = DriverManager.getConnection("jdbc:csql:","root","manager");
90 stmt = con.prepareStatement("select * from emp where f1=?");
91 Thread.sleep(1000);
92 long count = 0, recordCount=0;
93 int ret = 0;
94 long start = 0,end = 0,curr = 0;
95 long tot = 0;
96 for(int i=0; i<totalrecord; i++){
97 stmt.setInt(1, totalrecord*thid +i);
98 start = System.nanoTime();
99 try{
100 rs = stmt.executeQuery();
101 }catch(Exception e1)
103 try{
104 rs = stmt.executeQuery();
105 }catch(Exception e)
107 rs = stmt.executeQuery();
110 if(rs.next())
112 recordCount++;
115 end = System.nanoTime();
116 curr = end - start;
117 tot = tot + curr;
118 count++;
120 stmt.close();
121 long val = tot/totalrecord;
122 //System.out.println("Thread id "+this.thid+"Total select "+recordCount+" Avgtime :"+val);
123 System.out.println("Total Record select "+recordCount);
124 con.close();
125 }catch(Exception e) {
126 System.out.println("Exception in Test: "+e+"Thread NO"+thid);
127 e.printStackTrace();
131 public void deleteTest()
134 try{
135 Class.forName("csql.jdbc.JdbcSqlDriver");
136 con = DriverManager.getConnection("jdbc:csql:","root","manager");
137 stmt = con.prepareStatement("delete from emp where f1=?");
138 long count = 0, recordCount=0;
139 int ret = 0;
141 Thread.sleep(2000);
142 long start = 0,end = 0,curr = 0;
143 long tot = 0;
144 for(int i=0; i<totalrecord; i++){
145 stmt.setInt(1, totalrecord*thid+i);
146 start = System.nanoTime();
147 try{
148 ret = stmt.executeUpdate();
149 }catch(Exception e1)
151 try{
152 ret = stmt.executeUpdate();
153 }catch(Exception e)
155 // System.out.println("Exception in Test=2: "+e);
156 ret = stmt.executeUpdate();
159 if(ret!=1) break;
160 end = System.nanoTime();
161 curr = end - start;
162 tot = tot + curr;
163 count++;
165 stmt.close();
166 long val = tot/totalrecord;
167 System.out.println("Total Record Delete: "+count);
168 con.close();
169 }catch(Exception e) {
170 System.out.println("Exception in Test: "+e+"Thread NO"+thid);
171 e.printStackTrace();
174 public void updateTest()
176 try{
177 Class.forName("csql.jdbc.JdbcSqlDriver");
178 con = DriverManager.getConnection("jdbc:csql:","root","manager");
179 stmt = con.prepareStatement("update emp set f2='india' where f1=?");
180 long count = 0, recordCount=0;
181 int ret = 0;
183 Thread.sleep(1000);
184 long start = 0,end = 0,curr = 0;
185 long tot = 0;
186 for(int i=0; i<totalrecord; i++){
187 stmt.setInt(1, totalrecord*thid+i);
188 start = System.nanoTime();
189 try{
190 ret = stmt.executeUpdate();
191 }catch(Exception e1)
193 try{
194 ret = stmt.executeUpdate();
195 }catch(Exception e)
197 //System.out.println("Exception in Test=2: "+e);
198 ret = stmt.executeUpdate();
201 if(ret!=1) break;
202 end = System.nanoTime();
203 curr = end - start;
204 tot = tot + curr;
205 count++;
207 stmt.close();
208 long val = tot/totalrecord;
209 //System.out.println("Thread id "+this.thid+" Total rec Update: "+count+" Avgtime :"+val);
210 System.out.println("Total Record Update: "+count);
211 con.close();
212 }catch(Exception e) {
213 System.out.println("Exception in Test: "+e+"Thread NO"+thid);
214 e.printStackTrace();
218 public void selectTest1()
220 try{
221 Class.forName("csql.jdbc.JdbcSqlDriver");
222 con = DriverManager.getConnection("jdbc:csql:","root","manager");
223 stmt = con.prepareStatement("select * from emp where f1>=? and f1<?");
224 Thread.sleep(1000);
225 long count = 0, recordCount=0;
226 int ret = 0;
227 long start = 0,end = 0,curr = 0;
228 long tot = 0;
229 for(int i=0; i<totalrecord/100; i++){
230 stmt.setInt(1, totalrecord*thid +i);
231 stmt.setInt(2, totalrecord*thid +i+100);
232 start = System.nanoTime();
233 try{
234 rs = stmt.executeQuery();
235 }catch(Exception e1)
237 try{
238 rs = stmt.executeQuery();
239 }catch(Exception e)
241 rs = stmt.executeQuery();
244 while(rs.next())
246 recordCount++;
249 end = System.nanoTime();
250 curr = end - start;
251 tot = tot + curr;
252 count++;
254 stmt.close();
255 long val = tot/totalrecord;
256 //System.out.println("Thread id "+this.thid+"Total select "+recordCount+" Avgtime :"+val);
257 System.out.println("Total Record select "+recordCount);
258 con.close();
259 }catch(Exception e) {
260 System.out.println("Exception in Test: "+e+"Thread NO"+thid);
261 e.printStackTrace();