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;
}
}