This article mainly describes the actual operation steps of connecting Java to MYSQL database (taking MySQL as an example). We use relevant examples to introduce the actual operation process of connecting Java to MYSQL database. The following is the main content description of the article.
Of course, you must first install JDK (usually JDK1.5.X). Then install MySQL, which is relatively simple, so I won’t go into the specific process. After configuring these two environments, download the JDBC driver mysql-connector-java-5.0.5.zip (this is the latest version). Then extract it to any directory. I unzipped it to the D drive, and then added the mysql-connector-java-5.0.5-bin.jar in its directory to the classpath.
The details are as follows: "My Computer" -> "Properties" -> "Advanced" -> "Environment Variables", edit the classpath in the system variables, and change D:/mysql-connector-java-5.0.5/mysql-connector- java-5.0.5-bin.jar is added to the end, and ";" must be added before adding this string to distinguish it from the previous classpath. Then OK.
The environment is configured and it's very simple. Now, first configure Java to connect to MySQL, set its user name to "root" and its password to "root". Create a Database from the command line or using a SQL front-end software.
I used SQLyog's front-end software to create the Database.
Create the database first:
Copy the code code as follows:
CREATE DATABASE SCUTCS;
Next, create the table:
Copy the code code as follows:
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 data, you can use the SQL statement insert into <table name> values (value1, value2, ...);
You can also use SQLyog to operate
Okay, it’s created.
Next, let's write a .java file to demonstrate how to access Java to connect to the MySQL database.
Copy the code code as follows:
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 database name scutcs to be accessed
String url = "jdbc:mysql://127.0.0.1:3306/scutcs";
//Username during MySQL configuration
String user = "root";
//Password when Java connects to MySQL configuration
String password = "root";
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!");
// statement is used to execute SQL statements
Statement statement = conn.createStatement();
//SQL statement to be executed
String sql = "select * from student";
result set
Copy the code code as follows:
ResultSet rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("The execution results are as follows:");
System.out.println("-----------------");
System.out.println("Student ID" + "/t" + "Name");
System.out.println("-----------------");
String name = null;
while(rs.next()) {
Select the sname column data
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 use the GB2312 character set to decode the specified byte array
name = new String(name.getBytes("ISO-8859-1"),"GB2312");
//output result
Copy the code code as follows:
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();
}
}
}
Next, let’s run it to see the effect:
D:/testjdbc>javac JDBCTest.java
D:/testjdbc>java JDBCTest
Succeeded connecting to the Database!
-----------------------
The execution result is as follows:
-----------------------
Student number and name
-----------------------
0104421 Zhou Yuanxing
0208123 Wang Yiping
0209120 Wang Dali
0309119 Levi
0309203 Ouyang Meilin
Done.