The detailed steps of java connecting mysql database

Keywords: Database MySQL Java JDBC

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:
  1. public class SqlConnection {  
  2. //Here is the SqlConnection class  
  3.   
  4.         /* 
  5.         *java Connect to mysql database 
  6.         *1,load driver 
  7.         *2,Database connection string "jdbc:mysql://localhost:3306 / database name?" 
  8.         *3,Database login 
  9.         *3,Database login password 
  10.         */  
  11.   
  12.     private static final String URL="jdbc:mysql://localhost:3306/deom?";//Database connection string, where deom is the database name
  13.     private static final String NAME="admin";//Login name  
  14.     private static final String PASSWORD="13245";//Password  
  15.       
  16.     public void TheSqlConnection()  
  17.     {  
  18.         //1. Loading drive  
  19.         try {  
  20.             Class.forName("com.mysql.jdbc.Driver");  
  21.         } catch (ClassNotFoundException e) {  
  22.             System.out.println("Failed to load driver successfully, please check whether to import driver!");  
  23.                         //Add a println. If the driver is loaded abnormally, check whether the driver is added or whether the driver string is wrong  
  24.             e.printStackTrace();  
  25.         }  
  26.         Connection conn = null;  
  27.         try {  
  28.             conn = DriverManager.getConnection(URL, NAME, PASSWORD);  
  29.                 System.out.println("Database connection succeeded!");  
  30.         } catch (SQLException e) {  
  31.             System.out.println("Failed to get database connection!");  
  32.                         //Add a println. If the connection fails, check whether the connection string or login name and password are wrong  
  33.             e.printStackTrace();  
  34.         }  
  35.                //Close database after opening  
  36.         if(conn!=null)  
  37.         {  
  38.             try {  
  39.                 conn.close();  
  40.             } catch (SQLException e) {  
  41.                 // TODO Auto-generated catch block  
  42.                 e.printStackTrace();  
  43.                 conn=null;  
  44.             }  
  45.         }  
  46.     }  
  47. }  
Calling SqlConnection in MainClass
  1. package com.king.sqlCon;  
  2.   
  3. public class MainCalss {  
  4.   
  5.     public static void main(String[] args) {  
  6.         //TODO auto generated method stub  
  7.         new SqlConnection().TheSqlConnection();  
  8.     }  
  9.   
  10. }  

Posted by dsjoes on Thu, 30 Apr 2020 07:09:14 -0700