Implementation steps of Java connection MySQL database _java

Source: Internet
Author: User

This article is mainly to describe the Java connection to the MySQL database (for example, MySQL) the actual operation steps, we are related to the case of the way to draw Java connection to the MySQL database of the actual operation process, the following is the main content of the article description.

Of course, the first thing to install is a JDK (typically jdk1.5.x). Then install MySQL, these are relatively simple, the specific process will not say. After configuring both environments, download the JDBC driver Mysql-connector-java-5.0.5.zip (This is the latest version). Then unzip it to any directory. I unzipped it to D and then add the Mysql-connector-java-5.0.5-bin.jar to the classpath in the directory,

as follows: "My Computer"-> "Properties"-> "Advanced"-> "Environment variables", edit classpath in system variables, 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 from the previous Classpath area. Then determine.

The environment is well configured, very simple. Now, first configure Java connection MySQL, set its user name is "root", the password is "root". Create the database at the command line or with a SQL front-end software.

I use SQLyog front-end software to create the database.

To create a database first:

Copy Code code as follows:

CREATE DATABASE Scutcs;

Next, create the table:

Copy 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 the data, you can insert into < table name > values (value1, value2, ...) with SQL statements;

You can also use SQLyog to manipulate

All right, here we are.

Next, we'll write a. java file to demonstrate how to access the Java connection MySQL database.

Copy 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 name of the database to be accessed Scutcs
String url = "Jdbc:mysql://127.0.0.1:3306/scutcs";

User name when MySQL is configured
String user = "root";

Password for Java connection 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 used to execute SQL statements
Statement Statement = Conn.createstatement ();

The SQL statement to execute
String sql = "SELECT * from student";

Result set

Copy Code code as follows:

ResultSet rs = statement.executequery (SQL);
System.out.println ("-----------------");
System.out.println ("The results of the execution are as follows:");
System.out.println ("-----------------");
System.out.println ("School Number" + "T" + "name");
System.out.println ("-----------------");
String name = NULL;
while (Rs.next ()) {

Select sname This column of data
Name = Rs.getstring ("sname");
First decodes name into a byte sequence using the iso-8859-1 character set and stores the result in a new byte array.
The specified byte array is then decoded using the GB2312 character set
name = new String (name.getbytes ("iso-8859-1"), "GB2312");
Output results

Copy 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, we'll run a look at the effect:
D:\testjdbc>javac Jdbctest.java
D:\testjdbc>java Jdbctest
Succeeded connecting to the database!
-----------------------
The results of the execution are as follows:
-----------------------
Name of the school number
-----------------------
0104421 Weeks long journey
0208123 Wang Yiping
0209120 King vigorously
0309119 Levi
0309203 Ouyang Merrill
Done.

Related Article

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.