The detailed steps of java connecting mysql database
Connection Description:
a. The driver of mysql connection
b. mysql database installation
c,eclipse
e. Whether the database service is open (control panel - management tool - Service - find the corresponding service of mysql)
f. Create a database
1. Create a new java project and then a new folder -- libs (used to put various external packages)

2. Add a package to connect mysql database in the package
This is the download connection: http://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.0.8.zip
After downloading, you get a compressed package

After decompression, open the jar package with the red line around it, and then copy and paste it under the libs file of our java project

3. jar package on the build path
In eclipse
a: Click item - select attribute

b: Add
After opening the properties, click the java build path
Click Add jar, select the jar package under your project, confirm all the time, and finally add it

4. Connecting mysql database in java project
Create two new Class files in the java project package,
My names are MainClass and SqlConnection

Open SqlConnection
Add the following code:
- public class SqlConnection {
- //Here is the SqlConnection class
- /*
- *java Connect to mysql database
- *1,load driver
- *2,Database connection string "jdbc:mysql://localhost:3306 / database name?"
- *3,Database login
- *3,Database login password
- */
- private static final String URL="jdbc:mysql://localhost:3306/deom?";//Database connection string, where deom is the database name
- private static final String NAME="admin";//Login name
- private static final String PASSWORD="13245";//Password
- public void TheSqlConnection()
- {
- //1. Loading drive
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println("Failed to load driver successfully, please check whether to import driver!");
- //Add a println. If the driver is loaded abnormally, check whether the driver is added or whether the driver string is wrong
- e.printStackTrace();
- }
- Connection conn = null;
- try {
- conn = DriverManager.getConnection(URL, NAME, PASSWORD);
- System.out.println("Database connection succeeded!");
- } catch (SQLException e) {
- System.out.println("Failed to get database connection!");
- //Add a println. If the connection fails, check whether the connection string or login name and password are wrong
- e.printStackTrace();
- }
- //Close database after opening
- if(conn!=null)
- {
- try {
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- conn=null;
- }
- }
- }
- }
Calling SqlConnection in MainClass
- package com.king.sqlCon;
- public class MainCalss {
- public static void main(String[] args) {
- //TODO auto generated method stub
- new SqlConnection().TheSqlConnection();
- }
- }