Using Java-only code to make MySQL connections

Source: Internet
Author: User

Set up a database

1. Click Connect-->mysql: Enter the connection name, hostname/ IP address, port, user name, password (omitted without password), then click OK, the established table is gray to indicate off state, double click to open

2. re-create a database

Right-click the table--New database--fill in the database name, set to UTF8, collation can not write, default is the first:utf8-general-ci

Connect to a database using pure code

Steps:

< Span style= "font-family: the song Body;" >1. Load MySQL driver:

The default is to load the JVM (Java Virtual machine) driver, call the static method Class.forName (String name) for the Java.lang.class class to achieve

2. Connect the database statement:

You need to send a Java.sql.DriverManager request to get the Connection object :drivermanager. getconnection(String URL, Properties info)

parameter URL: Protocol: Sub-Protocol: Data source identification; Where the protocol is JDBC, the sub-protocol is the MySQL database management name, the data source ID is your IP or port number

Parameter Info: usually contains user name and password

3. Close Resources

 Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//1. Load the database driver        Try{class.forname ("Com.mysql.jdbc.Driver"); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }                //2. Establish a connection to get the connection objectConnection conn =NULL; Try {
Conn= Drivermanager.getconnection ("Jdbc:mysql:///epet", "root", "root"); System.out.println ("Connection succeeded");
} Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } //3. Close Resources Try{conn.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}

Using Java-only code to make MySQL connections

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.