My Java-linked database and toolkit for adding, deleting, querying, and modifying the database-source code development kit
Package com. msit. util;
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 tool link MySql database
*
* @ Author Administrator
*
*/
Public class SqlHelper {
// Link the required data (the data is directly written to the encrypted data file ).
Private static String url = "jdbc: mysql: // 127.0.0.1: 3306/msitdb ";
Private static String user = "root ";
Private static String password = "root ";
Private static String driverName = "com. mysql. jdbc. Driver ";
// Declare a static link object
Private static Connection connection = null;
// Compile a static code block (loaded first than the constructor) to load the driver class.
Static {
Try {
Class. forName (driverName );
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
}
}
Public static Connection getConnection (){
// Create a link object
Try {
Connection = DriverManager. getConnection (url, user, password );
} Catch (SQLException e ){
E. printStackTrace ();
}
Return connection;
}
/**
* Execute the add or delete modified SQL statement method.
*
* @ Param SQL
* @ Return number of affected rows
*/
Public static int executeNotQuery (String SQL ){
Int num = 0;
// 1. get the link object
Connection conn = getConnection ();
// 2. create a command execution object
Statement stm = null;
Try {
Stm = conn. createStatement ();
Num = cmd.exe cuteUpdate (SQL );
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
Close (null, stm, conn );
}
Return num;
}
/**
* Method for disabling resources
*
* @ Param rs
* Result set
* @ Param stm
* Execution object
* @ Param conn
* Link object
*/
Private static void close (ResultSet rs, Statement stm, Connection conn ){
If (null! = Rs ){
Try {
Rs. close ();
} Catch (SQLException e ){
System. out. println ("An error occurred while disabling the result set! ");
E. printStackTrace ();
}
}
If (null! = Stm ){
Try {
Stm. close ();
} Catch (SQLException e ){
System. out. println ("An error occurred while disabling the execution object! ");
E. printStackTrace ();
}
}
If (null! = Conn ){
Try {
Conn. close ();
} Catch (SQLException e ){
System. out. println ("An error occurred while closing the link object! ");
E. printStackTrace ();
}
}
Rs = null;
Stm = null;
Conn = null;
} // End
/**
* Method for executing a query statement
*
* @ Param SQL
* @ Return result set
*/
Public static ResultSet executeQuery (String SQL ){
ResultSet rs = null;
// 1. get the link object
Connection conn = getConnection ();
// 2. create a command object
Try {
Statement stm = conn. createStatement ();
// 3. run the query method to obtain the result set.
Rs = cmd.exe cuteQuery (SQL );
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
// 4. close the resource
// Close (rs, stm, conn );
}
Return rs;
}
/**
* Method for executing pre-compiled SQL statements
* @ Param SQL
* @ Param pams
* @ Return
*/
Public static int executPreStm (String SQL, String [] pams ){
Int num = 0;
PreparedStatement pstm = null;
Try {
// Create a compilation object
Pstm = getConnection (). prepareStatement (SQL );
// Add placeholder data
For (int I = 0; I <pams. length; I ++ ){
Pstm. setString (I, pams [i-1]);
}
// Execute the SQL statement
Num1_p0000.exe cuteUpdate (); // only non-query statements can be executed.
// Close the resource
Pstm. close ();
Connection. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Return num;
}
}