packagecom.tool;//登录连接 数据库
importjava.io.FileInputStream;importjava.io.IOException;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.Properties;public classSuperUtil {private staticProperties property;static{
String url= SuperUtil.class.getClassLoader().getResource("super.properties").getPath();try{
property= newProperties();
property.load(newFileInputStream(url));
}catch(IOException e) {
e.printStackTrace();
}
}public staticConnection getConnection() {
Connection connection= null;try{if(connection==null) {
Class.forName(property.getProperty("driver"));
String url= property.getProperty("url");
String username= property.getProperty("username");
String password= property.getProperty("password");
connection=DriverManager.getConnection(url, username, password);
}
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
}returnconnection;
}public static voidrelease(Connection con, Statement statement, ResultSet rs) {if (null !=rs) {try{
rs.close();
}catch(SQLException e) {
e.printStackTrace();
}finally{
rs= null;
}if (null !=statement) {try{
statement.close();
}catch(SQLException e) {
e.printStackTrace();
}finally{
statement= null;
}
}if (null !=con) {try{
con.close();
}catch(SQLException e) {
e.printStackTrace();
}finally{
con= null;
}
}
}
}/*** 增删改【Add、Del、Update】
*
*@paramsql
*@returnint*/
public static intexecuteNonQuery(String sql) {int result = 0;
Connection conn= null;
Statement stmt= null;try{
conn=getConnection();
stmt=conn.createStatement();
result=stmt.executeUpdate(sql);
}catch(SQLException err) {
err.printStackTrace();
free(null, stmt, conn);
}finally{
free(null, stmt, conn);
}returnresult;
}/*** 增删改【Add、Delete、Update】
*
*@paramsql
*@paramobj
*@returnint*/
public static intexecuteNonQuery(String sql, Object... obj) {int result = 0;
Connection conn= null;
PreparedStatement pstmt= null;try{
conn=getConnection();
pstmt=conn.prepareStatement(sql);for (int i = 0; i < obj.length; i++) {
pstmt.setObject(i+ 1, obj[i]);
}
result=pstmt.executeUpdate();
}catch(SQLException err) {
err.printStackTrace();
free(null, pstmt, conn);
}finally{
free(null, pstmt, conn);
}returnresult;
}/*** 查【Query】
*
*@paramsql
*@returnResultSet*/
public staticResultSet executeQuery(String sql) {
Connection conn= null;
Statement stmt= null;
ResultSet rs= null;try{
conn=getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException err) {
err.printStackTrace();
free(rs, stmt, conn);
}returnrs;
}/*** 查【Query】
*
*@paramsql
*@paramobj
*@returnResultSet*/
public staticResultSet executeQuery(String sql, Object... obj) {
Connection conn= null;
PreparedStatement pstmt= null;
ResultSet rs= null;try{
conn=getConnection();
pstmt=conn.prepareStatement(sql);for (int i = 0; i < obj.length; i++) {
pstmt.setObject(i+ 1, obj[i]);
}
rs=pstmt.executeQuery();
}catch(SQLException err) {
err.printStackTrace();
free(rs, pstmt, conn);
}returnrs;
}/*** 判断记录是否存在
*
*@paramsql
*@returnBoolean*/
public staticBoolean isExist(String sql) {
ResultSet rs= null;try{
rs=executeQuery(sql);
rs.last();int count =rs.getRow();if (count > 0) {return true;
}else{return false;
}
}catch(SQLException err) {
err.printStackTrace();
free(rs);return false;
}finally{
free(rs);
}
}/*** 判断记录是否存在
*
*@paramsql
*@returnBoolean*/
public staticBoolean isExist(String sql, Object... obj) {
ResultSet rs= null;try{
rs=executeQuery(sql, obj);
rs.last();int count =rs.getRow();if (count > 0) {return true;
}else{return false;
}
}catch(SQLException err) {
err.printStackTrace();
free(rs);return false;
}finally{
free(rs);
}
}/*** 获取查询记录的总行数
*
*@paramsql
*@returnint*/
public static intgetCount(String sql) {int result = 0;
ResultSet rs= null;try{
rs=executeQuery(sql);
rs.last();
result=rs.getRow();
}catch(SQLException err) {
free(rs);
err.printStackTrace();
}finally{
free(rs);
}returnresult;
}/*** 获取查询记录的总行数
*
*@paramsql
*@paramobj
*@returnint*/
public static intgetCount(String sql, Object... obj) {int result = 0;
ResultSet rs= null;try{
rs=executeQuery(sql, obj);
rs.last();
result=rs.getRow();
}catch(SQLException err) {
err.printStackTrace();
}finally{
free(rs);
}returnresult;
}/*** 释放【ResultSet】资源
*
*@paramrs*/
public static voidfree(ResultSet rs) {try{if (rs != null) {
rs.close();
}
}catch(SQLException err) {
err.printStackTrace();
}
}/*** 释放【Statement】资源
*
*@paramst*/
public static voidfree(Statement st) {try{if (st != null) {
st.close();
}
}catch(SQLException err) {
err.printStackTrace();
}
}/*** 释放【Connection】资源
*
*@paramconn*/
public static voidfree(Connection conn) {try{if (conn != null) {
conn.close();
}
}catch(SQLException err) {
err.printStackTrace();
}
}/*** 释放所有数据资源
*
*@paramrs
*@paramst
*@paramconn*/
public static voidfree(ResultSet rs, Statement st, Connection conn) {
free(rs);
free(st);
free(conn);
}
}