LoginSignup
9
10

More than 5 years have passed since last update.

Java MySQL 接続

Posted at

メモ
ORMばかりつかってて忘れていたので。


import static java.sql.DriverManager.*;

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

public class Main {

    public static final String URL = "jdbc:mysql://localhost:3306/base1";
    public static final String USER = "base1";
    public static final String PASS = "base1";

    public static void main(String[] args) throws SQLException {
        try (Connection connection = getConnection(URL, USER, PASS)) {

            PreparedStatement prepared = connection
                    .prepareStatement("SELECT ? + ? AS ANS");

            for (int i = 0, j = 10; i < 10; i++, j = j << 2) {
                prepared.setInt(1, i);
                prepared.setInt(2, j);

                ResultSet resultSet = prepared.executeQuery();
                if (resultSet.next())
                    System.out.println(resultSet.getObject(1));
            }
        }

    }
}
9
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10