LoginSignup
0
0

H2Database username password 設定

Last updated at Posted at 2024-01-06
import java.sql.*;
import java.util.Properties;

public class Main {
    public static void main(String[] args) {
        try {
            Class.forName("org.h2.Driver");
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException("ドライバのロードに失敗しました");
        }
        Connection con = null;
        try {
            // url, user, password
            con = DriverManager.getConnection("jdbc:h2:file:~/test", "sa", "");

            // 別の指定方法
            //Properties props = new Properties();
            //props.put("user", "sa");
            //props.put("password", "");
            //con = DriverManager.getConnection("jdbc:h2:~/test", props);

            PreparedStatement pstmt = con.prepareStatement("SELECT * FROM TEST");
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                System.out.println(rs.getString("Name"));
            }

            rs.close();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
0
0
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
0
0