[Java] I definitely want to connect MySQL !!! ~ About getting JDBC driver and JAR file location ~

I want to use MySQL in my project ...

In this Web application production, we are creating an application using a database called ** MySQL **. I was writing a program in Java using Eclipse. How do I connect and use MySQL? The question ...

Therefore, this time, I have summarized the steps ** to use MySQL (database) from a Java program **. The procedure is as follows: point_down_tone2:

** 1. Get JDBC driver (Connector / J) ** ** 2. Put the JAR file in your project ** ** 3. Try data connection **

What is a JDBC driver?

Before going into step 1 ... I'll briefly explain the JBDC driver: wink: A JDBC driver is a set of classes and interfaces required to operate a ** database. ** Each database developer provides it as a ** JAR file (a file that stores classes and interfaces together) **. Since the database used this time is MySQL, it is necessary to obtain a JDBC driver dedicated to MySQL. That is ** "Connector / J" **: bangbang:

1. Get JDBC driver (Connector / J)

This is step 1. Let's get the MySQL-only JBDC driver Connector / J immediately.

You can download it from the URL below: point_down_tone2: https://dev.mysql.com/downloads/connector/j/

Did you see a screen like the one below? image.png

Then select Select Platform: Platform Independent. Download the one that says ZIP below. image.png

If you can download it, the next step is to expand it. Right click on mysql-connector-java-x.x.xx.zip. Select "7-Zip-> Extract to mysql-connector-java-x.x.xx ".

コメント 2020-02-14 112439.png

image.png

When the Zip file is unzipped, a mysql-connector-java-x.x.xx folder is created. image.png

Open this folder. Make sure you have "mysql-connector-java-x.x.xx-bin.jar". image.png

You now have the JBDC driver: relaxed:

2. Put the JAR file in your project

Now that you have the JBDC driver, step 2. I will put this JAR file in the project. Since I am using ** Eclipse **, I can execute the JAR file by adding it to the ** "build path" ** of the project. For ** Dynamic Web Project **, place the JAR file under ** WEB-INF / lib **. image.png

Now you're ready !!!

3. Try data connection

Let's try to make a good connection in step 3. Create a class to check the connection and check the connection as follows.

ConnectSample


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectSample {

	public static void main(String[] args) {
		//DB connection constant
		String DATABASE_NAME = "Database name";
		String PROPATIES = "?characterEncoding=UTF-8&serverTimezone=JST";
		String URL = "jdbc:mySQL://localhost/" + DATABASE_NAME+PROPATIES;
		//DB connection / user constant
		String USER = "username";
		String PASS = "";

		try {
			//Connect to MySQL
			Class.forName("com.mysql.cj.jdbc.Driver");
			//Connect to database
			Connection conn = DriverManager.getConnection(URL, USER, PASS);

			//Processing for database
			System.out.println("Successful connection to database");

		} catch (SQLException | ClassNotFoundException e) {
			e.printStackTrace();
		}

	}

}

URL: jdbc: mySQL: // localhost / + database name

The user name can also be newly set in MySQL: point_down_tone2: CREATE USER'new username' @'localhost'; GRANT SELECT ON database name. * TO'new username' @'localhost'; FLUSH privileges;

If you can confirm the connection, you are done. Thank you for your hard work: grin:

Recommended Posts

[Java] I definitely want to connect MySQL !!! ~ About getting JDBC driver and JAR file location ~
[Java] I installed JDBC and tried to connect with servlet + MySQL. (There is a version using DAO / Bean)
[Java] Connect to MySQL
I started MySQL 5.7 with docker-compose and tried to connect
I want to transition screens with kotlin and java!
I want to connect to Heroku MySQL from a client
I want to find out which version of java the jar file I have is available
Generate and execute Jar file of Java file belonging to package
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Connect to MySQL 8 with Java
I want to implement various functions with kotlin and java!
I want to return to the previous screen with kotlin and java!
I want to display images with REST Controller of Java and Spring!
[About JDBC that connects Java and SQL]
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
Run R from Java I want to run rJava
I want to send an email in Java.
Connect to Aurora (MySQL) from a Java application
A story about trying to operate JAVA File
rsync4j --I want to touch rsync in Java.
I want to write quickly from java to sqlite