I want to be able to import the data in the database into blue J and run queries and then display the results to the user. However, I want to do this without the use of an ODBC DSN and an SQL server. I just want to be able to search the database using SQL directly. Is there a way I can do this? I have imported the SQL library, but am not sure how to use this to link to the database I have made. Thank you in advance
Asked
Active
Viewed 366 times
1 Answers
1
The standard way to access a relational database in Java is by using JDBC. There is a lot of tutorials.
for exemple (for a MySQL DB):
public Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
// could be an IP address, including localhost instead of an URL
return DriverManager.getConnection("jdbc:mysql://my-url.com:3306/my_database", "user", "password");
}
public void test() throws ClassNotFoundException, SQLException {
try(Connection c = getConnection()) {
try (PreparedStatement ps = c.prepareStatement("SELECT id, name FROM person WHERE email = ?")) {
ps.setString(1, "john.doe@mail.com");
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
Integer id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(id + " " + name);
}
}
}
}
}
The jar of the Driver has to be in the class path
Edit
UcanAccess is JDBC Driver for MS-Access

kwisatz
- 1,266
- 3
- 16
- 36