The correct procedure for connecting Java to the MySQL database

Source: Internet
Author: User
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.

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.

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 JDBC driver mysql-connector-java-5.0.5.zip (the latest version ). 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:

 
 
  1. CREATE DATABASE SCUTCS;

Create a table:

 
 
  1. CREATE TABLE STUDENT
  2. (
  3. SNO CHAR(7) NOT NULL,
  4. SNAME VARCHAR(8) NOT NULL,
  5. SEX CHAR(2) NOT NULL,
  6. BDATE DATE NOT NULL,
  7. HEIGHT DEC(5,2) DEFAULT 000.00,
  8. PRIMARY KEY(SNO)
  9. );

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.

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

Driver name

 
 
  1. String driver = "com.MySQL.jdbc.Driver";

The URL points to the name of the database to be accessed, scutcs

 
 
  1. String url = "jdbc:MySQL://127.0.0.1:3306/scutcs";

Username for MySQL Configuration

 
 
  1. String user = "root";

Password for MySQL Configuration

 
 
  1. String password = "root";
  2. try {

Load driver

 
 
  1. Class.forName(driver);

Continuous MySQL database

 
 
  1. Connection conn = DriverManager.getConnection(url, user, password);
  2. if(!conn.isClosed())
  3. System.out.println("Succeeded connecting to the Database!");

Statement is used to execute SQL statements.

 
 
  1. Statement statement = conn.createStatement();

SQL statement to be executed

 
 
  1. String sql = "select * from student";

Result set

 
 
  1. ResultSet rs = statement.exe cuteQuery (SQL );
  2. System. out. println ("-----------------");
  3. System. out. println ("the execution result is as follows :");
  4. System. out. println ("-----------------");
  5. System. out. println ("student ID" + "\ t" + "name ");
  6. System. out. println ("-----------------");
  7. String name = null;
  8. While (rs. next ()){

Select the sname Column

 
 
  1. 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.

 
 
  1. name = new String(name.getBytes("ISO-8859-1"),"GB2312");

Output result

 
 
  1. System.out.println(rs.getString("sno") + "\t" + name);
  2. }
  3. rs.close();
  4. conn.close();
  5. } catch(ClassNotFoundException e) {
  6. System.out.println("Sorry,can`t find the Driver!");
  7. e.printStackTrace();
  8. } catch(SQLException e) {
  9. e.printStackTrace();
  10. } catch(Exception e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.