Java Connection MySQL Database

Source: Internet
Author: User

Lead Jar Pack (copy sticker)

Then configure the build path

Find the jar package in the project

Load Driver

 Public classDbutil {Private Static FinalString dbdriver = "Com.mysql.jdbc.Driver"; Private Static FinalString Dburl = "Jdbc:mysql://127.0.0.1:3306/emp_dept"; Private Static FinalString USER = "root"; Private Static FinalString PASSWORD = "123456";  Public Static voidMain (string[] args)throwsException {//load the database driver Class.forNameClass.forName (Dbdriver);

Connecting to a database

The connection data must depend on the DriverManager class to complete

Private Static Final String Dburl = "Jdbc:mysql://127.0.0.1:3306/emp_dept";//  jdbc  database  native Name table name private Static Final String USER = "root";  User name privatestaticfinal String PASSWORD = "123";  Password

DriverManager method getconnection, return connection; in JDBK, each database connection is to use a connection package, so as long as the occurrence of connection, it means to link the database;

Connection conn = drivermanager.getconnection (Dburl, USER, PASSWORD);

Verify that there is no connection, just see that Conn is not NULL, it represents the connection

SYSTEM.OUT.PRINTLN (conn);

Already connected

Close the database

Conn.close ();

Java Connection MySQL Database

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.