Steps for connecting Java to the MYSQL database

Source: Internet
Author: User

This article mainly describes the actual operation steps of connecting Java to the MYSQL database (taking MySQL as an example). We use related instances to introduce the actual operation process of connecting Java to the MYSQL database, the following describes the main content of the article.

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 it to drive D and add the mysql-connector-java-5.0.5-bin.jar under its directory to classpath,

The details are as follows: "My computer"-> "properties"-> "advanced"-> "environment variable", edit classpath in the system variable, and D: \ mysql-connector-java-5.0.5 \ mysql-connector-java-5.0.5-bin.jar to the end, add ';' before adding this string to separate it from the previous classpath. Then confirm.

After the environment is configured, it is very simple. Now, configure Java to connect to MySQL, set the user name to "root", and the password to "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.

Create a database first:

 
 
  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 into <Table Name> values (value1, value2 ,...);

You can also use SQLyog to perform operations.

OK.

Next, we will compile the. java file to demonstrate how to access Java to connect to the MySQL database.

 
 
  1. import java.sql.*;   
  2. public class JDBCTest {   
  3. 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 used to connect to the MySQL configuration in Java

String password = "root ";

Try {

// Load the 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

 
 
  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

Name = rs. getString ("sname ");

// First, use the ISO-8859-1 character set to decode the name into a byte sequence 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

 
 
  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

Complete.

Original article title: connecting to MYSQL using java

Connection: http://www.cnblogs.com/soplayer/archive/2007/06/26/796565.html

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.