The following article describes how to connect Java to the MySQL database (taking the MySQL database as an example). We will take the MySQL database as an example to describe the actual operation process of correctly connecting Java to the MySQL database, the following is a detailed description of the content.
Of course, you must first install JDK (usually JDK 1.5.x ). Then install MySQL, which is relatively simple and the specific process will not be mentioned. After these two environments are configured, download the latest version of JDBC driver mysql-connector-java-5.0.5.zip ). Decompress the package to any directory. I decompress the package to drive D and add the MySQL-connector-java-5.0.5-bin.jar under its directory to classpath as follows:
My computer-> properties-> advanced-> environment variables, edit classpath in system variables, add D: \ MySQL-connector-java-5.0.5 \ MySQL-connector-java-5.0.5-bin.jar to the end, add ";" before adding this string to distinguish it from the previous classpath. Then confirm.
After the environment is configured, it is very simple. Now, configure MySQL with the username "root" and password "root ". Create a Database on the command line or using an SQL front-end software.
I used SQLyog's front-end software to create a Database.
First create a MySQL database:
- CREATE DATABASE SCUTCS;
Create a 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 data. You can use the SQL statement insert. <表名> Values (value1, value2 ,...);
You can also use SQLyog to perform operations.
OK.
Next, we will compile the. java file to demonstrate how to access the 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
- String url = "jdbc:MySQL://127.0.0.1:3306/scutcs";
Username for MySQL Configuration
- String user = "root";
Password for MySQL Configuration
- String password = "root";
- try {
Load driver
- Class.forName(driver);
Continuous MySQL 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
- ResultSet rs = statement.exe cuteQuery (SQL );
- System. out. println ("-----------------");
- System. out. println ("the execution result is 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
- name = rs.getString("sname");
First, use the ISO-8859-1 character set to decode the name into a byte sequence and store the results 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
- 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 ID name
0104421 weeks
0208123 Wang Yiping
0209120 Wang DaLi
0309119 Li Wei
0309203 Ouyang Merrill Lynch
Haha, it's a success. The above content is an introduction to connecting to the MySQL database using java. I hope you will have some gains.