Connect to the MySql database using java in Singleton mode

Source: Internet
Author: User
Package com. adrop. util;
Import java. SQL .*;
Public class DBManager {
// User name
Private String user = "";
// Password
Private String password = "";
// Host
Private String host = "";
// Database name
Private String database = "";
// Private DBManager dbm = null;
/*
Private String url = "jdbc: mysql: //" + host + "/" + "useUnicode = true & characterEncoding = GB2312 ";
*/
Private String url = "";
Private Connection con = null;
Statement stmt;
/**
* Private constructor ensures that external entities cannot be instantiated and can only be provided by DBManager itself
* You can only have one instance.
* The connection is obtained based on the host, database name, database user name, and database user password.
* @ Param host String
* @ Param database String
* @ Param user String
* @ Param password String
*/
Private DBManager (String host, String database, String user, String password ){
This. host = host;
This. database = database;
This. user = user;
This. password = password;
// Display Chinese characters
This. url = "jdbc: mysql: //" + host + "/" + database +
"? UseUnicode = true & characterEncoding = GB2312 ";
Try {
Class. forName ("org. gjt. mm. mysql. Driver ");
}
Catch (ClassNotFoundException e ){
System. err. println ("class not found:" + e. getMessage ());
}
Try {
Con = DriverManager. getConnection (this. url, this. user, this. password );
// The connection type is ResultSet. TYPE_SCROLL_INSENSITIVE and ResultSet. CONCUR_READ_ONLY.
Stmt = con. createStatement (ResultSet. TYPE_SCROLL_INSENSITIVE,
ResultSet. CONCUR_READ_ONLY );
}
Catch (SQLException ){
System. err. println ("SQL exception:" + a. getMessage ());
}

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.