Java connection to the MYSQL database implementation steps

  • 2020-04-01 02:06:54
  • OfStack

This article mainly describes to you is the Java connection MYSQL database (take MYSQL as an example) the actual operation steps, we are related to the example of the way to lead to the actual operation flow of Java connection MYSQL database, the following is the main content of the article description.

Of course, the JDK(typically jdk1.5.x) must be installed first. Then install MySQL, these are relatively simple, the specific process will not say. After configuring the two environments, download the JDBC driver mysql-connector-java-5.0.5.zip (this is the latest version). Then unzip it to any directory. I unzip to disk D and add mysql-connector-java-5.0.5-bin.jar in its directory to my classpath.

The details are as follows: "my computer" -> "Attributes" -> "Premium" -> "Environment variable", edit classpath in the system variable, add D:\mysql-connector-java-5.0.5\mysql-connector-java-5.0.5-bin.jar to the end, add "; "before adding the string. To distinguish it from the previous classpath. And then determine.

The environment is configured and simple. Now, configure the Java connection to MySQL with the user name "root" and password "root". Create the Database on the command line or with an SQL front-end software.

I created the Database using SQLyog's front-end software.

First create the database:


CREATE DATABASE SCUTCS; 

Next, create the table:


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 the data, you can use an SQL statement to insert into < The table name > Values (value1, value2,...). ;

You can also use SQLyog

All right, we're done.

Next, let's write a. Java file to demonstrate how to access a Java connection to a MySQL database.


import java.sql.*;    
public class JDBCTest {    
public static void main(String[] args){  

Driver name
String driver = "com. Mysql. JDBC driver";

// the URL points to the name of the database to be accessed, scutcs
The String url = "JDBC: mysql: / / 127.0.0.1:3306 / scutcs";

// username when MySQL was configured
String user = "root";

// password for Java connection to MySQL configuration

String password = "root";

Try {
// load the driver

Class.forname (driver);

// continuous database
The Connection conn = DriverManager. GetConnection (url, user, password);

If (! Conn. IsClosed ())
System.out.println(" succeeding connecting to the Database!" );

// statement is used to execute SQL statements
The Statement Statement = conn. CreateStatement ();

// SQL statement to execute
String SQL = "select * from student";

The result set


ResultSet rs = statement.executeQuery(sql);   
System.out.println("-----------------");   
System.out.println(" The results are shown below :");   
System.out.println("-----------------");   
System.out.println("  Student id " + "t" + "  The name ");   
System.out.println("-----------------");   
String name = null;   
while(rs.next()) {  

Select the sname column
Name = rs. Get string (" sname ");
// first decode the name into a sequence of bytes using the iso-8859-1 character set and store the result in a new byte array.
// then the specified byte array is decoded using the GB2312 character set
Name = new String (the name getBytes (" ISO - 8859-1 "), "GB2312");
// output result


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();    
}    
}    
}  

Let's run it and see what it looks like:
D: \ testjdbc> Javac JDBCTest. Java
D: \ testjdbc> Java JDBCTest
Succeeding connecting to the Database!
-----------------------
The execution results are as follows:
-----------------------
Student id name
-----------------------
0104421 Zhou Yuanhang
0208123 Wang Yiping
0209120 da-li wang
0309119 li d
0309203 ouyang merrill lynch
Finished.


Related articles: