CPSC 343 | Database Theory and Practice | Fall 2024 |
JDBC is an API for connecting to a database. You need a connector for your particular database in order to be able to use it.
This document is intended to describe the local environment for working with JDBC in CPSC 343. Refer to the Java Tutorial for information on using JDBC.
If you are working on your own computer, you will need to download the JDBC connector for MySQL.
If you are running Linux, you can copy it to your own computer from /classes/cs343/software/.
Otherwise download Connector/J 9.1.0, the JDBC connector for MySQL. For Windows and Mac, choose "Platform Independent" for the operating system and download the ZIP archive (the second link). For Linux, choose the package for your distribution or choose "Platform Independent" and download the ZIP archive. If you downloaded a package (Linux), use your package installer to install and note/find where the JAR file got installed; for the ZIP archive, extract the JAR file and put it somewhere you can find it.
Once you have the JDBC connector, you'll need to add it to the build path of every Java project that connects to a database. In Eclipse:
Right-click on the project name in the Package Explorer and choose Build Path→Configure Build Path.
Click on "Build Path" in the list of categories on the left side of the dialog box.
Click on the "Libraries" tab.
Click on the "Classpath" section in order to enable the "Add External JARs" button, then click on "Add External JARs", navigate to where the jar file got installed when you installed the JDBC connector (/classes/cs343/software if you are working in the HWS Linux environment), and select it. (The name should be mysql-connector-j-9.1.0.jar or similar.)
Click "Apply and Close" when you are done.
You will need to do this each time you create a new project.
Note: If you have the JDBC connector correctly configured but running the program results in the error something went wrong: No suitable driver found for jdbc:mysql, try adding the following line before the DriverManager.getConnection(url) step:
// load and register the JDBC driver Class.forName("com.mysql.cj.jdbc.Driver");