Example usage for java.sql ResultSet last

List of usage examples for java.sql ResultSet last

Introduction

In this page you can find the example usage for java.sql ResultSet last.

Prototype

boolean last() throws SQLException;

Source Link

Document

Moves the cursor to the last row in this ResultSet object.

Usage

From source file:com.snowy.data.java

public int isGameActive(int id) {
    int xs = 0;/*from www.j av  a  2s .c  om*/
    try {
        PreparedStatement ps = con.prepareStatement("select active from game where GameId=?");
        ps.setInt(1, id);
        ResultSet rs = ps.executeQuery();
        rs.last();
        xs = rs.getInt("active");
        //ps.close();
        //rs.close();
    } catch (SQLException ex) {
        Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex);
    }
    return xs;
}

From source file:com.snowy.data.java

public String getUsernameFromToken() {
    String s = "";
    try {/* ww w  .  jav a 2s  .  c o  m*/
        PreparedStatement ss = con.prepareStatement("select Username from users where token = ?");
        ss.setString(1, VaadinSession.getCurrent().getCsrfToken());
        ResultSet rs = ss.executeQuery();
        rs.last();
        if (rs.getRow() > 0) {
            s = rs.getString("Username");
        }
        //ss.close();
        //rs.close();
    } catch (SQLException ex) {
        Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex);
    }
    return s;
}

From source file:com.snowy.data.java

public int getUserIdFromUsername(String username) {
    int i = 0;//w ww. j a  va2  s .co  m
    try {
        String s = username;

        PreparedStatement ps = con.prepareStatement("select user_id from users where Username =?");
        ps.setString(1, s);
        ResultSet rs = ps.executeQuery();
        rs.last();
        i = rs.getInt("user_id");
        //rs.close();
        //ps.close();
    } catch (SQLException ex) {
        Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex);
    }
    return i;
}

From source file:com.snowy.data.java

public int getUserIdFromToken() {
    int i = 0;// w ww .j  a  v  a 2s  .  c  o m
    try {
        String s = this.getUsernameFromToken();

        PreparedStatement ps = con.prepareStatement("select user_id from users where Username =?");
        ps.setString(1, s);
        ResultSet rs = ps.executeQuery();
        rs.last();
        i = rs.getInt("user_id");
        //rs.close();
        //ps.close();
    } catch (SQLException ex) {
        Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex);
    }
    return i;
}

From source file:com.snowy.data.java

public void resetActiveIfNot() {
    try {/*from  w w w. j  a  v a2  s . c  o  m*/
        PreparedStatement ps = con.prepareStatement("select active from users where Username =?");
        ps.setString(1, this.getUsernameFromToken());
        ResultSet rs = ps.executeQuery();
        rs.last();
        if (rs.getInt("active") != 1) {
            ps = con.prepareStatement("update users set active =? where Username=?");
            ps.setInt(1, 1);
            ps.setString(2, this.getUsernameFromToken());
            ps.executeUpdate();
        }
        //ps.close();
        //rs.close();
    } catch (SQLException ex) {
        Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.snowy.data.java

public int getNumberOfTurns(int gameId) {
    int i = 0;/* ww  w  . ja v a  2  s.  c  o m*/
    try {
        //doesnt work

        PreparedStatement ps = con.prepareStatement("Select MoveOrder from game where GameId=?");
        ps.setInt(1, gameId);
        ResultSet rs = ps.executeQuery();
        rs.last();
        String boardString = rs.getString("MoveOrder");

        char[] boardCharArray = boardString.toCharArray();
        java.util.Arrays.sort(boardCharArray);
        //Logger.getLogger(data.class.getName()).info(boardCharArray[41]+"");
        i = Integer.parseInt(String.valueOf(boardCharArray[41]));
        //Logger.getLogger(data.class.getName()).info(i+"");
        //ps.close();
        //rs.close();
    } catch (SQLException ex) {
        Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex);
    }
    return i;
}

From source file:com.snowy.data.java

public boolean login(String username, String password) {

    //Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, con.toString());
    boolean b = false;
    try {//w  w w.jav a 2s .c om
        PreparedStatement s = con.prepareStatement("select Username,Password from users where Username = ?");
        s.setString(1, username);
        ResultSet rs = s.executeQuery();
        rs.last();
        //srs = template.queryForRowSet("select Username,Password from tempuser where Username = ?", username);
        //srs.last();
        if (rs.getRow() >= 1) {
            b = PasswordHash.verifyPassword(password.trim(), rs.getString("Password").trim());

        } else {
            b = false;
        }
        //s.close();
        //rs.close();
    } catch (PasswordHash.InvalidHashException | SQLException
            | PasswordHash.CannotPerformOperationException ex) {
        b = false;
        Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex);
    }

    return b;
}

