java uses JDBC to connect to mysql database

Personal home page: https://zhangquan1995.github.io

Here the JDK version of java is 1.8.0X and the mysql version is 5.7.

mysql has a user name of root and a password of zhang110.

Download JDBC driver

Download the JDBC driver mysql-connector-java-5.1.46.zip and unzip it to the installation directory of Java (any path will work, but remember the path).

Configure JDBC's environment variables so that a java program anywhere on your computer can call this jar package.

My Computer -> Properties -> Advanced -> Environment Variables -> Classpath, add the unzipped path \mysql-connector-java-5.1.46-bin\mysql-connector-java-5.1.46-bin.jar; to the classpath.

Note: The path must reach the.jar file.

Create a database

Create Database scutcs with SQLyog database management software:

create database scutcs;
create table student
(
sno char(7) not null,
sname varchar(8) not null,
sex char(2) not null,
bdate date not null,
height dec(5,2) default 000.00,
primary key(sno)
);

Then insert some data.

java access database

import java.sql.*;
public class JDBCTest 
{
  public static void main(String[] args)
  {
      // Driver Name
      String driver = "com.mysql.jdbc.Driver";
      // URL points to the database name scutcs to be accessed
      String url = "jdbc:mysql://127.0.0.1:3306/scutcs";
      // User name when MySQL is configured
      String user = "root"; 
      // Password for MySQL configuration
      String password = "zhang110";
      try 
      { 
          // load driver
          Class.forName(driver);
          // Continuous Database
          Connection conn = DriverManager.getConnection(url, user, password);
          if(!conn.isClosed()) 
              System.out.println("Succeeded connecting to the Database!");
          // State is used to execute SQL statement s
          Statement statement = conn.createStatement();
          //Execute SQL statement
          /*
          Statement There are three ways to execute SQL statements:
          1.execute()Any SQL statement can be executed, returning a Boolean value
            Returns true if the first result after execution is resultSet, otherwise false
          2.executeQuery()Execute the select statement to return the result set of the query
          3.executeUpdate()Used to execute DML statements, returning an integer
            Represents the number of records affected by the SQL statement
          */
          // SQL statement to execute
          String sql = "select * from student";
          // Result Set
          ResultSet rs = statement.executeQuery(sql);
          System.out.println("-----------------");
          System.out.println("The results are as follows:");
          System.out.println("-----------------");
          System.out.println(" School Number" + "\t" + " Full name");
          System.out.println("-----------------");
          String name = null;
          while(rs.next()) 
          {
              // Select the sname column
              name = rs.getString("sname");
              // First decode the name into a byte sequence using the ISO-8859-1 character set and store the result in a new byte array.
              // Then decode the specified byte array using the GB2312 character set
              //name = new String(name.getBytes("ISO-8859-1"),"GB2312");
              // Output Results
              System.out.println(rs.getString("sno") + "\t" + name);
          }
          rs.close();
          conn.close();
      }
      catch(ClassNotFoundException e)
      {
          System.out.println("Sorry,can't find the Driver!"); 
          e.printStackTrace();
      }
      catch(SQLException e)
      {
          e.printStackTrace();
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
  }
}

Compile and execute the JDBCTest.java file as follows:

Succeeded connecting to the Database!

School Number Full name
201801 Zhang San
201802 Li Si
201803 Wang Hong
201804 Von Lily

Tags: Java MySQL Database SQL

Posted on Sat, 28 Mar 2020 10:54:47 -0700 by lalov1