Java file to demonstrate how to access the MySQL database

Source: Internet
Author: User

Java file to demonstrate how to access the MySQL database.

Note: Create database on the command line or with a SQL front-end software.

Create the database First:

CREATE DATABASE Scutcs;

Next, create the 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 the data with the SQL statement insert into < table name > values (value1, value2, ...);

Java file to demonstrate how to access the MySQL database.

1 ImportJava.sql.*;2 3  Public classJdbctest {4 5  Public Static voidMain (string[] args) {6 7            //driver name8String Driver = "Com.mysql.jdbc.Driver";9 Ten            //the URL points to the database name you want to access Scutcs OneString url = "Jdbc:mysql://127.0.0.1:3306/scutcs"; A  -            //user name when MySQL is configured -String user = "root";  the  -            //password for MySQL configuration -String password = "root"; -  +            Try {  -             //Load Driver + Class.forName (driver); A  at             //Continuous Database -Connection conn =drivermanager.getconnection (URL, user, password); -  -             if(!conn.isclosed ()) -SYSTEM.OUT.PRINTLN ("Succeeded connecting to the database!"); -  in             //statement used to execute SQL statements -Statement Statement =conn.createstatement (); to  +             //the SQL statement to execute -String sql = "SELECT * FROM Student"; the  *             //result set $ResultSet rs =statement.executequery (SQL);Panax Notoginseng  -System.out.println ("-----------------"); theSystem.out.println ("The execution results are as follows:"); +System.out.println ("-----------------"); ASystem.out.println ("School Number" + "T" + "name")); theSystem.out.println ("-----------------"); +  -String name =NULL; $  $              while(Rs.next ()) { -      -              //Select sname This column of data theName = Rs.getstring ("sname"); -     Wuyi              //first, use the iso-8859-1 character set to decode name into a sequence of bytes and store the result in a new byte array.  the              //the specified byte array is then decoded using the GB2312 character set -Name =NewString (Name.getbytes ("iso-8859-1"), "GB2312"); Wu  -              //Output Results AboutSystem.out.println (rs.getstring ("sno") + "\ T" +name); $             } -  - rs.close (); - conn.close (); A  +}Catch(ClassNotFoundException e) { the  -  $System.out.println ("Sorry,can ' t find the driver!");  the e.printstacktrace (); the  the  the}Catch(SQLException e) { -  in  the e.printstacktrace (); the  About  the}Catch(Exception e) { the  the  + e.printstacktrace (); -  the Bayi            }  the }  the}

The results of the operation are shown below:

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
================================
1000001 Luo Luo
1000023 Yingle
1000020 Wang Dawei
1000019 Li Chuang
1000063 Ouyang Scarlett

Haha, success, and finally achieved the desired effect!!!!!!!!!

Java file to demonstrate how to access the 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.