~ubuntu-branches/ubuntu/hoary/libpgjava/hoary

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/example/datestyle.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-06 23:43:06 UTC
  • Revision ID: james.westby@ubuntu.com-20020206234306-hsg7suqr8q56qg40
Tags: upstream-7.2
ImportĀ upstreamĀ versionĀ 7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package example;
 
2
 
 
3
import java.io.*;
 
4
import java.sql.*;
 
5
import java.text.*;
 
6
 
 
7
/*
 
8
 * This example tests the various date styles that are available to postgresql.
 
9
 *
 
10
 * To use this example, you need a database to be in existence. This example
 
11
 * will create a table called datestyle.
 
12
 */
 
13
 
 
14
public class datestyle
 
15
{
 
16
        Connection db;  // The connection to the database
 
17
        Statement st;   // Our statement to run queries with
 
18
 
 
19
        // This is our standard to compare results with.
 
20
        java.sql.Date standard;
 
21
 
 
22
        // This is a list of the available date styles including variants.
 
23
        // These have to match what the "set datestyle" statement accepts.
 
24
        String styles[] = {
 
25
                                                  "postgres,european",
 
26
                                                  "postgres,us",
 
27
                                                  "iso",                // iso has no variants - us/european has no affect
 
28
                                                  "sql,european",
 
29
                                                  "sql,us",
 
30
                                                  "german"              // german has no variants - us/european has no affect
 
31
                                          };
 
32
 
 
33
        public datestyle(String args[]) throws ClassNotFoundException, FileNotFoundException, IOException, SQLException
 
34
        {
 
35
                String url = args[0];
 
36
                String usr = args[1];
 
37
                String pwd = args[2];
 
38
 
 
39
                // Load the driver
 
40
                Class.forName("org.postgresql.Driver");
 
41
 
 
42
                // Connect to database
 
43
                System.out.println("Connecting to Database URL = " + url);
 
44
                db = DriverManager.getConnection(url, usr, pwd);
 
45
 
 
46
                System.out.println("Connected...Now creating a statement");
 
47
                st = db.createStatement();
 
48
 
 
49
                // Clean up the database (in case we failed earlier) then initialise
 
50
                cleanup();
 
51
                init();
 
52
 
 
53
                // Now run tests using JDBC methods
 
54
                doexample();
 
55
 
 
56
                // Clean up the database
 
57
                cleanup();
 
58
 
 
59
                // Finally close the database
 
60
                System.out.println("Now closing the connection");
 
61
                st.close();
 
62
                db.close();
 
63
 
 
64
        }
 
65
 
 
66
        /*
 
67
         * This drops the table (if it existed). No errors are reported.
 
68
         */
 
69
        public void cleanup()
 
70
        {
 
71
                try
 
72
                {
 
73
                        st.executeUpdate("drop table datestyle");
 
74
                }
 
75
                catch (Exception ex)
 
76
                {
 
77
                        // We ignore any errors here
 
78
                }
 
79
        }
 
80
 
 
81
        /*
 
82
         * This initialises the database for this example
 
83
         */
 
84
        public void init() throws SQLException
 
85
        {
 
86
                // Create a table holding a single date
 
87
                st.executeUpdate("create table datestyle (dt date)");
 
88
 
 
89
                // Now create our standard date for the test.
 
90
                //
 
91
                // NB: each component of the date should be different, otherwise the tests
 
92
                //         will not be valid.
 
93
                //
 
94
                // NB: January = 0 here
 
95
                //
 
96
                standard = new java.sql.Date(98, 0, 8);
 
97
 
 
98
                // Now store the result.
 
99
                //
 
100
                // This is an example of how to set a date in a date style independent way.
 
101
                // The only way of doing this is by using a PreparedStatement.
 
102
                //
 
103
                PreparedStatement ps = db.prepareStatement("insert into datestyle values (?)");
 
104
                ps.setDate(1, standard);
 
105
                ps.executeUpdate();
 
106
                ps.close();
 
107
        }
 
108
 
 
109
        /*
 
110
         * This performs the example
 
111
         */
 
112
        public void doexample() throws SQLException
 
113
        {
 
114
                System.out.println("\nRunning tests:");
 
115
 
 
116
                for (int i = 0;i < styles.length;i++)
 
117
                {
 
118
                        System.out.print("Test " + i + " - " + styles[i]);
 
119
                        System.out.flush();
 
120
 
 
121
                        // set the style
 
122
                        st.executeUpdate("set datestyle='" + styles[i] + "'");
 
123
 
 
124
                        // Now because the driver needs to know what the current style is,
 
125
                        // we have to run the following:
 
126
                        st.executeUpdate("show datestyle");
 
127
                        // This is a limitation, but there is no real way around this.
 
128
 
 
129
                        // Now we query the table.
 
130
                        ResultSet rs = st.executeQuery("select dt from datestyle");
 
131
 
 
132
                        // Throw an exception if there is no result (if the table is empty
 
133
                        // there should still be a result).
 
134
                        if (rs == null)
 
135
                                throw new SQLException("The test query returned no data");
 
136
 
 
137
                        while (rs.next())
 
138
                        {
 
139
                                // The JDBC spec states we should only read each column once.
 
140
                                // In the current implementation of the driver, this is not necessary.
 
141
                                // Here we use this fact to see what the query really returned.
 
142
                                if (standard.equals(rs.getDate(1)))
 
143
                                        System.out.println(" passed, returned " + rs.getString(1));
 
144
                                else
 
145
                                        System.out.println(" failed, returned " + rs.getString(1));
 
146
                        }
 
147
                        rs.close();
 
148
                }
 
149
        }
 
150
 
 
151
        /*
 
152
         * Display some instructions on how to run the example
 
153
         */
 
154
        public static void instructions()
 
155
        {
 
156
                System.out.println("\nThis example tests the drivers ability to handle dates correctly if the\nbackend is running any of the various date styles that it supports.\nIdealy this should work fine. If it doesn't, then there is something wrong\npossibly in postgresql.Connection or in the backend itself. If this does occur\nthen please email a bug report.\n");
 
157
                System.out.println("Useage:\n java example.datestyle 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.");
 
158
                System.exit(1);
 
159
        }
 
160
 
 
161
        /*
 
162
         * This little lot starts the test
 
163
         */
 
164
        public static void main(String args[])
 
165
        {
 
166
                System.out.println("PostgreSQL datestyle test v6.3 rev 1\n");
 
167
 
 
168
                if (args.length < 3)
 
169
                        instructions();
 
170
 
 
171
                // This line outputs debug information to stderr. To enable this, simply
 
172
                // add an extra parameter to the command line
 
173
                if (args.length > 3)
 
174
                        DriverManager.setLogStream(System.err);
 
175
 
 
176
                // Now run the tests
 
177
                try
 
178
                {
 
179
                        datestyle test = new datestyle(args);
 
180
                }
 
181
                catch (Exception ex)
 
182
                {
 
183
                        System.err.println("Exception caught.\n" + ex);
 
184
                        ex.printStackTrace();
 
185
                }
 
186
        }
 
187
}