Reprint: The correct operation flow of Java connection MySQL database

Source: Internet
Author: User

Reprint website: http://www.bitscn.com/pdb/mysql/201005/186551.html

The following article is mainly about the Java connection MySQL database (for example, MySQL database), we mainly take the MySQL database as an example of the correct Java connection to the MySQL database of the actual operation process, the following is the detailed description of its contents.

Of course, the first thing to install is the JDK (typically jdk1.5.x). Then install MySQL, these are relatively simple, the specific process will not say. After configuring these two environments, download the JDBC driver Mysql-connector-java-5.0.5.zip (This is the latest version). Then unzip it to either directory. I was extracted to the D drive, and then add the Mysql-connector-java-5.0.5-bin.jar in its directory to Classpath, as follows:

"My Computer", "Properties", "Advanced", "Environment variable", edit classpath in the system variable, will D:\MySQL-connector-java-5.0.5\ Mysql-connector-java-5.0.5-bin.jar to the end, add ";" before adding the string to separate from the previous Classpath area. And then determine.

The environment is well configured, very simple.

Now, configure MySQL with the username "root" and the password "root". Create database on the command line or with a SQL front-end software.

I use SQLyog's front-end software to create database.

First create a MySQL database:

    1. CREATE DATABASE Scutcs;

Next, create the table:

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

Then insert the data with the SQL statement insert into < table name > values (value1, value2, ...);

You can also use SQLyog to manipulate

All right, I've created it.

Next, let's write a. 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) {   

The driver name is string driver = "com.  MySQL.jdbc.Driver "; The URL points to the database name to be accessed scutcsstring url = "Jdbc:mysql://127.0.0.1:3306/scutcs";  MySQL configuration username String user = "root";   MySQL configuration password 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 used to execute SQL statements Statement statement = Conn.createstatement (); SQL statement to execute string sql = "SELECT * from student";  Result set ResultSet rs = statement.executequery (SQL);  System.out.println ("Execution results are as follows:");  System.out.println ("School number" + "\ T" + "name"); String name = NULL; while (Rs.next ()) {//select sname This column of data name = Rs.getstring ("sname");//Use the iso-8859-1 character set first to decode name into a sequence of bytes 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 ();   }

Now let's run a look at the effect:

D:\testjdbc>javac Jdbctest.java

D:\testjdbc>java Jdbctest

Succeeded connecting to the database!
The execution results are as follows:

Name of the study number

0104421 weeks Excursion

0208123 Wang Yiping

0209120 Wang vigorously

0309119 Levi

0309203 Ouyang Merrill

Haha, success, the above related content is the Java connection MySQL database Introduction, hope you can have some gains.

Reprint: The correct operation flow of Java connection MySQL database

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.