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 **
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:
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?
Then select Select Platform: Platform Independent. Download the one that says ZIP below.
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 ".
When the Zip file is unzipped, a mysql-connector-java-x.x.xx folder is created.
Open this folder. Make sure you have "mysql-connector-java-x.x.xx-bin.jar".
You now have the JBDC driver: relaxed:
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 **.
Now you're ready !!!
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