JDBC database programming operations (for MySQL) _ MySQL

Source: Internet
Author: User
JDBC database programming operation class (for MySQL) package com. msit. Uitll;

Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;

/**
* Database help class connecting to MySQL database
*
* @ Author ruanj_000
*
*/
Public class SQLHelp {

Private static String dirverName = "com. mysql. jdbc. Driver ";
Private static String url = "jdbc: mysql: // 127.0.0.1: 3306/misidb ";
Private static String user = "root ";
Private static String password = "root ";
Private static Connection connection = null;

// Compile static code blocks (faster than constructors) for loading driver classes
Static {
Try {
Class. forName (dirverName );
// Create a connection object

} Catch (ClassNotFoundException e ){
E. printStackTrace ();
}
}

/**
* Obtain the connection object
*
* @ Return
*/
Public static Connection getConnection (){
Try {
Connection = DriverManager. getConnection (url, user, password );
} Catch (SQLException e ){
E. printStackTrace ();
}
Return connection;
}

/**
* SQL statements used to add, delete, and modify
*
* @ Param SQL
* @ Return: number of affected rows
*/
Public static int ExecuteNonQuery (String SQL ){
Int num = 0;
Connection conn = getConnection ();
Statement sta = null;
Try {
Sta = conn. createStatement ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Try {
Num = sta.exe cuteUpdate (SQL );
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
Close (null, sta, conn );
}
Return num;
}

/**
* Query statement execution method
* @ Param SQL SQL SQL script language for data query
* @ Return result set
*/
Public static ResultSet ExecuteQuery (String SQL ){
ResultSet rs = null;
Connection con = getConnection ();
Statement sta = null;
Try {
Sta = con. createStatement ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Try {
Rs1_sta.exe cuteQuery (SQL );
} Catch (SQLException e ){
E. printStackTrace ();
}
Finally {
// Close (rs, sta, con );
}
Return rs;
}

/**
* Method for disabling resources
*
* @ Param rs
* Result set
* @ Param sta
* Execution object
* @ Param conn
* Connection object
*/
Private static void Close (ResultSet rs, Statement sta, Connection conn ){
If (rs! = Null ){
Try {
Rs. close ();
} Catch (SQLException e ){
System. out. println ("An error occurred while disabling the data result set ..! ");
E. printStackTrace ();
}
}
If (sta! = Null ){
Try {
Sta. close ();
} Catch (SQLException e ){
System. out. println ("An exception occurred while shutting down the execution object ,,! ");
E. printStackTrace ();
}
}
If (conn! = Null ){
Try {
Conn. close ();
} Catch (SQLException e ){
System. out. println ("An error occurred while closing the data connection ,,! ");
E. printStackTrace ();
}
}
Rs = null; sta = null; conn = null;
}

/**
* Method for executing pre-compiled SQL statements
* @ Param SQL
* @ Param prgm
* @ Return
*/
Public static int pstm (String SQL, String prgm []) {
Int num = 0;
PreparedStatement pstm = null;
Try {
Pstm = getConnection (). prepareStatement (SQL );
For (int I = 1; I <= prgm. length; I ++ ){
Pstm. setString (I, prgm [i-1]);
}
Num1_p0000.exe cuteUpdate ();
Pstm. close ();
Connection. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Return num;
}

}

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.