実習_day01_JdbcUtilツールクラス


jdbcを独自のツールクラスにカプセル化する方法~
1.他にもいくつかのパッケージがあります.例えば、削除したり、ページを分けたりします.の
package wonderful.wzf;

import java.sql.*;

/**
 * @author :wonderful_wzf
 * @message: jdbc   JdbcUtil   
 * @Bolg :https://blog.csdn.net/wonderful_wzf
 * @date :2020/5/18,19:54
 */
public class JdbcUtil {
    /**
     * Connection     
     */
    static Connection connection = null;
    /**
     *  PreparedStatement  
     */
    static PreparedStatement preparedStatement = null;
    /**
     *    
     */
    static ResultSet resultSet = null;
    /**
     * mysql jdbc  
     */
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    /**
     *       url
     */
    static final String JDBC_URL = "jdbc:mysql://localhost:3306/wonderful";
    /**
     *          
     */
    static final String USER = "root";
    /**
     *         
     */
    static final String PASSWORD = "root";

    /**
     * @function   Connection
     * @return      Connection  
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(JDBC_DRIVER);
        connection = DriverManager.getConnection(JDBC_URL,USER,PASSWORD);
        return connection;
    }

    /**
     * @function   sql  ,     PreparedStatement  
     * @param sql
     * @param params
     * @return
     */
    public static PreparedStatement getPreparedStatement(String sql,Object[] params) throws SQLException, ClassNotFoundException {
        connection = getConnection();
        preparedStatement = connection.prepareStatement(sql);
        if(params!=null){
            for(int i = 0 ;i<params.length;i++){
                preparedStatement.setObject(i+1,params[i]);
            }
        }
        return preparedStatement;
    }
    /**
     *        
     * @param connection
     * @param statement
     * @param resultSet
     */
    public static void close( ResultSet resultSet, Statement statement,Connection connection){
        /*connection,statement,resultSet           ?
         :              ,             
          ,             ,               
                  ,        ResultSet,Statement,
          Connection       .*/
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}


2.自分が以前書いたパッケージを参考にして~
//   
	//                  
	public boolean executeUpdate(String sql,Object[] params) {
		int num =-1;
	try {
		pstm = getPreparedStatement(sql, params);
		 num=pstm.executeUpdate();
	} 
	catch (ClassNotFoundException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}catch (Exception e) {
		e.printStackTrace();
	}
	finally {
			closeAll(rs, pstm, con);
		}
					if(num>0) {
						return true;
					}
					else {
						return false;
					}
	}
	// 
	//    
    public ResultSet executeQuery(String sql,Object[] params) {
		int num =0;
	try {
		pstm = getPreparedStatement(sql, params);
		 rs = pstm.executeQuery();
		} 
	catch (ClassNotFoundException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}catch (Exception e) {
		e.printStackTrace();
	}
	return rs;
	}
	
	//       currentPage:      pageSize:    
    public ResultSet  paginationQuery(int currentPage,int pageSize) throws ClassNotFoundException, SQLException {
    	con=getConnection();
    	String sql = "select * from user limit ?,?";
    	pstm = con.prepareStatement(sql);
    	
    	pstm.setInt(1,currentPage*pageSize);
    	pstm.setInt(2, pageSize);
    	rs=pstm.executeQuery();
		return rs;
    }

私の今の理解によると、ページを分けてゆっくりと私达のバックグラウンドがする必要はありませんようで、フロントエンドは完成することができます~ps:今日また学びました:接続プールを创建して、c 3 p 0と接続プール、すでにとても遅くて、明日この点の内容を更新します欧~みんなを歓迎して学习を讨论して、互いに进歩します