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();
}
}
}
}
}
H2Database username password 設定
Last updated at Posted at 2024-01-06
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
List of users who liked
00