Updated October 30, 2024
Overview
This documentation applies to some projects, notably Projects 5 and 8. It is also a basic setup for generic JDBC connectivity to a MySQL database.
Setup
You must download the MySQL JDBC jar file from https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-j-9.1.0.zip and then extract the contents of the zip file. You only need the mysql-connector-j-9.1.0.jar file
.
After creating your IntelliJ project:
- Select File then Project Structure.
- Select Modules from the left menu.
- Select Dependencies.
- Select +, then JARs or Directories, browse to the MySQL jar, select it, and select Open.
The JAR is now integrated with your project. Here is some supporting code to include in your project. This will provide the basic database connectivity you will need.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBSupport {
public static Connection getDBConn() {
Connection mdb;
String userName="GETFROMINSTRUCTOR", password="GETFROMINSTRUCTOR";
String url = "jdbc:mysql://GETFROMINSTRUCTOR";
try {
mdb = DriverManager.getConnection(url +
"?user="+ userName + "&password=" + password);
return mdb;
} catch (SQLException ex) {
// handle any errors
System.err.println("SQLException: " + ex.getMessage());
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("VendorError: " + ex.getErrorCode());
return null;
}
}
public static boolean getMysqlStatus(Connection mdb) {
if ( mdb != null )
try {
return !mdb.isClosed();
} catch (SQLException e) {
return false;
}
return false;
}
public static void closeDB(Connection mdb) {
try {
if ( mdb != null && !mdb.isClosed() )
mdb.close();
} catch (SQLException e) {
System.err.println("Failed closing MySQL connection.");
}
}
}
Finally, here is a piece of code to get you started on project 5.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Project5_lastname {
public static void main(String[] args) throws SQLException {
Connection mydb = DBSupport.getDBConn();
ResultSet results;
// test query for project 5
String query = "select word from wordsToInsert order by rand() limit 1;";
/* this is some test code for you! */
boolean status = DBSupport.getMysqlStatus(mydb);
System.out.println("DB status is " + (status ? "GOOD!" :"BAD!"));
if (status) {
results = mydb.createStatement().executeQuery(query);
while (results.next())
System.out.println("Got word -> " + results.getString("word"));
}
/* END test code */
DBSupport.closeDB(mydb);
}
}
And here is another to get you started on project 8.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Project8_lastname {
public static void main(String[] args) throws SQLException {
Connection mydb = DBSupport.getDBConn();
ResultSet results;
// test query for project 8
String query = "select path,type from pathnames order by rand() limit 1;";
/* this is some test code for you! */
boolean status = DBSupport.getMysqlStatus(mydb);
System.out.println("DB status is " + (status ? "GOOD!" :"BAD!"));
if (status) {
results = mydb.createStatement().executeQuery(query);
while (results.next())
System.out.println("Got path -> " + results.getString("path")
+ " as " + results.getString("type"));
}
/* END test code */
DBSupport.closeDB(mydb);
}
}
Credentials
You will be sent the URL and credentials for the DBsupport.java
file as an announcement. Once you have them, plug them into the code and compile it as is. It should compile successfully with a test of the MySQL database. If connectivity is good, you will see something like
Project 5
DB status is GOOD! Got word -> MAIN
Project 8
DB status is GOOD! Got path -> /home/julie/fiber as D
Otherwise, you will get some kind of error like the one shown below.
SQLException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. SQLState: 08S01 VendorError: 0 DB status is BAD!
This may not be the only kind of error received. There are so many ways to fail a database connection.