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: 2009-08-24 14:42:25 -0700 (Mon, 24 Aug 2009) $'
10
 * '$Revision: 5035 $'
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.database.*;
33
import edu.ucsb.nceas.metacat.properties.PropertyService;
34
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
35

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

    
40
public class JDBCTest {
41

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

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

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

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

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

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

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

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

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

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

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

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

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

    
157
	}//insert
158

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

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

    
170
		try {
171

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

    
182
		}
183

    
184
	}//select
185

    
186
}//DBConnectionPoolTester
(1-1/45)