[Java] Connect to MySQL


MySQL : 5.7 Java : 8.1 Mac Eclipse

Clear the following conditions before starting

-MySQL already has a table etc. ・ You can connect without difficulty at the terminal etc.

Download JDBC

Download JDBC for MySQL https://dev.mysql.com/downloads/connector/j/

** Download "Platform Independent (Architecture Independent), Compressed TAR Archive" ** スクリーンショット 2019-10-21 17.25.07.png

Because there is mysql-connector-java-5.1.48-bin.jar in the unzipped file Put in / Tomcat / lib of tomcat (In my case, / Users / namari / apache-tomcat-9.0.27 / lib)

Try to connect

Create SqlTest.java appropriately in the project.


package chapter14;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

 * Servlet implementation class SqlTest
public class SqlTest extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(
	    HttpServletRequest request,
	    HttpServletResponse response
	) throws ServletException, IOException {

	    //This time, the book database on localhost
	    String url = "jdbc:mysql://localhost/book";

	    	Connection conn = DriverManager.getConnection(url, "username", "password");

	    	//Processing for database
	    	msg = "ok";
	    } catch (SQLException | ClassNotFoundException e){
	    	msg = "NG";


Recommended Posts

[Java] Connect to MySQL
Connect to MySQL 8 with Java
Connect from Java to MySQL using Eclipse
Connect to Aurora (MySQL) from a Java application
Connect from Java to PostgreSQL
Connect to DB with Java
[Android] Connect to MySQL (unfinished)
Operation to connect multiple Streams @Java
[Java] Introduction to Java
Introduction to java
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Kotlin's improvements to Java
From Java to Ruby !!
[Java / PostgreSQL] Connect the WEB application to the database
How to install MySQL
Introduction to java command
[MySQL + Java] Numbering procedure
Solution that gives an error when trying to connect to DB (MySQL) in Java
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
[Java] [SQL Server] Connect to local SQL Server 2017 using JDBC for SQL Server
How to connect MySQL / MariaDB + HikariCP with Liferay 7 / DXP
To connect from Spring to MySQL on virtual server (unsolved)
I started MySQL 5.7 with docker-compose and tried to connect
Can't connect to local MySQL server through socket'/tmp/mysql.sock' (2) error
I want to connect to Heroku MySQL from a client
[Java] How to use Map
How to lower java version
Migration from Cobol to JAVA
[Java] How to use Map
Convert Java Powerpoint to XPS
Java adds table to PDF
How to uninstall Java 8 (Mac)
Java to play with Function
Java --How to make JTable
How to use java Optional
New features from Java7 to Java8
How to minimize Java images
How to write java comments
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] Introduction to lambda expressions
[Java] How to use string.format
Shell to kill Java process
How to use Java Map
How to set Java constants
[java] Reasons to use static
Connect CentOS 7 to L2TP VPN
How to use Java variables
[Java] Introduction to Stream API
Java8 to start now ~ Optional ~
How to convert Java radix
[Java] Convert ArrayList to array
Connect to oracle with eclipse!
Java thread to understand loosely
[Java] How to implement multithreading
[Java] How to use Optional ①
From Ineffective Java to Effective Java
How to initialize Java array