|
6 | 6 |
|
7 | 7 | /**
|
8 | 8 | *
|
9 |
| - * $Id: basic.java,v 1.7 2001/01/31 09:23:45 peter Exp $ |
| 9 | + * $Id: basic.java,v 1.8 2001/10/25 05:59:58 momjian Exp $ |
10 | 10 | *
|
11 | 11 | * This example tests the basic components of the JDBC driver, and shows
|
12 | 12 | * how even the simplest of queries can be implemented.
|
|
20 | 20 |
|
21 | 21 | publicclassbasic
|
22 | 22 | {
|
23 |
| -Connectiondb;// The connection to the database |
24 |
| -Statementst;// Our statement to run queries with |
25 |
| - |
26 |
| -publicbasic(Stringargs[])throwsClassNotFoundException,FileNotFoundException,IOException,SQLException |
27 |
| - { |
28 |
| -Stringurl =args[0]; |
29 |
| -Stringusr =args[1]; |
30 |
| -Stringpwd =args[2]; |
31 |
| - |
32 |
| -// Load the driver |
33 |
| -Class.forName("org.postgresql.Driver"); |
34 |
| - |
35 |
| -// Connect to database |
36 |
| -System.out.println("Connecting to Database URL = " +url); |
37 |
| -db =DriverManager.getConnection(url,usr,pwd); |
38 |
| - |
39 |
| -System.out.println("Connected...Now creating a statement"); |
40 |
| -st =db.createStatement(); |
41 |
| - |
42 |
| -// Clean up the database (in case we failed earlier) then initialise |
43 |
| -cleanup(); |
44 |
| - |
45 |
| -// Now run tests using JDBC methods |
46 |
| -doexample(); |
47 |
| - |
48 |
| -// Clean up the database |
49 |
| -cleanup(); |
50 |
| - |
51 |
| -// Finally close the database |
52 |
| -System.out.println("Now closing the connection"); |
53 |
| -st.close(); |
54 |
| -db.close(); |
55 |
| - |
56 |
| -//throw postgresql.Driver.notImplemented(); |
57 |
| - } |
58 |
| - |
59 |
| -/** |
60 |
| - * This drops the table (if it existed). No errors are reported. |
61 |
| - */ |
62 |
| -publicvoidcleanup() |
63 |
| - { |
64 |
| -try { |
65 |
| -st.executeUpdate("drop table basic"); |
66 |
| - }catch(Exceptionex) { |
67 |
| -// We ignore any errors here |
68 |
| - } |
69 |
| - } |
70 |
| - |
71 |
| -/** |
72 |
| - * This performs the example |
73 |
| - */ |
74 |
| -publicvoiddoexample()throwsSQLException |
75 |
| - { |
76 |
| -System.out.println("\nRunning tests:"); |
77 |
| - |
78 |
| -// First we need a table to store data in |
79 |
| -st.executeUpdate("create table basic (a int2, b int2)"); |
80 |
| - |
81 |
| -// Now insert some data, using the Statement |
82 |
| -st.executeUpdate("insert into basic values (1,1)"); |
83 |
| -st.executeUpdate("insert into basic values (2,1)"); |
84 |
| -st.executeUpdate("insert into basic values (3,1)"); |
85 |
| - |
86 |
| -// This shows how to get the oid of a just inserted row |
87 |
| -// updated for 7.1 |
88 |
| -st.executeUpdate("insert into basic values (4,1)"); |
89 |
| -intinsertedOID = ((org.postgresql.Statement)st).getInsertedOID(); |
90 |
| -System.out.println("Inserted row with oid "+insertedOID); |
91 |
| - |
92 |
| -// Now change the value of b from 1 to 8 |
93 |
| -st.executeUpdate("update basic set b=8"); |
94 |
| -System.out.println("Updated "+st.getUpdateCount()+" rows"); |
95 |
| - |
96 |
| -// Now delete 2 rows |
97 |
| -st.executeUpdate("delete from basic where a<3"); |
98 |
| -System.out.println("deleted "+st.getUpdateCount()+" rows"); |
99 |
| - |
100 |
| -// For large inserts, a PreparedStatement is more efficient, because it |
101 |
| -// supports the idea of precompiling the SQL statement, and to store |
102 |
| -// directly, a Java object into any column. PostgreSQL doesnt support |
103 |
| -// precompiling, but does support setting a column to the value of a |
104 |
| -// Java object (like Date, String, etc). |
105 |
| -// |
106 |
| -// Also, this is the only way of writing dates in a datestyle independent |
107 |
| -// manner. (DateStyles are PostgreSQL's way of handling different methods |
108 |
| -// of representing dates in the Date data type.) |
109 |
| -PreparedStatementps =db.prepareStatement("insert into basic values (?,?)"); |
110 |
| -for(inti=2;i<5;i++) { |
111 |
| -ps.setInt(1,4);// "column a" = 5 |
112 |
| -ps.setInt(2,i);// "column b" = i |
113 |
| -ps.executeUpdate();// executeUpdate because insert returns no data |
114 |
| - } |
115 |
| -ps.close();// Always close when we are done with it |
116 |
| - |
117 |
| -// Finally perform a query on the table |
118 |
| -System.out.println("performing a query"); |
119 |
| -ResultSetrs =st.executeQuery("select a, b from basic"); |
120 |
| -if(rs!=null) { |
121 |
| -// Now we run through the result set, printing out the result. |
122 |
| -// Note, we must call .next() before attempting to read any results |
123 |
| -while(rs.next()) { |
124 |
| -inta =rs.getInt("a");// This shows how to get the value by name |
125 |
| -intb =rs.getInt(2);// This shows how to get the value by column |
126 |
| -System.out.println(" a="+a+" b="+b); |
127 |
| - } |
128 |
| -rs.close();// again, you must close the result when done |
129 |
| - } |
130 |
| - |
131 |
| -// Now run the query again, showing a more efficient way of getting the |
132 |
| -// result if you don't know what column number a value is in |
133 |
| -System.out.println("performing another query"); |
134 |
| -rs =st.executeQuery("select * from basic where b>1"); |
135 |
| -if(rs!=null) { |
136 |
| -// First find out the column numbers. |
137 |
| -// |
138 |
| -// It's best to do this here, as calling the methods with the column |
139 |
| -// numbers actually performs this call each time they are called. This |
140 |
| -// really speeds things up on large queries. |
141 |
| -// |
142 |
| -intcol_a =rs.findColumn("a"); |
143 |
| -intcol_b =rs.findColumn("b"); |
144 |
| - |
145 |
| -// Now we run through the result set, printing out the result. |
146 |
| -// Again, we must call .next() before attempting to read any results |
147 |
| -while(rs.next()) { |
148 |
| -inta =rs.getInt(col_a);// This shows how to get the value by name |
149 |
| -intb =rs.getInt(col_b);// This shows how to get the value by column |
150 |
| -System.out.println(" a="+a+" b="+b); |
151 |
| - } |
152 |
| -rs.close();// again, you must close the result when done |
153 |
| - } |
154 |
| - |
155 |
| -// Now test maxrows by setting it to 3 rows |
156 |
| -st.setMaxRows(3); |
157 |
| -System.out.println("performing a query limited to "+st.getMaxRows()); |
158 |
| -rs =st.executeQuery("select a, b from basic"); |
159 |
| -while(rs.next()) { |
160 |
| -inta =rs.getInt("a");// This shows how to get the value by name |
161 |
| -intb =rs.getInt(2);// This shows how to get the value by column |
162 |
| -System.out.println(" a="+a+" b="+b); |
163 |
| - } |
164 |
| -rs.close();// again, you must close the result when done |
165 |
| - |
166 |
| -// The last thing to do is to drop the table. This is done in the |
167 |
| -// cleanup() method. |
168 |
| - } |
169 |
| - |
170 |
| -/** |
171 |
| - * Display some instructions on how to run the example |
172 |
| - */ |
173 |
| -publicstaticvoidinstructions() |
174 |
| - { |
175 |
| -System.out.println("\nThis example tests the basic components of the JDBC driver, demonstrating\nhow to build simple queries in java.\n"); |
176 |
| -System.out.println("Useage:\n java example.basic jdbc:postgresql:database user password [debug]\n\nThe debug field can be anything. It's presence will enable DriverManager's\ndebug trace. Unless you want to see screens of items, don't put anything in\nhere."); |
177 |
| -System.exit(1); |
178 |
| - } |
179 |
| - |
180 |
| -/** |
181 |
| - * This little lot starts the test |
182 |
| - */ |
183 |
| -publicstaticvoidmain(Stringargs[]) |
184 |
| - { |
185 |
| -System.out.println("PostgreSQL basic test v6.3 rev 1\n"); |
186 |
| - |
187 |
| -if(args.length<3) |
188 |
| -instructions(); |
189 |
| - |
190 |
| -// This line outputs debug information to stderr. To enable this, simply |
191 |
| -// add an extra parameter to the command line |
192 |
| -if(args.length>3) |
193 |
| -DriverManager.setLogStream(System.err); |
194 |
| - |
195 |
| -// Now run the tests |
196 |
| -try { |
197 |
| -basictest =newbasic(args); |
198 |
| - }catch(Exceptionex) { |
199 |
| -System.err.println("Exception caught.\n"+ex); |
200 |
| -ex.printStackTrace(); |
201 |
| - } |
202 |
| - } |
| 23 | +Connectiondb;// The connection to the database |
| 24 | +Statementst;// Our statement to run queries with |
| 25 | + |
| 26 | +publicbasic(Stringargs[])throwsClassNotFoundException,FileNotFoundException,IOException,SQLException |
| 27 | +{ |
| 28 | +Stringurl =args[0]; |
| 29 | +Stringusr =args[1]; |
| 30 | +Stringpwd =args[2]; |
| 31 | + |
| 32 | +// Load the driver |
| 33 | +Class.forName("org.postgresql.Driver"); |
| 34 | + |
| 35 | +// Connect to database |
| 36 | +System.out.println("Connecting to Database URL = " +url); |
| 37 | +db =DriverManager.getConnection(url,usr,pwd); |
| 38 | + |
| 39 | +System.out.println("Connected...Now creating a statement"); |
| 40 | +st =db.createStatement(); |
| 41 | + |
| 42 | +// Clean up the database (in case we failed earlier) then initialise |
| 43 | +cleanup(); |
| 44 | + |
| 45 | +// Now run tests using JDBC methods |
| 46 | +doexample(); |
| 47 | + |
| 48 | +// Clean up the database |
| 49 | +cleanup(); |
| 50 | + |
| 51 | +// Finally close the database |
| 52 | +System.out.println("Now closing the connection"); |
| 53 | +st.close(); |
| 54 | +db.close(); |
| 55 | + |
| 56 | +//throw postgresql.Driver.notImplemented(); |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * This drops the table (if it existed). No errors are reported. |
| 61 | + */ |
| 62 | +publicvoidcleanup() |
| 63 | +{ |
| 64 | +try |
| 65 | +{ |
| 66 | +st.executeUpdate("drop table basic"); |
| 67 | +} |
| 68 | +catch (Exceptionex) |
| 69 | +{ |
| 70 | +// We ignore any errors here |
| 71 | +} |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * This performs the example |
| 76 | + */ |
| 77 | +publicvoiddoexample()throwsSQLException |
| 78 | +{ |
| 79 | +System.out.println("\nRunning tests:"); |
| 80 | + |
| 81 | +// First we need a table to store data in |
| 82 | +st.executeUpdate("create table basic (a int2, b int2)"); |
| 83 | + |
| 84 | +// Now insert some data, using the Statement |
| 85 | +st.executeUpdate("insert into basic values (1,1)"); |
| 86 | +st.executeUpdate("insert into basic values (2,1)"); |
| 87 | +st.executeUpdate("insert into basic values (3,1)"); |
| 88 | + |
| 89 | +// This shows how to get the oid of a just inserted row |
| 90 | +// updated for 7.1 |
| 91 | +st.executeUpdate("insert into basic values (4,1)"); |
| 92 | +intinsertedOID = ((org.postgresql.Statement)st).getInsertedOID(); |
| 93 | +System.out.println("Inserted row with oid " +insertedOID); |
| 94 | + |
| 95 | +// Now change the value of b from 1 to 8 |
| 96 | +st.executeUpdate("update basic set b=8"); |
| 97 | +System.out.println("Updated " +st.getUpdateCount() +" rows"); |
| 98 | + |
| 99 | +// Now delete 2 rows |
| 100 | +st.executeUpdate("delete from basic where a<3"); |
| 101 | +System.out.println("deleted " +st.getUpdateCount() +" rows"); |
| 102 | + |
| 103 | +// For large inserts, a PreparedStatement is more efficient, because it |
| 104 | +// supports the idea of precompiling the SQL statement, and to store |
| 105 | +// directly, a Java object into any column. PostgreSQL doesnt support |
| 106 | +// precompiling, but does support setting a column to the value of a |
| 107 | +// Java object (like Date, String, etc). |
| 108 | +// |
| 109 | +// Also, this is the only way of writing dates in a datestyle independent |
| 110 | +// manner. (DateStyles are PostgreSQL's way of handling different methods |
| 111 | +// of representing dates in the Date data type.) |
| 112 | +PreparedStatementps =db.prepareStatement("insert into basic values (?,?)"); |
| 113 | +for (inti =2;i <5;i++) |
| 114 | +{ |
| 115 | +ps.setInt(1,4);// "column a" = 5 |
| 116 | +ps.setInt(2,i);// "column b" = i |
| 117 | +ps.executeUpdate();// executeUpdate because insert returns no data |
| 118 | +} |
| 119 | +ps.close();// Always close when we are done with it |
| 120 | + |
| 121 | +// Finally perform a query on the table |
| 122 | +System.out.println("performing a query"); |
| 123 | +ResultSetrs =st.executeQuery("select a, b from basic"); |
| 124 | +if (rs !=null) |
| 125 | +{ |
| 126 | +// Now we run through the result set, printing out the result. |
| 127 | +// Note, we must call .next() before attempting to read any results |
| 128 | +while (rs.next()) |
| 129 | +{ |
| 130 | +inta =rs.getInt("a");// This shows how to get the value by name |
| 131 | +intb =rs.getInt(2);// This shows how to get the value by column |
| 132 | +System.out.println(" a=" +a +" b=" +b); |
| 133 | +} |
| 134 | +rs.close();// again, you must close the result when done |
| 135 | +} |
| 136 | + |
| 137 | +// Now run the query again, showing a more efficient way of getting the |
| 138 | +// result if you don't know what column number a value is in |
| 139 | + |
| 140 | +System.out.println("performing another query"); |
| 141 | +rs =st.executeQuery("select * from basic where b>1"); |
| 142 | +if (rs !=null) |
| 143 | +{ |
| 144 | +// First find out the column numbers. |
| 145 | +// |
| 146 | +// It's best to do this here, as calling the methods with the column |
| 147 | +// numbers actually performs this call each time they are called. This |
| 148 | +// really speeds things up on large queries. |
| 149 | +// |
| 150 | +intcol_a =rs.findColumn("a"); |
| 151 | +intcol_b =rs.findColumn("b"); |
| 152 | + |
| 153 | +// Now we run through the result set, printing out the result. |
| 154 | +// Again, we must call .next() before attempting to read any results |
| 155 | +while (rs.next()) |
| 156 | +{ |
| 157 | +inta =rs.getInt(col_a);// This shows how to get the value by name |
| 158 | +intb =rs.getInt(col_b);// This shows how to get the value by column |
| 159 | +System.out.println(" a=" +a +" b=" +b); |
| 160 | +} |
| 161 | +rs.close();// again, you must close the result when done |
| 162 | +} |
| 163 | + |
| 164 | +// Now test maxrows by setting it to 3 rows |
| 165 | + |
| 166 | +st.setMaxRows(3); |
| 167 | +System.out.println("performing a query limited to " +st.getMaxRows()); |
| 168 | +rs =st.executeQuery("select a, b from basic"); |
| 169 | +while (rs.next()) |
| 170 | +{ |
| 171 | +inta =rs.getInt("a");// This shows how to get the value by name |
| 172 | +intb =rs.getInt(2);// This shows how to get the value by column |
| 173 | +System.out.println(" a=" +a +" b=" +b); |
| 174 | +} |
| 175 | +rs.close();// again, you must close the result when done |
| 176 | + |
| 177 | +// The last thing to do is to drop the table. This is done in the |
| 178 | +// cleanup() method. |
| 179 | +} |
| 180 | + |
| 181 | +/** |
| 182 | + * Display some instructions on how to run the example |
| 183 | + */ |
| 184 | +publicstaticvoidinstructions() |
| 185 | +{ |
| 186 | +System.out.println("\nThis example tests the basic components of the JDBC driver, demonstrating\nhow to build simple queries in java.\n"); |
| 187 | +System.out.println("Useage:\n java example.basic jdbc:postgresql:database user password [debug]\n\nThe debug field can be anything. It's presence will enable DriverManager's\ndebug trace. Unless you want to see screens of items, don't put anything in\nhere."); |
| 188 | +System.exit(1); |
| 189 | +} |
| 190 | + |
| 191 | +/** |
| 192 | + * This little lot starts the test |
| 193 | + */ |
| 194 | +publicstaticvoidmain(Stringargs[]) |
| 195 | +{ |
| 196 | +System.out.println("PostgreSQL basic test v6.3 rev 1\n"); |
| 197 | + |
| 198 | +if (args.length <3) |
| 199 | +instructions(); |
| 200 | + |
| 201 | +// This line outputs debug information to stderr. To enable this, simply |
| 202 | +// add an extra parameter to the command line |
| 203 | +if (args.length >3) |
| 204 | +DriverManager.setLogStream(System.err); |
| 205 | + |
| 206 | +// Now run the tests |
| 207 | +try |
| 208 | +{ |
| 209 | +basictest =newbasic(args); |
| 210 | +} |
| 211 | +catch (Exceptionex) |
| 212 | +{ |
| 213 | +System.err.println("Exception caught.\n" +ex); |
| 214 | +ex.printStackTrace(); |
| 215 | +} |
| 216 | +} |
203 | 217 | }
|