1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 23 24 import java.sql.Connection ; 25 import java.sql.DriverManager ; 26 import java.sql.DatabaseMetaData ; 27 import java.sql.ResultSetMetaData ; 28 import java.sql.Statement ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 import java.sql.SQLException ; 32 import java.sql.Types ; 33 34 import org.apache.derby.tools.ij; 35 36 42 43 public class prepStmtMetaData { 44 45 public static void main(String [] args) { 46 ResultSetMetaData met; 47 Connection con; 48 PreparedStatement ps; 49 System.out.println("Test prepStmtMetaData starting"); 50 51 try 52 { 53 ij.getPropertyArg(args); 56 con = ij.startJBMS(); 57 con.setAutoCommit(true); 59 testMetaData(con, "create table ab(a int)", true); 61 62 testMetaData(con, "alter table ab add column b int", true); 64 65 Statement s = con.createStatement(); 67 s.execute("create procedure testproc() language java external name " + 68 "'org.apache.derbyTesting.functionTests.tests.jdbcapi.prepStmtMetaData.tstmeth'"+ 69 " parameter style java"); 70 71 testMetaData(con, "call testproc()", false); 73 74 testMetaData(con, "drop procedure testproc", true); 76 77 testMetaData(con, "create schema myschema", true); 79 80 testMetaData(con, "drop schema myschema restrict", true); 82 83 86 89 testMetaData(con, "create view myview as select * from ab", true); 91 92 testMetaData(con, "drop view myview", true); 94 95 testMetaData(con, "drop table ab", false); 97 98 testMetaData(con, "create index aindex on ab(a)", true); 100 101 testMetaData(con, "drop index aindex", false); 103 104 testMetaData(con, "insert into ab values(1,1)", true); 106 107 testMetaData(con, "update ab set a = 2", false); 109 110 testMetaData(con, "delete from ab", false); 112 113 ps = con.prepareStatement("select * from ab"); 115 met = ps.getMetaData(); 116 System.out.println("Result meta data for select"); 117 dumpRSMetaData(met); 118 ps.close(); 119 120 testMetaData(con, "set schema rock", false); 122 123 executeStmt(con,"create table bug4579 (c11 int)"); 125 executeStmt(con,"insert into bug4579 values 1"); 126 testMetaData(con, "set schema rick", false); 127 ps = con.prepareStatement("select * from bug4579"); 128 met = ps.getMetaData(); 129 System.out.println("bug 4579 and 5338 : Result meta data for select *"); 130 dumpRSMetaData(met); 131 executeStmt(con,"alter table bug4579 add column c12 int"); 132 met = ps.getMetaData(); 133 System.out.println("bug 4579 and 5338 : Result meta data for select * after alter table but w/o execute query"); 134 dumpRSMetaData(met); 135 executeStmt(con,"alter table bug4579 add column c13 int"); 136 ps.execute(); 137 met = ps.getMetaData(); 138 System.out.println("bug 4579 and 5338 : Result meta data for select * after alter table and execute query"); 139 dumpRSMetaData(met); 140 ps.close(); 141 142 executeStmt(con,"drop table ab"); 144 executeStmt(con,"drop table bug4579"); 145 146 con.close(); 147 } 148 catch (SQLException e) { 149 dumpSQLExceptions(e); 150 e.printStackTrace(System.out); 151 } 152 catch (Throwable e) { 153 System.out.println("FAIL -- unexpected exception:"); 154 e.printStackTrace(System.out); 155 } 156 157 System.out.println("Test prepStmtMetaData finished"); 158 } 159 static private void testMetaData(Connection con, String statement, boolean execute) { 160 try { 161 PreparedStatement ps = con.prepareStatement(statement); 162 ResultSetMetaData met = ps.getMetaData(); 163 dumpRSMetaData(met); 164 if (execute) 165 ps.execute(); 166 ps.close(); 167 168 } 169 catch (SQLException e) { 170 dumpSQLExceptions(e); 171 } 172 catch (Throwable e) { 173 System.out.println("FAIL -- unexpected exception:"); 174 e.printStackTrace(System.out); 175 } 176 } 177 static private void executeStmt(Connection con, String statement) { 178 try { 179 PreparedStatement ps = con.prepareStatement(statement); 180 ps.execute(); 181 ps.close(); 182 } 183 catch (SQLException e) { 184 dumpSQLExceptions(e); 185 } 186 catch (Throwable e) { 187 System.out.println("FAIL -- unexpected exception:"); 188 e.printStackTrace(System.out); 189 } 190 } 191 static private void dumpSQLExceptions (SQLException se) { 192 System.out.println("FAIL -- unexpected exception"); 193 while (se != null) { 194 System.out.print("SQLSTATE("+se.getSQLState()+"):"); 195 se.printStackTrace(System.out); 196 se = se.getNextException(); 197 } 198 } 199 static void dumpRSMetaData(ResultSetMetaData rsmd) throws SQLException { 200 if (rsmd == null || rsmd.getColumnCount() == 0) 201 { 202 System.out.println("ResultSetMetaData is Null or empty"); 203 return; 204 } 205 206 int numCols = rsmd.getColumnCount (); 208 209 if (numCols <= 0) { 210 System.out.println("(no columns!)"); 211 return; 212 } 213 214 for (int i=1; i<=numCols; i++) { 216 if (i > 1) System.out.print(","); 217 System.out.print(rsmd.getColumnLabel(i)); 218 } 219 System.out.println(); 220 } 221 222 public static void tstmeth() 223 { 224 } 226 227 } 228 | Popular Tags |