Copy Code code as follows:
Package sns.team6.util;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
/**
* Link the Database tool class
*
* @author Xu Rui
*
*/
public class DBHelper {
MySQL Drive path
Private static final String Driver = "Com.mysql.jdbc.Driver";
Connection path for database
Private static final String URL = "JDBC:MYSQL://LOCALHOST:3306/SNSTEAM6";
/**
* Objects that connect to the database
*
* @return
*/
public static Connection Getconnection () {
connection conn = null;
try {
class.forname (driver);
conn = drivermanager.getconnection (URL, "root", "root");
} catch (Exception e) {
e.printstacktrace ();
 &NBSP}
return conn;
.}
/**
* Turn off Open resources
*
* @param conn
* @param PST
* @param rst
&NB Sp */
public static void Closeinfo (Connection conn, PreparedStatement PST,
resultset rst) {
try {
if (rst!= null) {
rst.close ();
rst = null;
}
if (PST!= null) {
pst.close ();
pst = null;
}
   IF (conn!= null) {
conn.close ();
conn = null;
}
} catch (Exception e) {
e.printstacktrace ();
 &NBSP}
}
/**
* Get result set object
*
* @param sql
* @param params
* @return
*/
public static ResultSet ResultSet (String sql, object[] params) {
The linked object of the database
Connection conn = null;
The Operation object of the database
PreparedStatement PST = NULL;
Result Object
ResultSet rst = null;
try {
The linked object of the database
conn = Dbhelper.getconnection ();
The Operation object of the database
PST = conn.preparestatement (SQL);
Determine if there are parameters
if (params!= null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
Assigning a value to an Action object
Pst.setobject (i + 1, params[i]);
}
}
Get the result object
rst = Pst.executequery ();
catch (SQLException e) {
rst = null;
E.printstacktrace ();
}
return rst;
}
/**
* Get the added result object
*
* @param sql
* @param params
* @return
*/
public static Boolean result (String sql, object[] params) {
Boolean flag = false;
The linked object of the database
Connection conn = null;
The Operation object of the database
PreparedStatement PST = NULL;
try {
The linked object of the database
conn = Dbhelper.getconnection ();
The Operation object of the database
PST = conn.preparestatement (SQL);
Determine if there are parameters
if (params!= null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
Assigning a value to an Action object
Pst.setobject (i + 1, params[i]);
}
}
Gets the result object, which is the int type, indicating the number of rows executed successfully
int row = Pst.executeupdate ();
if (Row > 0) {
If greater than 0 indicates successful execution
Flag = true;
}
catch (SQLException e) {
Flag = false;
E.printstacktrace ();
}
return flag;
}
}