Connection to Database (MySQL)
Last updated
Last updated
Here's how you can install JDBC and use it to connect to a database:
1. Installing JDBC Driver:
To use JDBC, you need to download and install the appropriate JDBC driver for your database. Each database vendor provides its JDBC driver.
Example: Installing MySQL JDBC Driver:
Download the "latest" MySQL JDBC driver from ⇒ "Connector/J" ⇒ Connector/J 8.0.{xx}, where {xx} is the latest update number ⇒ In "Select Operating System", choose "Platform Independent" ⇒ ZIP Archive (e.g., "mysql-connector-j-8.0.{xx}.zip
" ⇒ "No thanks, just start my download".
Include the JAR file in your Java project's classpath. Or you can placed it to folder in your java file.
2. Connecting to Databases:
You can establish a connection to a database using the appropriate JDBC URL, username, and password.
Example Code: Connecting to MySQL Database
You can compile Java database programs without the JDBC driver.
But to run the JDBC programs, the JDBC driver's JAR-file must be included in the environment variable CLASSPATH
, or in the java
's command-line option -cp
.
You can set the -cp
option for Java runtime as follows: