1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A class to test how many sql command one connection can execute
|
4
|
* Copyright: 2000 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Jing Tao
|
7
|
*
|
8
|
* '$Author: jones $'
|
9
|
* '$Date: 2006-11-10 11:30:36 -0800 (Fri, 10 Nov 2006) $'
|
10
|
* '$Revision: 3080 $'
|
11
|
*
|
12
|
* This program is free software; you can redistribute it and/or modify
|
13
|
* it under the terms of the GNU General Public License as published by
|
14
|
* the Free Software Foundation; either version 2 of the License, or
|
15
|
* (at your option) any later version.
|
16
|
*
|
17
|
* This program is distributed in the hope that it will be useful,
|
18
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20
|
* GNU General Public License for more details.
|
21
|
*
|
22
|
* You should have received a copy of the GNU General Public License
|
23
|
* along with this program; if not, write to the Free Software
|
24
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25
|
*/
|
26
|
|
27
|
|
28
|
|
29
|
import java.io.*;
|
30
|
import java.util.Vector;
|
31
|
import java.lang.*;
|
32
|
import java.sql.*;
|
33
|
import edu.ucsb.nceas.metacat.*;
|
34
|
|
35
|
|
36
|
/**
|
37
|
* A class to test DBConnectionPool class
|
38
|
*/
|
39
|
|
40
|
public class JDBCTest
|
41
|
{
|
42
|
|
43
|
/**
|
44
|
* the main routine used to test how many sql command one connection can
|
45
|
* execute
|
46
|
* Usage: java -cp metacat.jar JDBC-diver-file <-Driver driverName -t times>
|
47
|
*
|
48
|
* @param drivername, the jdbc dirver name for database
|
49
|
* @param times, how many queries will be run
|
50
|
*/
|
51
|
public static void main(String [] args)
|
52
|
{
|
53
|
//instaniate a DBConnectionPool object
|
54
|
//Becuase it is singleton, we should use getInstance method
|
55
|
int loop = 10;
|
56
|
String driverName = null;
|
57
|
Connection conn = null;
|
58
|
long index = (new Double (Math.random()*100000)).longValue();
|
59
|
System.out.println("index: "+index);
|
60
|
|
61
|
|
62
|
try
|
63
|
{
|
64
|
for ( int i=0 ; i < args.length; ++i )
|
65
|
{
|
66
|
if ( args[i].equals("-Driver"))
|
67
|
{
|
68
|
driverName=args[++i];
|
69
|
|
70
|
}//if
|
71
|
else if ( args[i].equals( "-t" ) )
|
72
|
{
|
73
|
loop = Integer.parseInt(args[++i]);
|
74
|
}//else if
|
75
|
else
|
76
|
{
|
77
|
System.err.println
|
78
|
( " args[" +i+ "] '" +args[i]+ "' ignored." );
|
79
|
}//else
|
80
|
}//for
|
81
|
|
82
|
System.out.println("Driver name: "+driverName);
|
83
|
//open and get one connection
|
84
|
conn = getConnection(driverName);
|
85
|
|
86
|
|
87
|
//use this connection excecute sql command
|
88
|
for (int i= 0; i<loop; i++)
|
89
|
{
|
90
|
|
91
|
if (conn == null || conn.isClosed())
|
92
|
{
|
93
|
System.out.println("db conncetion is bad");
|
94
|
break;
|
95
|
}
|
96
|
//System.out.println("metadata: "+conn.getMetaData());
|
97
|
//System.out.println("warning: "+conn.getWarnings());
|
98
|
insert(conn, index);
|
99
|
select(conn);
|
100
|
index++;
|
101
|
}//for
|
102
|
System.out.println("End");
|
103
|
|
104
|
}//try
|
105
|
catch (SQLException e)
|
106
|
{
|
107
|
System.out.println("error in sql: "+e.getMessage());
|
108
|
}//catch
|
109
|
catch (Exception ee)
|
110
|
{
|
111
|
System.out.println("error in other: "+ee.getMessage());
|
112
|
}
|
113
|
finally
|
114
|
{
|
115
|
try
|
116
|
{
|
117
|
conn.close();
|
118
|
}
|
119
|
catch (SQLException eee)
|
120
|
{
|
121
|
System.out.println("error in close connection: "+eee.getMessage());
|
122
|
}
|
123
|
}//finally
|
124
|
}//main
|
125
|
|
126
|
|
127
|
/**
|
128
|
* Method to open a connection to database
|
129
|
*/
|
130
|
private static Connection getConnection(String nameOfDriver)
|
131
|
throws SQLException, ClassNotFoundException
|
132
|
{
|
133
|
String url = null;
|
134
|
String user = null;
|
135
|
String password = null;
|
136
|
url = MetaCatUtil.getOption("defaultDB");
|
137
|
//System.out.println("url: "+url);
|
138
|
user = MetaCatUtil.getOption("user");
|
139
|
//System.out.println("user: "+user);
|
140
|
password = MetaCatUtil.getOption("password");
|
141
|
//System.out.println("password: "+password);
|
142
|
//load Oracle dbDriver
|
143
|
Class.forName(nameOfDriver);
|
144
|
//System.out.println("after load dbDriver");
|
145
|
//open and return connection
|
146
|
return DriverManager.getConnection(url, user, password);
|
147
|
}
|
148
|
|
149
|
|
150
|
/**
|
151
|
* Method to run a sal insert command
|
152
|
* @param conn, the connection will be used
|
153
|
* @param i, part of docid
|
154
|
*/
|
155
|
private static void insert(Connection conn, long i) throws SQLException
|
156
|
{
|
157
|
|
158
|
int serialNumber = 0;
|
159
|
//Connection conn = null;
|
160
|
PreparedStatement pStmt = null;
|
161
|
String sql = "insert into xml_documents (docid) values (?)";
|
162
|
String docid = "jing."+i;
|
163
|
try
|
164
|
{
|
165
|
|
166
|
pStmt = conn.prepareStatement(sql);
|
167
|
pStmt.setString(1, docid);
|
168
|
pStmt.execute();
|
169
|
System.out.println("Inserted successfully: "+i);
|
170
|
}
|
171
|
finally
|
172
|
{
|
173
|
pStmt.close();
|
174
|
}
|
175
|
|
176
|
}//insert
|
177
|
|
178
|
|
179
|
/**
|
180
|
* Method to run a sql select commnad
|
181
|
* @param conn, connection will be used
|
182
|
*/
|
183
|
private static void select(Connection conn) throws SQLException
|
184
|
{
|
185
|
|
186
|
int serialNumber = 0;
|
187
|
PreparedStatement pStmt = null;
|
188
|
ResultSet rs = null;
|
189
|
String sql = "select docid from xml_documents where docid like'%jing%'";
|
190
|
|
191
|
try
|
192
|
{
|
193
|
|
194
|
pStmt = conn.prepareStatement(sql);
|
195
|
pStmt.execute();
|
196
|
rs = pStmt.getResultSet();
|
197
|
if (rs.next())
|
198
|
{
|
199
|
String str = rs.getString(1);
|
200
|
System.out.println("Select docid: "+str);
|
201
|
}
|
202
|
}
|
203
|
finally
|
204
|
{
|
205
|
pStmt.close();
|
206
|
|
207
|
}
|
208
|
|
209
|
}//select
|
210
|
|
211
|
}//DBConnectionPoolTester
|