1 14 package org.compiere.db; 15 16 import java.sql.*; 17 18 24 public class TestConnection 25 { 26 27 34 public TestConnection (String jdbcURL, String uid, String pwd) 35 { 36 System.out.println("Test Connection for " + jdbcURL); 37 m_jdbcURL = jdbcURL; 38 m_uid = uid; 39 m_pwd = pwd; 40 init(); 41 if (m_conn != null) 42 { 43 long time = test(); 44 time += test(); 45 time += test(); 46 time += test(); 47 System.out.println(""); 48 System.out.println("Total Average (" + m_jdbcURL + ")= " + (time/4) + "ms"); 49 } 50 } 52 private String m_jdbcURL; 53 private String m_uid = "compiere"; 54 private String m_pwd = "compiere"; 55 private String m_sql = "SELECT * FROM AD_Element"; 56 private Connection m_conn; 57 58 61 private void init() 62 { 63 long start = System.currentTimeMillis(); 64 Driver driver = null; 65 try 66 { 67 driver = DriverManager.getDriver(m_jdbcURL); 68 } 69 catch (SQLException ex) 70 { 71 } 73 if (driver == null) 74 { 75 try 76 { 77 DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); 78 } 79 catch (SQLException ex) 80 { 81 System.err.println("Init = register Driver: " + ex); 82 } 83 } 84 long end = System.currentTimeMillis(); 85 System.out.println("(1) Driver = " + (end - start) + "ms"); 86 start = System.currentTimeMillis(); 88 try 89 { 90 m_conn = DriverManager.getConnection(m_jdbcURL, m_uid, m_pwd); 91 } 92 catch (SQLException ex) 93 { 94 System.err.println("Init = get Connection: " + ex); 95 } 96 end = System.currentTimeMillis(); 97 System.out.println("(2) Get Connection = " + (end - start) + "ms"); 98 start = System.currentTimeMillis(); 100 try 101 { 102 if (m_conn != null) 103 m_conn.close(); 104 } 105 catch (SQLException ex) 106 { 107 System.err.println("Init = close Connection: " + ex); 108 } 109 end = System.currentTimeMillis(); 110 System.out.println("(3) Close Connection = " + (end - start) + "ms"); 111 } 113 117 private long test() 118 { 119 System.out.println(""); 120 long totalStart = System.currentTimeMillis(); 121 long start = System.currentTimeMillis(); 122 try 123 { 124 m_conn = DriverManager.getConnection(m_jdbcURL, m_uid, m_pwd); 125 } 126 catch (SQLException ex) 127 { 128 System.err.println("Test get Connection: " + ex); 129 return -1; 130 } 131 long end = System.currentTimeMillis(); 132 System.out.println("(A) Get Connection = " + (end - start) + "ms"); 133 try 135 { 136 start = System.currentTimeMillis(); 137 Statement stmt = m_conn.createStatement(); 138 end = System.currentTimeMillis(); 139 System.out.println("(B) Create Statement = " + (end - start) + "ms"); 140 start = System.currentTimeMillis(); 142 ResultSet rs = stmt.executeQuery(m_sql); 143 end = System.currentTimeMillis(); 144 System.out.println("(C) Execute Query = " + (end - start) + "ms"); 145 int no = 0; 147 start = System.currentTimeMillis(); 148 while (rs.next()) 149 { 150 int i = rs.getInt("AD_Client_ID"); 151 String s = rs.getString("Name"); 152 i += s.length(); 153 no++; 154 } 155 end = System.currentTimeMillis(); 156 System.out.println("(D) Read ResultSet = " + (end - start) + "ms - per 10 rows " + ((end - start)/(no/10)) + "ms"); 157 start = System.currentTimeMillis(); 159 rs.close(); 160 end = System.currentTimeMillis(); 161 System.out.println("(E) Close ResultSet = " + (end - start) + "ms"); 162 start = System.currentTimeMillis(); 164 stmt.close(); 165 end = System.currentTimeMillis(); 166 System.out.println("(F) Close Statement = " + (end - start) + "ms"); 167 } 168 catch (SQLException e) 169 { 170 System.err.println("Test: " + e); 171 } 172 start = System.currentTimeMillis(); 174 try 175 { 176 if (m_conn != null) 177 m_conn.close(); 178 } 179 catch (SQLException ex) 180 { 181 System.err.println("Test close Connection: " + ex); 182 } 183 end = System.currentTimeMillis(); 184 System.out.println("(G) Close Connection = " + (end - start) + "ms"); 185 186 long totalEnd = System.currentTimeMillis(); 187 System.out.println("Total Test = " + (totalEnd - totalStart) + "ms"); 188 return (totalEnd - totalStart); 189 } 191 192 193 199 public static void main(String [] args) 200 { 201 String url = "jdbc:oracle:thin:@24.151.26.64:1521:lap11"; 202 String uid = "compiere"; 203 String pwd = "compiere"; 204 if (args.length == 0) 206 { 207 System.out.println("TestConnection <jdbcUrl> <uid> <pwd>"); 208 System.out.println("Example: jdbc:oracle:thin:@dev:1521:dev compiere compiere"); 209 System.out.println("Example: jdbc:oracle:oci8:@dev compiere compiere"); 210 } 211 else if (args.length > 0) 212 url = args[0]; 213 else if (args.length > 1) 214 url = args[1]; 215 else if (args.length > 2) 216 url = args[2]; 217 218 System.out.println(""); 219 TestConnection test = new TestConnection(url, uid, pwd); 220 } 222 } | Popular Tags |