Connect to Mysql jdbc (control layer)

Source: Internet
Author: User

Package util;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
Import java. SQL. ResultSet;
Public class DBUtil {
Connection conn = null;
PreparedStatement stmt = null;
String Driver = "com. mysql. jdbc. Driver ";
String url = "jdbc: mysql: // 10.137.5.23/mcip? UseUnicode = true & characterEncoding = GBK ";
String user = "mcip ";
String password = "mcip ";
ResultSet rs = null;
// Complete the connection Creation
Public Connection getConnection () throws Exception {
Class. forName (Driver );
If (conn = null ){
Conn = DriverManager. getConnection (url, user, password );
}
Return conn;
}
// Create a statement object
Public PreparedStatement createStatement (String SQL) throws Exception {
Stmt = getConnection (). prepareStatement (SQL );
Return stmt;
}
// Execute the method returned by the result set
Public ResultSet excuteQuery () throws Exception {
Rs = stmt.exe cuteQuery ();
Return rs;
}
// Execute the method without returning the result set
Public int excuteUpdate () throws Exception {
Return stmt.exe cuteUpdate ();
}
// Close the object
Public void close (){
If (rs! = Null) try {rs. close ();} catch (Exception e ){}
If (stmt! = Null) try {stmt. close ();} catch (Exception e ){}
If (conn! = Null) try {conn. close ();} catch (Exception e ){}
}
}

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.