@@ -10,16 +10,21 @@ public JDBC_Test()
1010
1111public static void main (String argv [])
1212 {
13+ if (argv .length <3 ) {
14+ System .err .println ("java JDBC_Test jdbc-url user password [debug]" );
15+ System .exit (1 );
16+ }
17+
1318String url =new String (argv [0 ]);
1419String usr =new String (argv [1 ]);
1520String pwd =new String (argv [2 ]);
1621Connection db ;
1722Statement s ;
18- ResultSet rs ;
1923
2024// This line outputs debug information to stderr. To enable this, simply
21- // remove the //
22- DriverManager .setLogStream (System .err );
25+ // add an extra parameter to the command line
26+ if (argv .length >3 )
27+ DriverManager .setLogStream (System .err );
2328
2429// Load the driver
2530try {
@@ -31,12 +36,15 @@ public static void main(String argv[])
3136// Lets do a few things -- it doesn't do everything, but
3237// it tests out basic functionality
3338try {
39+ //----------------------------------------
40+ // Connect to database
3441System .out .println ("Connecting to Database URL = " +url );
3542db =DriverManager .getConnection (url ,usr ,pwd );
3643System .out .println ("Connected...Now creating a statement" );
3744s =db .createStatement ();
3845
39- // test Date & Warnings
46+ //----------------------------------------
47+ // test DateStyle & Warnings
4048System .out .println ("Ok... now set European date style" );
4149s .executeUpdate ("set datestyle='european'" );
4250
@@ -49,59 +57,115 @@ public static void main(String argv[])
4957 }
5058db .clearWarnings ();
5159
60+ //----------------------------------------
61+ // Creating a table
5262System .out .println ("Ok...now we will create a table" );
5363s .executeUpdate ("create table test (a int2, b int2,c timestamp,d date)" );
5464
65+ //----------------------------------------
66+ // Simple inserts
5567System .out .println ("Now we will insert some columns" );
5668s .executeUpdate ("insert into test values (1, 1,'now','now')" );
5769s .executeUpdate ("insert into test values (2, 1,'now','01-11-1997')" );// As we are in european, this should mean 1 November 1997
5870s .executeUpdate ("insert into test values (3, 1,'now','11-01-1997')" );// As we are in european, this should mean 11 January 1997
5971System .out .println ("Inserted some data" );
6072
73+ //----------------------------------------
74+ // Now a select (see seperate method at end)
6175System .out .println ("Now lets try a select" );
62- rs =s .executeQuery ("select a, b,c,d from test" );
63- System .out .println ("Back from the select...the following are results" );
64- System .out .println ("rowabc d 'd as string'" );
65- int i =0 ;
66- while (rs .next ())
67- {
68- int a =rs .getInt ("a" );// Example of retriving by column name
69- int b =rs .getInt ("b" );
70- Timestamp c =rs .getTimestamp (3 );// Example of by column number
71- java .sql .Date d =rs .getDate (4 );// Note, java.sql.Date here
72- System .out .println ("row " +i +"" +a +"" +b +"" +c +"" +d +" '" +rs .getString (4 )+"'" );
73- i ++;
74- }
76+ select (s ,"" );
7577
76- // This is a bug at the moment... when you use set datestyle
77- // it must be followed by show datestyle
78- System .out .println ("Now switch to US date format" );
79- s .executeUpdate ("set datestyle='US'" );
80- s .executeUpdate ("show datestyle" );
81-
82- System .out .println ("Now lets try a select" );
83- rs =s .executeQuery ("select a, b,c,d from test" );
84- System .out .println ("Back from the select...the following are results" );
85- //int i = 0;
86- System .out .println ("rowabc d 'd as string'" );
87- while (rs .next ())
88- {
89- int a =rs .getInt ("a" );// Example of retriving by column name
90- int b =rs .getInt ("b" );
91- Timestamp c =rs .getTimestamp (3 );// Example of by column number
92- java .sql .Date d =rs .getDate (4 );// Note, java.sql.Date here
93- System .out .println ("row " +i +"" +a +"" +b +"" +c +"" +d +" '" +rs .getString (4 )+"'" );
94- i ++;
95- }
78+ //----------------------------------------
79+ // Now run some tests
80+ runTests (db ,s );
9681
82+ //----------------------------------------
83+ // Dropping a table
9784System .out .println ("Ok...dropping the table" );
9885s .executeUpdate ("drop table test" );
9986
87+ //----------------------------------------
88+ // Closing the connection
10089System .out .println ("Now closing the connection" );
10190s .close ();
10291db .close ();
92+
93+ //----------------------------------------
10394 }catch (SQLException e ) {
10495System .out .println ("Exception: " +e .toString ());
10596 }
10697 }
98+
99+ /**
100+ * This performs some tests - not really part of an example, hence
101+ * they are in a seperate method.
102+ */
103+ public static void runTests (Connection db ,Statement s )throws SQLException
104+ {
105+ //----------------------------------------
106+ // This is a bug at the moment... when you use set datestyle
107+ // it must be followed by show datestyle
108+ System .out .println ("Now switch to US date format" );
109+ s .executeUpdate ("set datestyle='US'" );
110+ s .executeUpdate ("show datestyle" );
111+
112+ System .out .println ("Now lets try a select" );
113+ select (s ,"" );
114+
115+ //----------------------------------------
116+ // Inserting dates using PreparedStatement
117+ System .out .println ("Ok, now a test using PreparedStatement" );
118+ Date dt =new Date (97 ,11 ,1 );
119+ PreparedStatement ps =db .prepareStatement ("insert into test values (?,?,'now',?)" );
120+
121+ // first insert in US style
122+ s .executeUpdate ("set datestyle='US'" );
123+ s .executeUpdate ("show datestyle" );
124+ ps .setInt (1 ,8 );
125+ ps .setInt (2 ,8 );
126+ ps .setDate (3 ,dt );
127+ ps .executeUpdate ();
128+
129+ // second insert in European style
130+ s .executeUpdate ("set datestyle='european'" );
131+ s .executeUpdate ("show datestyle" );
132+ ps .setInt (1 ,9 );
133+ ps .setInt (2 ,9 );
134+ ps .setDate (3 ,dt );
135+ ps .executeUpdate ();
136+
137+ System .out .println ("Now run the select again - first as European" );
138+ select (s ,"where a>7" );
139+
140+ s .executeUpdate ("set datestyle='US'" );
141+ s .executeUpdate ("show datestyle" );
142+ System .out .println ("Then as US" );
143+ select (s ,"where a>7" );
144+ }
145+
146+ /**
147+ * This performs a select. It's seperate because the tests use it in
148+ * multiple places.
149+ * @param s Statement to run under
150+ * @throws SQLException
151+ */
152+ public static void select (Statement s ,String sql )throws SQLException
153+ {
154+ sql ="select a, b,c,d from test " +sql ;
155+ System .out .println ("\n Query: " +sql );
156+ ResultSet rs =s .executeQuery (sql );
157+ System .out .println ("rowabc d 'd as string'" );
158+ System .out .println ("-------------------------------------------------------------------------------" );
159+ int i =0 ;
160+ while (rs .next ()) {
161+ int a =rs .getInt ("a" );// Example of retriving by column name
162+ int b =rs .getInt ("b" );
163+ Timestamp c =rs .getTimestamp (3 );// Example of by column number
164+ java .sql .Date d =rs .getDate (4 );// Note, java.sql.Date here
165+ System .out .println ("row " +i +"" +a +"" +b +"" +c +"" +d +" '" +rs .getString (4 )+"'" );
166+ i ++;
167+ }
168+ rs .close ();
169+ }
170+
107171}