This post shows how to connect Java to MySQL using JDBC. Therefore, nothing fancy here. Just JDBC basics.
MySQL JDBC Requirements
We used the following items for this post. Although we can use newer versions, the Java JDBC codes pretty much stay the same. For example, the codes would still work on Windows 10, MySQL 8.x, and the appropriate version of MySQL Java connector. In fact, it does not matter which Windows version we use because Java is platform-independent! We could even run our codes in Linux.
- Windows 7 Professional SP1
- MySQL 5.6.16 – Community Server (GPL)
- MySQL Java Connector 5.1.34
Configure a MySQL Instance
For our Java codes to work, we need a MySQL server running. We could create one using MySQL Docker image.
Java Codes
The JDBC API allows us to connect to virtually any database with its respective Java Connector. The following Java codes show how to connect to MySQL using the JDBC API and Java Connector.
First, we could have an empty static method like the main method to start with.
1 2 3 4 5 6 7 | package com.turreta.mysql; public class MySqlJdbcSample { public static void main(String... a) { // ... } } |
Second, we load the appropriate MySQL Java Connector driver class. Note that this operation is sluggish, and we should not use it in production. When you have learned enough Java and JDBC, you realize that there are other efficient ways to load the MySQL Java connector driver class. For now, we stick to using this technique.
1 2 3 | ... Class.forName("com.mysql.jdbc.Driver"); ... |
Third, we create MySQL connections using the MySQL Java Connector driver we have loaded. Notice we pass some parameters that we can get from our MySQL server. For example, the port number and the MySQL database. Also, we pass a database user’s credentials that our Java application will use to interact with the database.
1 2 3 4 5 6 | ... connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/turretadb", "root", "password"); ... |
Fourth, we create Statement objects from Connection objects in Java.
1 2 3 | ... stmt = connection.createStatement(); ... |
JDBC Connection objects allow us to retrieve data from or update/insert data to MySQL. We supply SQL commands to these objects to do our bidding. For example, we could retrieve rows of data from MySQL using the following line of Java codes.
1 2 3 | ... Result rs = stmt.executeQuery("SELECT LASTNAME, FIRSTNAME, MIDDLENAME FROM PERSON"); ... |
When we put the codes together plus some more, we will have a Java program the uses the JDBC API to use MySQL. Consider the complete Java codes below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package com.turreta.mysql; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Connection; import java.sql.DriverManager; public class MySqlJdbcSample { public static void main(String... a) { Connection connection = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/turretadb", "root", "password"); stmt = connection.createStatement(); rs = stmt.executeQuery("SELECT LASTNAME, FIRSTNAME, MIDDLENAME FROM PERSON"); while(rs.next()) { System.out.println("LASTNAME: " + rs.getString("LASTNAME")); System.out.println("MIDDLENAME: " + rs.getString("MIDDLENAME")); System.out.println("FIRSTNAME: " + rs.getString("FIRSTNAME")); System.out.println("================================================"); } } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } } |
This post is (now) part of a reboot Java tutorial.