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:
- 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 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.
- 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 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
- 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 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
- 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
Complete.
Original article title: connecting to MYSQL using java
Connection: http://www.cnblogs.com/soplayer/archive/2007/06/26/796565.html