Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4027409
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: Mysql/postgreSQL

2012-11-12 01:32:45

If the following program runs OK, then we have everything installed OK. We check the version of the PostgreSQL server.

  1. package zetcode;

  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;

  9. public class Version {

  10.     public static void main(String[] args) {

  11.         Connection con = null;
  12.         Statement st = null;
  13.         ResultSet rs = null;

  14.     
  15.         String url = "jdbc:postgresql://localhost/testdb";
  16.         String user = "user12";
  17.         String password = "user12";

  18.         try {
  19.             con = DriverManager.getConnection(url, user, password);
  20.             st = con.createStatement();
  21.             rs = st.executeQuery("SELECT VERSION()");

  22.             if (rs.next()) {
  23.                 System.out.println(rs.getString(1));
  24.             }

  25.         } catch (SQLException ex) {
  26.             Logger lgr = Logger.getLogger(Version.class.getName());
  27.             lgr.log(Level.SEVERE, ex.getMessage(), ex);

  28.         } finally {
  29.             try {
  30.                 if (rs != null) {
  31.                     rs.close();
  32.                 }
  33.                 if (st != null) {
  34.                     st.close();
  35.                 }
  36.                 if (con != null) {
  37.                     con.close();
  38.                 }

  39.             } catch (SQLException ex) {
  40.                 Logger lgr = Logger.getLogger(Version.class.getName());
  41.                 lgr.log(Level.WARNING, ex.getMessage(), ex);
  42.             }
  43.         }
  44.     }
  45. }

We connect to the database and get some info about the PostgreSQL server.

  1. 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.

  1. con = DriverManager.getConnection(url, user, password);

We establish a connection to the database, using the connection url, user name and password.

  1. st = con.createStatement();

The createStatement() method of the connection object creates a Statement object for sending SQL statements to the database.

  1. 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.

  1. if (result.next()) {
  2.     System.out.println(result.getString(1));
  3. }

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.

  1. } catch (SQLException ex) {
  2.     Logger lgr = Logger.getLogger(Version.class.getName());
  3.     lgr.log(Level.SEVERE, ex.getMessage(), ex);

  4. }

In case of an exception, we log the error message. For this console example, the message is displayed in the terminal.

  1. try {
  2.     if (rs != null) {
  3.         rs.close();
  4.     }
  5.     if (st != null) {
  6.         st.close();
  7.     }
  8.     if (con != null) {
  9.         con.close();
  10.     }
  11. ...

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.

  1. } catch (SQLException ex) {
  2.     Logger lgr = Logger.getLogger(Version.class.getName());
  3.     lgr.log(Level.WARNING, ex.getMessage(), ex);
  4. }

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) |
给主人留下些什么吧!~~