/****/
packagejavaBeanServlet;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;/***@authorAdministrator
**/
public classUnilt {private static final String URL = "jdbc:mysql://localhost:3306/userdb";private static final String USER = "root";private static final String PASSWORD = "111111";protected static Statement s = null;protected static ResultSet rs = null;protected static Connection coon = null;public static synchronizedConnection getConnection() {try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}try{
coon=DriverManager.getConnection(URL, USER, PASSWORD);
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returncoon;
}public static intexecuteUpdate(String sql) {int result = 0;try{
s=getConnection().createStatement();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}try{
result=s.executeUpdate(sql);
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnresult;
}public staticResultSet executeQuery(String sql) {try{
s=getConnection().createStatement();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}try{
rs=s.executeQuery(sql);
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnrs ;
}public staticPreparedStatement executePreparedStatement(String sql) {
PreparedStatement ps= null;try{
ps=getConnection().prepareStatement(sql);
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnps;
}public static voidrollback() {try{
getConnection().rollback();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}public static voidclose() {if (rs != null) {try{
rs.close();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}if (s != null) {try{
s.close();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}if (coon != null) {try{
coon.close();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}