If the following program runs OK, then we have everything
installed OK. We check the version of the PostgreSQL server.
- package zetcode;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class Version {
- public static void main(String[] args) {
- Connection con = null;
- Statement st = null;
- ResultSet rs = null;
-
- String url = "jdbc:postgresql://localhost/testdb";
- String user = "user12";
- String password = "user12";
- try {
- con = DriverManager.getConnection(url, user, password);
- st = con.createStatement();
- rs = st.executeQuery("SELECT VERSION()");
- if (rs.next()) {
- System.out.println(rs.getString(1));
- }
- } catch (SQLException ex) {
- Logger lgr = Logger.getLogger(Version.class.getName());
- lgr.log(Level.SEVERE, ex.getMessage(), ex);
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- if (st != null) {
- st.close();
- }
- if (con != null) {
- con.close();
- }
- } catch (SQLException ex) {
- Logger lgr = Logger.getLogger(Version.class.getName());
- lgr.log(Level.WARNING, ex.getMessage(), ex);
- }
- }
- }
- }
We connect to the database and get some info about the PostgreSQL server.
- String url = "jdbc:postgresql://localhost/testdb";
This is the connection url for the PostgreSQL database. Each driver
has a different syntax for the url. In our case, we provide a
host, a port and a database name.
- con = DriverManager.getConnection(url, user, password);
We establish a connection to the database, using the connection url,
user name and password.
- st = con.createStatement();
The createStatement() method of the connection
object creates a Statement object for sending SQL statements to the database.
- rs = st.executeQuery("SELECT VERSION()");
The createStatement() method of the connection
object executes the given SQL statement, which returns a single ResultSet object.
The ResultSet is a table of data returned by a specific SQL statement.
- if (result.next()) {
- System.out.println(result.getString(1));
- }
A ResultSet object maintains a cursor pointing to its current row of data.
Initially the cursor is positioned before the first row. The next()
method moves the cursor to the next row. If there are no rows left, the method
returns false. The getString() method retrieves the value
of a specified column. The first column has index 1.
- } catch (SQLException ex) {
- Logger lgr = Logger.getLogger(Version.class.getName());
- lgr.log(Level.SEVERE, ex.getMessage(), ex);
- }
In case of an exception, we log the error message. For this console
example, the message is displayed in the terminal.
- try {
- if (rs != null) {
- rs.close();
- }
- if (st != null) {
- st.close();
- }
- if (con != null) {
- con.close();
- }
- ...
Inside the finally block, we close the database resources. We also
check if the objects are not equal to null. This is to prevent
null pointer exceptions. Otherwise we might get a NullPointerException,
which would terminate the application and might leave the resources not cleaned up.
- } catch (SQLException ex) {
- Logger lgr = Logger.getLogger(Version.class.getName());
- lgr.log(Level.WARNING, ex.getMessage(), ex);
- }
We log an error message, when the resources could not be closed.
$ javac zetcode/Version.java
$ java -cp .:lib/postgresql.jdbc4.jar zetcode.Version
PostgreSQL 9.1.2 on i686-pc-linux-gnu, compiled by gcc-4.6.real
(Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1, 32-bit
This is a sample output of the program.
可能的错误参考:http://blog.chinaunix.net/uid-25885064-id-3404672.html
阅读(2189) | 评论(0) | 转发(1) |