MySQL program and database connection Help

Source: Internet
Author: User

public class SQLconnection {
/** Name of connection database */
private static String username;
/** password to connect to database */
private static String userpwd;
/** Path to connection database */
private static String URL;
/**
* Static initialization block, get the necessary parameters of database connection from the properties file

The contents of the *properties file include

*url:jdbc:mysql://127.0.0.1:3306/crm\?useunicode=true&characterencoding=utf-8

*name:root
*pwd:admin


*/
static{
Properties Pro = new properties ();
try {
Pro.load (New FileInputStream ("./src/sql.properties"));
Url=pro.getproperty ("url");
Username=pro.getproperty ("name");
Userpwd=pro.getproperty ("pwd");
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
/**
* Join Database
* @return a digital library connection
*/
public static Connection getconnection () {

Connection conn = null;
try {
Class.forName ("Com.mysql.jdbc.Driver");
conn = Drivermanager.getconnection (URL,USERNAME,USERPWD);
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Return conn;
}
/**
* Close Database connection
* @param conn A database connection
*/
public static void CloseConnection (Connection conn) {
try {
Conn.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

MySQL program and database connection Help

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.