Project

General

Profile

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: daigle $'
9
 *     '$Date: 2008-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
10
 * '$Revision: 4080 $'
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
import java.io.*;
28
import java.util.Vector;
29
import java.lang.*;
30
import java.sql.*;
31
import edu.ucsb.nceas.metacat.*;
32
import edu.ucsb.nceas.metacat.service.PropertyService;
33
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
34

    
35
/**
36
 * A class to test DBConnectionPool class
37
 */
38

    
39
public class JDBCTest {
40

    
41
	/**
42
	 * the main routine used to test how many sql command one connection can 
43
	 * execute   
44
	 * Usage: java -cp metacat.jar JDBC-diver-file  <-Driver driverName -t times>
45
	 *
46
	 * @param drivername, the jdbc dirver name for database
47
	 * @param times, how many queries  will be run
48
	 */
49
	public static void main(String[] args) {
50
		//instaniate a DBConnectionPool object
51
		//Becuase it is singleton, we should use getInstance method
52
		int loop = 10;
53
		String driverName = null;
54
		Connection conn = null;
55
		long index = (new Double(Math.random() * 100000)).longValue();
56
		System.out.println("index: " + index);
57

    
58
		try {
59
			for (int i = 0; i < args.length; ++i) {
60
				if (args[i].equals("-Driver")) {
61
					driverName = args[++i];
62

    
63
				}//if
64
				else if (args[i].equals("-t")) {
65
					loop = Integer.parseInt(args[++i]);
66
				}//else if 
67
				else {
68
					System.err.println("   args[" + i + "] '" + args[i] + "' ignored.");
69
				}//else
70
			}//for
71

    
72
			System.out.println("Driver name: " + driverName);
73
			//open and get one connection
74
			conn = getConnection(driverName);
75

    
76
			//use this connection excecute sql command
77
			for (int i = 0; i < loop; i++) {
78

    
79
				if (conn == null || conn.isClosed()) {
80
					System.out.println("db conncetion is bad");
81
					break;
82
				}
83
				//System.out.println("metadata: "+conn.getMetaData());
84
				//System.out.println("warning: "+conn.getWarnings());
85
				insert(conn, index);
86
				select(conn);
87
				index++;
88
			}//for
89
			System.out.println("End");
90

    
91
		}//try
92
		catch (SQLException e) {
93
			System.out.println("error in sql: " + e.getMessage());
94
		}//catch
95
		catch (Exception ee) {
96
			System.out.println("error in other: " + ee.getMessage());
97
		} finally {
98
			try {
99
				conn.close();
100
			} catch (SQLException eee) {
101
				System.out.println("error in close connection: " + eee.getMessage());
102
			}
103
		}//finally
104
	}//main
105

    
106
	/**
107
	 * Method to open a connection to database
108
	 */
109
	private static Connection getConnection(String nameOfDriver) throws SQLException,
110
			ClassNotFoundException {
111
		String url = null;
112
		String user = null;
113
		String password = null;
114

    
115
		try {
116
			url = PropertyService.getProperty("database.connectionURI");
117
			//System.out.println("url: "+url);
118
			user = PropertyService.getProperty("database.user");
119
			//System.out.println("user: "+user);
120
			password = PropertyService.getProperty("database.password");
121
			//System.out.println("password: "+password);
122
		} catch (PropertyNotFoundException pnfe) {
123
			System.err.println("Could not get property in static block: "
124
					+ pnfe.getMessage());
125
		}
126

    
127
		//load Oracle dbDriver
128
		Class.forName(nameOfDriver);
129
		//System.out.println("after load dbDriver");
130
		//open and return connection
131
		return DriverManager.getConnection(url, user, password);
132
	}
133

    
134
	/**
135
	 * Method to run a sal insert command
136
	 * @param conn, the connection will be used
137
	 * @param i, part of docid
138
	 */
139
	private static void insert(Connection conn, long i) throws SQLException {
140

    
141
		int serialNumber = 0;
142
		//Connection conn = null;
143
		PreparedStatement pStmt = null;
144
		String sql = "insert into xml_documents (docid) values (?)";
145
		String docid = "jing." + i;
146
		try {
147

    
148
			pStmt = conn.prepareStatement(sql);
149
			pStmt.setString(1, docid);
150
			pStmt.execute();
151
			System.out.println("Inserted successfully: " + i);
152
		} finally {
153
			pStmt.close();
154
		}
155

    
156
	}//insert
157

    
158
	/**
159
	 * Method to run a sql select commnad
160
	 * @param conn, connection will be used
161
	 */
162
	private static void select(Connection conn) throws SQLException {
163

    
164
		int serialNumber = 0;
165
		PreparedStatement pStmt = null;
166
		ResultSet rs = null;
167
		String sql = "select docid from xml_documents where docid like'%jing%'";
168

    
169
		try {
170

    
171
			pStmt = conn.prepareStatement(sql);
172
			pStmt.execute();
173
			rs = pStmt.getResultSet();
174
			if (rs.next()) {
175
				String str = rs.getString(1);
176
				System.out.println("Select docid: " + str);
177
			}
178
		} finally {
179
			pStmt.close();
180

    
181
		}
182

    
183
	}//select
184

    
185
}//DBConnectionPoolTester
(1-1/37)