postgresql-JDBC-基本增删改查

 public List getUsersByAddress(String theAddress)
    {
        List result = new ArrayList();
        try{
        String sql = "SELECT userID from lab4.ChirpUsers where address=?";
        PreparedStatement st = connection.prepareStatement(sql);
        st.setString(1, theAddress);
        ResultSet rs = st.executeQuery();
        while(rs.next()){
        Integer userId = rs.getInt(1);
        result.add(userId);
        }
        }catch(SQLException e){
        e.printStackTrace();
        }
        return result;

    }


public int makeUsersInactive() {
        // your code here; return 0 appears for now to allow this skeleton to compile.
        int rs = 0;
        try{
        String sql = "update lab4.ChirpUsers set active = true where active = false";
        Statement st = connection.createStatement();
        rs = st.executeUpdate(sql);
       
        st.close();
        }catch(SQLException e){
        e.printStackTrace();
        }
    return rs;
    }


 public int purgeBadUsers(int censorLimit)
    {
        // There's nothing special about the name storedFunctionResult
        int storedFunctionResult = 0;
        try{
        String sql = "{call lab4.purgeBadFunction(?)}";
        CallableStatement cst = connection.prepareCall(sql);
        cst.registerOutParameter(1, Types.INTEGER);
        cst.setInt(1, censorLimit);
        cst.execute();
        storedFunctionResult = cst.getInt(1);
        }catch(SQLException e){
        e.printStackTrace();
        }
        return storedFunctionResult;
    }


你可能感兴趣的:(JAVA,POSTGRESQL)