JDBC Documentation
JDBC Documentation
The Java Database Connectivity interface enables a Java application to connect to a relational
database in a storage independent manner. That is, JDBC presents a uniform interface to Java
applications that allows changing the database with just a few value changes in the Java code.
These are the steps necessary to establish a connection from a Java application to a MySQL
database using Eclipse.
3306 refers to server port on which MySQL listens for remote Queries.
3. Load DB connector/driver into memory: (omit this section if your J-connector is rev 8 or
higher. This connector is used with MySQL 8.0 and higher.)
Class.forName(“com.mysql.jdbc.Driver”);
forName() is a static method in Class that will load the driver into a table of
available drivers.
1
J. Pepe
String query = “select * from members;”;
6. Create Statement object:
ResultSet rs = stmt.executeQuery(query);
When ResultSet object created, the cursor is set to point before the first record.
stmt.executeUpdate(SQL statement);
There may be a return value depending on the statement. E.g., insert or delete will
return the integer number of rows effected.
2
J. Pepe
9. Close connection
con.close();
Be sure to close connections when done with them. Some DBs will have a
maximum number of connections limit. May throw checked exception.
3
J. Pepe
The four SQL statements that we will work with are Select, Delete, Insert, and Update. See the
reference posted on Bb to review these statements.
Executing a Delete, Update, or Insert returns an int containing the number of records modified.
E.g.,
String sql = “delete from customers where city=’Boston’;”;
int count = stmt.executeUpdate(sql);
System.out.println(“Number of records deleted = “ + count);
Run examples:
FirstMySQLExample
CreateMySQLData
MySQLSelect
MySQLReport
The MySQL J-connector jar file has to be downloaded for these examples to run. This file must
be placed on the program’s Classpath in Eclipse. You can do this with the actions right click over
program, run as, run configurations, click Classpath tab, select User Entries, add external jar,
locate the J-connector file.
For an Android application the jar file must be placed in the application /res/libs/ folder and
placed on the build path in Eclipse.
4
J. Pepe
Java Application
JDBC API
5
J. Pepe
Prepared Statements
Objects of the Statement class are used to execute static SQL statements containing no
parameters. I.e., all search criteria values of the SQL statement are known at compile time.
Prepared Statements are used to execute SQL statements where some values are not known
until runtime. This is like a Parameter Query in MS Access.
E.g., String sql = “select * from members where city=? and state=?;”;
? is used to specify values to be supplied at runtime. May have one or more such values in a
SQL statement.
Java contains a class, PreparedStatement, that allows you to execute a SQL statement with one
or more values supplied at runtime. The SQL statement is precompiled and stored in a
PreparedStatement object. The object can be used to execute the SQL multiple times with
parameter substitution. It begs to be used in a loop.
PreparedStatement methods to set the values have two arguments, an integer to specify which
parameter is being set, and a value for that parameter.
.setString(1, “Chicago”)
.setInt(3, 123)
.executeUpdate() - returns the int number of records modified, used to execute delete,
update, or insert statements
6
J. Pepe
Prepared Statement Benefits:
Java 7 introduced try with resources blocks. They allow connections and streams to be open in
the resource part and then automatically closed when exiting the try block.
...
} catch(SomeException e) { . . . }
7
J. Pepe
Metadata
In a database, metadata is information about the data and database itself, and can include:
The Data Dictionary component of the database is a centralized catalog of such information.
meta.getDatabaseProductName() à String
meta.getDatabaseProductVersion() à String
meta.getDriverName() à String
meta.getDriverVersion() à String
meta.getURL() à String
ResultSet rs = stmt.executeQuery(query);
meta.getColumnCount() à int
meta.getColumnLabel(int) à String
meta.getColumnTypeName(int) à String
8
J. Pepe
MySQL Data Types
Access MySQL
9
J. Pepe