Java 【データベース操作編】



目次


データベースへの接続

データベースに接続する処理について解説します。

プログラムソース

import java.util.Scanner;
import java.sql.*;

class ClassName {
 public static void main(String[] args) throws SQLException {

    String strCn = "jdbc:oracle:thin:@ホスト名:1521/XEPDB1";

    Connection Cn 
     = DriverManager.getConnection(strCn,"MYUSER","MYUSER");

    System.out.println("接続成功");

    Scanner scan = new Scanner(System.in);
    scan.nextLine();

    Cn.close();

   }
}

解説

DriverManager.getConnection(接続文字列,ユーザー,パスワード)

データベースに接続します。

Connection.Close

データベースとの接続を切断します。


データの取得

データを取得する処理について解説します。

プログラムソース

import java.util.Scanner;
import java.sql.*;

class ClassName {
 public static void main(String[] args) throws SQLException {

    String strCn = "jdbc:oracle:thin:@ホスト名:1521/XEPDB1";
    Connection Cn
     = DriverManager.getConnection(strCn,"MYUSER","MYUSER");

    Statement St = Cn.createStatement(); 

    String strSQL = "SELECT * FROM 社員マスタ";
    ResultSet Rs = St.executeQuery(strSQL);

    while(Rs.next()) {
       System.out.println(Rs.getInt(1) + " " + Rs.getString(2));
    } 

    Scanner scan = new Scanner(System.in);
    scan.nextLine();

    Rs.close(); St.close(); Cn.close();

   }
}

解説

Connection.createStatement()

ステートメントを作成します。

Statement.executeQuery(SQL文)

SQL文を実行し、データを取得します。

ResultSet

取得したデータを格納するオブジェクト


SQLの実行

SQLを実行する処理について解説します。

プログラムソース

import java.util.Scanner;
import java.sql.*;

class ClassName {
 public static void main(String[] args) throws SQLException {

    String strCn = "jdbc:oracle:thin:@ホスト名:1521/XEPDB1";
    Connection Cn
     = DriverManager.getConnection(strCn,"MYUSER","MYUSER");

    Statement St = Cn.createStatement(); 

    String strSQL = "UPDATE 社員マスタ "
                   + "SET 部門コード = 3 "
                   + "WHERE 社員コード = 4";
    St.executeUpdate(strSQL);

    System.out.println("更新完了");

    Scanner scan = new Scanner(System.in);
    scan.nextLine();

    St.close(); Cn.close();

   }
}

解説

Statement.executeUpdate(SQL文)

指定されたSQL文を実行します。


トランザクション処理

トランザクション処理について解説します。

プログラムソース

import java.util.Scanner;
import java.sql.*;

class ClassName {
 public static void main(String[] args) throws SQLException {
   Connection Cn = null; Statement St = null;
   String strCn; String strSQL;

   try {
     strCn = "jdbc:oracle:thin:@ホスト名:1521/XEPDB1";
     Cn = DriverManager.getConnection(strCn,"MYUSER","MYUSER");
     Cn.setAutoCommit(false);
     St = Cn.createStatement(); 
     strSQL="INSERT INTO 社員マスタ VALUES(1, '社員A', 1)";
     St.executeUpdate(strSQL);
     strSQL="INSERT INTO 所有資格マスタ VALUES(1, 1, '簿記3級')";
     St.executeUpdate(strSQL);
     Cn.commit(); System.out.println("成功");
   } catch (SQLException e) {
     Cn.rollback(); System.out.println("失敗");
   }

   Scanner scan = new Scanner(System.in);
   scan.nextLine();
   St.close(); Cn.close();
 }
}

解説

Connection.AutoCommit(false)

自動コミットを無効にします。

Connection.commit()

更新内容を確定する場合は、コミットします。

Connection.rollback()

更新内容を破棄する場合は、ロールバックします。


データベース別の接続文字列

データベース別の接続文字列については、ご覧のとおりです。

プログラムソース

SQLServer
String strCn = "jdbc:sqlserver://localhost\\SQLEXPRESS;“
             + "DatabaseName=Database;";

※mssql-jdbc-9.4.0.jre8.jar使用

MySQL
String strCn = "jdbc:mysql://localhost/myschema";

※mysql-connector-java-8.0.26.jar使用