サンプル
【1】INSERT文の実行
SamplePostgreSQLWithInsert.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class SamplePostgreSQLWithInsert {
/**
* main
*/
public static void main(String[] args) throws Exception {
try {
System.out.println("Start");
SamplePostgreSQLWithUpdate.dbAccess();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Finished");
}
}
private static void dbAccess() throws Exception {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/Sample", "user", "password");
statement = connection.createStatement();
// !! ここに注目 !!
int number = statement.executeUpdate("INSERT INTO Person VALUES('X0000002','Tom','m',current_timestamp)");
System.out.println(number);
resultSet = statement.executeQuery("SELECT * FROM Person");
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();
}
}
}
}
【2】Update文の実行
SamplePostgreSQLWithUpdate.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class SamplePostgreSQLWithUpdate {
/**
* main
*/
public static void main(String[] args) throws Exception {
try {
System.out.println("Start");
SamplePostgreSQLWithUpdate.dbAccess();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Finished");
}
}
private static void dbAccess() throws Exception {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/Sample", "user", "password");
statement = connection.createStatement();
// !! ここに注目 !!
int number = statement.executeUpdate(
"UPDATE Person SET name='Amy',sex='f',updateDate=current_timestamp WHERE id='X0000002'");
System.out.println(number);
resultSet = statement.executeQuery("SELECT * FROM Person");
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();
}
}
}
}
【3】Delete文の実行
SamplePostgreSQLWithDelete.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class SamplePostgreSQLWithDelete {
/**
* main
*/
public static void main(String[] args) throws Exception {
try {
System.out.println("Start");
SamplePostgreSQLWithUpdate.dbAccess();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Finished");
}
}
private static void dbAccess() throws Exception {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/Sample", "user", "password");
statement = connection.createStatement();
// !! ここに注目 !!
int number = statement.executeUpdate(
"DELETE FROM Person WHERE id='X0000002'");
System.out.println(number);
resultSet = statement.executeQuery("SELECT * FROM Person");
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();
}
}
}
}