Project

General

Profile

« Previous | Next » 

Revision 4080

Added by daigle about 16 years ago

Merge 1.9 changes into Head

View differences:

JDBCTest.java
24 24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 25
 */
26 26

  
27

  
28

  
29 27
import java.io.*;
30 28
import java.util.Vector;
31 29
import java.lang.*;
32 30
import java.sql.*;
33 31
import edu.ucsb.nceas.metacat.*;
32
import edu.ucsb.nceas.metacat.service.PropertyService;
33
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
34 34

  
35

  
36 35
/**
37 36
 * A class to test DBConnectionPool class
38 37
 */
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 38

  
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
  
39
public class JDBCTest {
126 40

  
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();
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);
206 57

  
207
    }
208
    
209
  }//select
210
  
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

  
211 185
}//DBConnectionPoolTester

Also available in: Unified diff