|
4 | 4 |
|
5 | 5 | class JDBC_Test
|
6 | 6 | {
|
7 |
| - public JDBC_Test() |
| 7 | + public JDBC_Test() |
| 8 | + { |
| 9 | + } |
| 10 | + |
| 11 | + public static void main(String argv[]) |
| 12 | + { |
| 13 | + String url = new String(argv[0]); |
| 14 | + String usr = new String(argv[1]); |
| 15 | + String pwd = new String(argv[2]); |
| 16 | + Connection db; |
| 17 | + Statement s; |
| 18 | + ResultSet rs; |
| 19 | + |
| 20 | + // This line outputs debug information to stderr. To enable this, simply |
| 21 | + // remove the // |
| 22 | + DriverManager.setLogStream(System.err); |
| 23 | + |
| 24 | + // Load the driver |
| 25 | + try { |
| 26 | + Class.forName("postgresql.Driver"); |
| 27 | + } catch (ClassNotFoundException e) { |
| 28 | + System.err.println("Exception: " + e.toString()); |
| 29 | + } |
| 30 | + |
| 31 | + // Lets do a few things -- it doesn't do everything, but |
| 32 | + // it tests out basic functionality |
| 33 | + try { |
| 34 | + System.out.println("Connecting to Database URL = " + url); |
| 35 | + db = DriverManager.getConnection(url, usr, pwd); |
| 36 | + System.out.println("Connected...Now creating a statement"); |
| 37 | + s = db.createStatement(); |
| 38 | + |
| 39 | + // test Date & Warnings |
| 40 | + System.out.println("Ok... now set European date style"); |
| 41 | + s.executeUpdate("set datestyle='european'"); |
| 42 | + |
| 43 | + System.out.println("and see what style we are now using (handled by warnings)"); |
| 44 | + s.executeUpdate("show datestyle"); |
| 45 | + SQLWarning sw = db.getWarnings(); |
| 46 | + while(sw!=null) { |
| 47 | + System.out.println("--> "+sw.getMessage()); |
| 48 | + sw=sw.getNextWarning(); |
| 49 | + } |
| 50 | + db.clearWarnings(); |
| 51 | + |
| 52 | + System.out.println("Ok...now we will create a table"); |
| 53 | + s.executeUpdate("create table test (a int2, b int2,c timestamp,d date)"); |
| 54 | + |
| 55 | + System.out.println("Now we will insert some columns"); |
| 56 | + s.executeUpdate("insert into test values (1, 1,'now','now')"); |
| 57 | + s.executeUpdate("insert into test values (2, 1,'now','01-11-1997')"); // As we are in european, this should mean 1 November 1997 |
| 58 | + s.executeUpdate("insert into test values (3, 1,'now','11-01-1997')"); // As we are in european, this should mean 11 January 1997 |
| 59 | + System.out.println("Inserted some data"); |
| 60 | + |
| 61 | + System.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("row a b c d 'd as string'"); |
| 65 | + int i = 0; |
| 66 | + while (rs.next()) |
8 | 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++; |
9 | 74 | }
|
10 |
| - |
11 |
| - public static void main(String argv[]) |
| 75 | + |
| 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("row a b c d 'd as string'"); |
| 87 | + while (rs.next()) |
12 | 88 | {
|
13 |
| - String url = new String(argv[0]); |
14 |
| - Connection db; |
15 |
| - Statement s; |
16 |
| - ResultSet rs; |
17 |
| - |
18 |
| - // Load the driver |
19 |
| - try |
20 |
| - { |
21 |
| - Class.forName("postgresql.Driver"); |
22 |
| - } catch (ClassNotFoundException e) { |
23 |
| - System.err.println("Exception: " + e.toString()); |
24 |
| - } |
25 |
| - |
26 |
| - // Lets do a few things -- it doesn't do everything, but |
27 |
| - // it tests out basic functionality |
28 |
| - try |
29 |
| - { |
30 |
| - System.out.println("Connecting to Database URL = " + url); |
31 |
| - db = DriverManager.getConnection(url, "adrian", ""); |
32 |
| - System.out.println("Connected...Now creating a statement"); |
33 |
| - s = db.createStatement(); |
34 |
| - System.out.println("Ok...now we will create a table"); |
35 |
| - s.executeUpdate("create table test (a int2, b int2)"); |
36 |
| - System.out.println("Now we will insert some columns"); |
37 |
| - s.executeUpdate("insert into test values (1, 1)"); |
38 |
| - s.executeUpdate("insert into test values (2, 1)"); |
39 |
| - s.executeUpdate("insert into test values (3, 1)"); |
40 |
| - System.out.println("Inserted some data"); |
41 |
| - System.out.println("Now lets try a select"); |
42 |
| - rs = s.executeQuery("select a, b from test"); |
43 |
| - System.out.println("Back from the select...the following are results"); |
44 |
| - int i = 0; |
45 |
| - while (rs.next()) |
46 |
| - { |
47 |
| - int a = rs.getInt("a"); |
48 |
| - int b = rs.getInt("b"); |
49 |
| - System.out.println("row " + i + " " + a + " " + b); |
50 |
| - i++; |
51 |
| - } |
52 |
| - System.out.println("Ok...dropping the table"); |
53 |
| - s.executeUpdate("drop table test"); |
54 |
| - System.out.println("Now closing the connection"); |
55 |
| - s.close(); |
56 |
| - db.close(); |
57 |
| - } catch (SQLException e) { |
58 |
| - System.out.println("Exception: " + e.toString()); |
59 |
| - } |
| 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++; |
60 | 95 | }
|
| 96 | + |
| 97 | + System.out.println("Ok...dropping the table"); |
| 98 | + s.executeUpdate("drop table test"); |
| 99 | + |
| 100 | + System.out.println("Now closing the connection"); |
| 101 | + s.close(); |
| 102 | + db.close(); |
| 103 | + } catch (SQLException e) { |
| 104 | + System.out.println("Exception: " + e.toString()); |
| 105 | + } |
| 106 | + } |
61 | 107 | }
|
0 commit comments