Java Database Connectivity - Basics 1
JDBC is an API that allows access to databases from clients using the Java language. It relies on the specific Connector to be available from the DB.
This post will discuss:
- DriverManager Interface/Connection/Statement/ResultSet Objects and Methods
JDBC Functionality
The libraries associated with connecting to the DB must be installed in order for it to be usable. Connector/J is the one used with JDBC.
JDBC allows the client running Java to interface with a DB. The parameters specify a MySQL (com.mysql) & (jdbc:mysql) DB will be accessed.
JDBC commands can be executed within a try-catch block as SQL exceptions are checked exceptions - so it’s easier to figure what goes wrong w/ then.
Returns the interface Driver associated with the contained parameter.
Class.forName("com.mysql.cj.jdbc.Driver");
Create the object Connection to handle the connection with the DB.
Connection con = DriverManager.getConnection("jdbc:mysql://address:port/schema?serverTimezone=UTC","username","password");
Create the object Statement to handle commands written in Java.
Statement statementA = con.createStatement();
Create the ResultSet object, which contains the table with the queried data.
ResultSet resultSet = statementA.executeQuery("QUERY");
The .next method both moves to the next row in the table, while also functioning as a conditional, where true if the next row exists and false when it does not.
resultSet.next();
OR
statement(resultSet.next()){
...
}
-gonkgonk