【Java】【PostgreSQL】PreparedStatement で SELECT/INSERT/UPDATE/DELETE を行う

準備

 * 以下の関連記事を参照のこと
http://blogs.yahoo.co.jp/dk521123/33317642.html

サンプル

SELECT文

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class SamplePostgreSQLWithPreparedStatement {

   public static void main(String[] args) {
      try {
         System.out.println("Start");

         SamplePostgreSQLWithPreparedStatement.dbAccess();
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         System.out.println("Finished");
      }
   }
   
   private static void dbAccess() throws Exception {
      Connection connection = null;
      PreparedStatement statement = null;
      ResultSet resultSet = null;

      try {
         Class.forName("org.postgresql.Driver");

         connection = DriverManager.getConnection(
               "jdbc:postgresql://localhost:5432/Sample", "user", "password");
         
         statement = connection.prepareStatement("SELECT * FROM Person WHERE id=?");
         statement.setString(1, "X0000001");
         resultSet = statement.executeQuery();
         while (resultSet.next()) {
            System.out.println(resultSet.getString("id"));
            System.out.println(resultSet.getString("name"));
            System.out.println(resultSet.getString("sex"));
            System.out.println(resultSet.getString("updateDate"));
         }
      } finally {
         if (resultSet != null) {
            resultSet.close();
         }
         if (statement != null) {
            statement.close();
         }
         if (connection != null) {
            connection.close();
         }
      }
   }
}

INSERT文

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class SamplePostgreSQLWithPreparedStatement {

   public static void main(String[] args) {
      try {
         System.out.println("Start");

         SamplePostgreSQLWithPreparedStatement.dbAccess();
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         System.out.println("Finished");
      }
   }
   
   private static void dbAccess() throws Exception {
      Connection connection = null;
      PreparedStatement statement = null;
      ResultSet resultSet = null;

      try {
         Class.forName("org.postgresql.Driver");

         connection = DriverManager.getConnection(
               "jdbc:postgresql://localhost:5432/Sample", "user", "password");

         // !! ここに注目 !!
         statement = connection.prepareStatement(
               "INSERT INTO Person VALUES(?,?,?,?)");
         statement.setString(1, "X0000004");
         statement.setString(2, "Poul");
         statement.setString(3, "m");
         statement.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
         int number = statement.executeUpdate();
         System.out.println(number);
      } finally {
         if (resultSet != null) {
            resultSet.close();
         }
         if (statement != null) {
            statement.close();
         }
         if (connection != null) {
            connection.close();
         }
      }
   }
}

UPDATE文

* 上記「INSERT文」の「// !! ここに注目 !!」を以下のSQL文に変えるだけ
statement = connection.prepareStatement(
      "UPDATE Person SET name='Tommy',sex='f',updateDate=current_timestamp WHERE id=?");
statement.setString(1, "X0000001");
int number = statement.executeUpdate();

DELETE文

* 上記「INSERT文」の「// !! ここに注目 !!」を以下のSQL文に変えるだけ
statement = connection.prepareStatement(
      "DELETE FROM Person WHERE id=?'");
statement.setString(1, "X0000001");
int number = statement.executeUpdate();


関連記事

PostgreSQL / Java で INSERT/UPDATE/DELETE を行う

http://blogs.yahoo.co.jp/dk521123/33365684.html