[java]SQL ServerにJDBC接続してインサートを実行する

環境
Spring Tool Suite 4
JavaSE17

書式
1.プレースホルダ
クエスチョンマーク(?)は、プレースホルダです。パラメータで値を設定します。
入力される不正な文字を抑止できるのでSQLインジェクション対策になります。
2.接続とtry-with-resources構文
tryの後ろのかっこにresource (リソース)を記述します。
リソースとは、AutoCloseableインターフェースまたはCloseableインタフェースを実装した
クラスをインスタンス化した変数の宣言です。
3.conn.setAutoCommit(false);
手動コミットするようにしています。デフォルトは自動コミットモードです。自動コミットモードは、
SQLを実行する毎にコミットされます。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SqlTest {
public static void main(String[] args) {
final String URL = "jdbc:sqlserver://localhost\\SQLEXPRESS;"
+ "Database=cftDB;integratedSecurity=true;";
final String SQL =
"insert into usertbl (uid,uname,addr) VALUES (?,?,?)";
try (Connection conn = DriverManager.getConnection(URL)){
conn.setAutoCommit(false);
try(PreparedStatement ps = conn.prepareStatement(SQL)){
ps.setInt(1, 1);
ps.setString(2,"山田太郎");
ps.setString(3,"東京都");
ps.executeUpdate();
conn.commit();
} catch (Exception e) {
conn.rollback();
System.out.println("rollback");
throw e;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("finished");
}
}
}
package com.arkgame.study; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class SqlTest { public static void main(String[] args) { final String URL = "jdbc:sqlserver://localhost\\SQLEXPRESS;" + "Database=cftDB;integratedSecurity=true;"; final String SQL = "insert into usertbl (uid,uname,addr) VALUES (?,?,?)"; try (Connection conn = DriverManager.getConnection(URL)){ conn.setAutoCommit(false); try(PreparedStatement ps = conn.prepareStatement(SQL)){ ps.setInt(1, 1); ps.setString(2,"山田太郎"); ps.setString(3,"東京都"); ps.executeUpdate(); conn.commit(); } catch (Exception e) { conn.rollback(); System.out.println("rollback"); throw e; } } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("finished"); } } }
package com.arkgame.study;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SqlTest {
      public static void main(String[] args) {

            final String URL = "jdbc:sqlserver://localhost\\SQLEXPRESS;"
                         + "Database=cftDB;integratedSecurity=true;";
            final String SQL = 
                        "insert into usertbl (uid,uname,addr) VALUES (?,?,?)";

            try (Connection conn = DriverManager.getConnection(URL)){

                  conn.setAutoCommit(false);

            try(PreparedStatement ps = conn.prepareStatement(SQL)){
                ps.setInt(1, 1);
                ps.setString(2,"山田太郎");
                ps.setString(3,"東京都");
                
                ps.executeUpdate();
                conn.commit();
            } catch (Exception e) {
                conn.rollback();
                System.out.println("rollback");
                throw e;
            }

            } catch (SQLException e) {
                  e.printStackTrace();
            } catch (Exception e) {
                  e.printStackTrace();
            } finally {
                  System.out.println("finished");
            }
      }
}

 

Java

Posted by arkgame