From source file:com.snowy.data.java

public ArrayList<ArrayList<Integer>> getMoveOrder(int gameId) {
    ArrayList<ArrayList<Integer>> returns = new ArrayList<>();
    try {//w w  w .java  2  s. c  om
        PreparedStatement ps = con.prepareStatement("Select MoveOrder from game where GameId=?");
        ps.setInt(1, gameId);
        ResultSet rs = ps.executeQuery();
        rs.last();
        String boardString = rs.getString("MoveOrder");

        char[] boardCharArray = boardString.toCharArray();

        for (int i = 0; i < boardCharArray.length; i += 7) {
            ArrayList<Integer> in = new ArrayList<>();
            for (int x = 0; x < 7; x++) {
                in.add(Integer.parseInt(String.valueOf(boardCharArray[i + x])));
            }
            returns.add(in);
        }

        //rs.close();
        //ps.close();
    } catch (SQLException ex) {
        Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex);
    }
    return returns;
}

From source file:com.snowy.data.java

public HashMap<String, Integer> retrieveActiveUsers() {

    HashMap<String, Integer> hm = new HashMap<>();
    try {/*from   w  ww  . j av  a2s . co  m*/
        PreparedStatement ps = con.prepareStatement("select Username, user_id from users where active = ?");
        ps.setInt(1, 1);
        ResultSet rs = ps.executeQuery();
        rs.last();
        if (rs.getRow() >= 1) {
            rs.beforeFirst();
            while (rs.next()) {

                hm.put(rs.getString("Username"), rs.getInt("user_id"));
                //Logger.getLogger(data.class.getName()).info(hm.get(rs.getString("Username")).toString());
            }
        }
        //rs.first();
        //while(rs.next()){
        //hm.put( rs.getInt("user_id"),rs.getString("Username"));

        //}
        //ps.close();
        //rs.close();
    } catch (SQLException ex) {
        Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex);
    }
    return hm;
}

From source file:com.snowy.data.java

public ArrayList<ArrayList<String>> retriveChallenges() {
    ArrayList<ArrayList<String>> res = new ArrayList<>();
    try {/*from www  .  j  a va 2  s. c  o  m*/

        String uname = this.getUsernameFromToken();
        PreparedStatement ps = con.prepareStatement(
                "select requestor,requested,timestamp,accepted,RequestId from gamerequest where requestor = (select user_id from users where Username=?) or requested = (select user_id from users where Username=?) and timestamp >subdate(current_timestamp, interval 2 hour)");
        ps.setString(1, uname);
        ps.setString(2, uname);
        //PreparedStatement pss =con.prepareStatement("select Username from users where user_id=?");

        ResultSet rs = ps.executeQuery();
        rs.last();
        if (rs.getRow() > 0) {
            rs.beforeFirst();
            while (rs.next()) {

                ArrayList<String> al = new ArrayList<>();
                al.add(this.getUsernameFromId(rs.getInt("requestor")));
                al.add(this.getUsernameFromId(rs.getInt("requested")));
                al.add(String.valueOf(rs.getTimestamp("timestamp").getTime()));
                al.add(rs.getInt("accepted") + "");
                //al.add(rs.getInt("gameCreated")+"");
                al.add(rs.getInt("RequestId") + "");
                res.add(al);
            }
        }
        //ps.close();
        //rs.close();
    } catch (SQLException ex) {
        Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex);
    }
    return res;
}