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