JDBC Connection MySQL

Source: Internet
Author: User

1, add jar package in Lib;

2, DBHelper class:

public class dbhelper{

Private static final String Driver = "Com.mysql.jdbc.Driver"; Database-driven

Private static final String URL = "Jdbc:mysql://localhost:3306/shopping?useunicon = True&ccharacterencoding=utf-8" ; Connection Address

Private static final String username = "root"; Database user Name

Private static final String password = ""; Database Password

private static Connection coon = null;

The static code block is responsible for loading the driver

static{

try{

Class.forName (driver);

}

catch (Exception ex) {

Ex.printstacktrace ();

}

}

Singleton mode Returns a database connection object

public static Connection getconnection () throws exception{

if (conn = = null) {

Connection con = drivermanager.getconnection (Url,username,password);

Return conn;

}

Return conn;

}

}

JDBC Connection MySQL

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.