How to connect JDBC to MySQL 5.7

How to connect JDBC to MySQL 5.7

1. First prepare the MySQL and Eclipse environment. After the environment is set up, download the JDBC driver package from the Eclipse official website. The download address is http://dev.mysql.com/downloads/connector/j/

2. Take out mysql-connector-java-5.1.31-bin.jar from the downloaded file, put it in the project, and import the path

Method: Right-click the project name->Build Path->Configure Build Path, select Add External JAR... Find the location of mysql-connector-java-5.1.31-bin.jar, and then load the driver package into the project.

3. Write an example to test it

package testmysql; 
import java.sql.*; 
public class Test { 
 
  public static void main(String[] args) { 
    String driver = "com.mysql.jdbc.Driver"; 
    String URL = "jdbc:mysql://localhost:3306/student"; 
    Connection con = null; 
    try 
    { 
      Class.forName(driver); 
    } 
    catch(java.lang.ClassNotFoundException e) 
    { 
      System.out.println("Connect Successfully."); 
      System.out.println("Cant't load Driver"); 
    } 
    try   
    {                                         
      con=DriverManager.getConnection(URL,"root","root"); 
      System.out.println("Connect Successfully."); 
    }  
    catch(Exception e) 
    { 
      System.out.println("Connect fail:" + e.getMessage()); 
    } 
  } 
}

After connecting to the database, you can query the database table according to the content in the table. First, there must be content in the table. After entering some information into the table, you can use SQL language to query

import java.sql.*;  
public class Main {  
  
  public static void main(String[] args) {  
    String driver = "com.mysql.jdbc.Driver";  
    String URL = "jdbc:mysql://localhost:3306/xiaolu";  
    Connection con = null; 
    ResultSet rs = null; 
    Statement st = null; 
    String sql = "select * from student"; 
    try  
    {  
      Class.forName(driver);  
    }  
    catch(java.lang.ClassNotFoundException e)  
    {  
      // System.out.println("Connect Successfully.");  
      System.out.println("Cant't load Driver");  
    }  
    try    
    {                                          
      con=DriverManager.getConnection(URL,"root","root");  
      st=con.createStatement(); 
      rs=st.executeQuery(sql); 
      if(rs!=null) { 
        ResultSetMetaData rsmd = rs.getMetaData(); 
        int countcols = rsmd.getColumnCount(); 
        for(int i=1;i<=countcols;i++) { 
          if(i>1) System.out.print(";"); 
          System.out.print(rsmd.getColumnName(i)+" "); 
        } 
        System.out.println(""); 
        while(rs.next()) { 
          System.out.print(rs.getString("sno")+" "); 
          System.out.print(rs.getString("sname")+" "); 
          System.out.print(rs.getString("ssex")+" "); 
          System.out.print(rs.getString("sage")+" "); 
          System.out.println(rs.getString("sdept")+" "); 
        } 
      } 
      //System.out.println("Connect Successfully.");  
      System.out.println("ok"); 
      rs.close(); 
      st.close(); 
      con.close(); 
    }   
    catch(Exception e)  
    {  
      System.out.println("Connect fail:" + e.getMessage());  
    }  
  }  
}

This is the end of the article about JDBC connection to MySQL 5.7. For more information, please check other related articles on 123WORDPRESS.COM.

You may also be interested in:
  • Java jdbc connects to mysql database to implement add, delete, modify and query operations
  • JDBC connects to MySql database steps and query, insert, delete, update, etc.
  • How to use JDBC to connect to MYSQL database in JSP
  • Java uses jdbc to connect to the database tool class and jdbc to connect to mysql data example
  • JDBC connection to MySQL instance detailed explanation
  • Summary of problems that may occur when using JDBC to connect to Mysql database
  • mysql jdbc connection steps and common parameters
  • Problems with JDBC connecting to MySQL
  • Java connects to Mysql database via JDBC
  • Six-step example code for JDBC connection (connecting to MySQL)

<<:  SMS verification code login function based on antd pro (process analysis)

>>:  Three Ways to Lock and Unlock User Accounts in Linux

Recommend

How to set horizontal navigation structure in Html

This article shares with you two methods of setti...

Detailed tutorial on installing ElasticSearch 6.4.1 on CentOS7

1. Download the ElasticSearch 6.4.1 installation ...

Common structural tags in XHTML

structure body, head, html, title text abbr, acro...

Mysql backup multiple database code examples

This article mainly introduces the Mysql backup m...

Docker container log analysis

View container logs First, use docker run -it --r...

8 Reasons Why You Should Use Xfce Desktop Environment for Linux

For several reasons (including curiosity), I star...

How to find the specified content of a large file in Linux

Think big and small, then redirect. Sometimes Lin...

NodeJs high memory usage troubleshooting actual combat record

Preface This is an investigation caused by the ex...

Solution to installing vim in docker container

Table of contents The beginning of the story Inst...

Web front-end performance optimization

Web front-end optimization best practices: conten...

Vue implementation example using Google Recaptcha verification

In our recent project, we need to use Google robo...

How to set up URL link in Nginx server

For websites with an architecture like LNMP, they...

Detailed steps to configure my.ini for mysql5.7 and above

There is no data directory, my-default.ini and my...

A brief introduction to React

Table of contents 1. CDN introduction 1.1 react (...