Example usage for java.sql ResultSet getDate

List of usage examples for java.sql ResultSet getDate

Introduction

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

Prototype

java.sql.Date getDate(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a java.sql.Date object in the Java programming language.

Usage

From source file:com.webpagebytes.wpbsample.database.WPBDatabase.java

public Map<Date, Integer> getSumOperationsFromLastDays(int type, int days) throws SQLException {
    Connection connection = getConnection();
    PreparedStatement statement = null;
    Map<Date, Integer> result = new HashMap<Date, Integer>();
    try {/*  w w w.  j  a va  2 s  .  com*/
        statement = connection.prepareStatement(GET_ACC_OPERATIONS_STATS_BY_DAYS);
        statement.setInt(1, type);
        statement.setInt(2, days);
        ResultSet rs = statement.executeQuery();
        while (rs.next()) {
            result.put(rs.getDate(1), rs.getInt(3));
        }
    } finally {
        if (statement != null) {
            statement.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
    return result;
}

From source file:com.webpagebytes.wpbsample.database.WPBDatabase.java

public Map<Date, Integer> getCountOperationsFromLastDays(int type, int days) throws SQLException {
    Connection connection = getConnection();
    PreparedStatement statement = null;
    Map<Date, Integer> result = new HashMap<Date, Integer>();
    try {//w ww  . j a  va2 s . com
        statement = connection.prepareStatement(GET_ACC_OPERATIONS_STATS_BY_DAYS);
        statement.setInt(1, type);
        statement.setInt(2, days);
        ResultSet rs = statement.executeQuery();
        while (rs.next()) {
            result.put(rs.getDate(1), rs.getInt(2));
        }
    } finally {
        if (statement != null) {
            statement.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
    return result;
}

From source file:com.havoc.hotel.admin.dao.impl.BookingDAOImpl.java

@Override
public Booking getById(int bookingId) throws SQLException {

    return (Booking) jdbcTemplate.query(SQLConstant.BOOKING_GETBYID, new Object[] { bookingId },
            new ResultSetExtractor<Booking>() {

                @Override/*ww w  .  ja v  a  2 s .co  m*/
                public Booking extractData(ResultSet rs) throws SQLException, DataAccessException {
                    Booking b = null;
                    if (rs.next()) {
                        b = new Booking();
                        b.setBookingId(rs.getInt("booking_id"));
                        b.setFirstName(rs.getString("first_name"));
                        b.setLastName(rs.getString("last_name"));
                        Room r = new Room();
                        r.setRoomId(rs.getInt("room_id"));
                        r.setRoomPrice(rs.getInt("room_price"));
                        r.setRoomNumber(rs.getInt("room_number"));
                        b.setRoom(r);
                        b.setCheckinDate(rs.getDate("checkin_date"));
                        b.setTotalDays(rs.getInt("total_days"));
                        b.setTotalNights(rs.getInt("total_nights"));
                        b.setCheckoutDate(rs.getDate("checkout_date"));
                        Customer c = new Customer();
                        c.setCustomerId(rs.getInt("customer_id"));
                        c.setFirstName(rs.getString("first_name"));
                        c.setLastName(rs.getString("last_name"));
                        c.setUsername(rs.getString("username"));
                        b.setCustomer(c);
                        b.setTotalPrice(rs.getInt("total_price"));
                        b.setPdf(rs.getString("pdf"));
                        b.setStatus(rs.getBoolean("status"));
                    }
                    return b;
                }

            });
}

From source file:nl.tudelft.stocktrader.mysql.MySQLCustomerDAO.java

public List<Order> getCompletedOrders(String userId) throws DAOException {
    PreparedStatement selectClosedOrders = null;
    PreparedStatement updateClosedOrders = null;
    try {// ww  w.j av  a  2  s  .  c o m
        selectClosedOrders = sqlConnection.prepareStatement(SQL_SELECT_CLOSED_ORDERS);
        selectClosedOrders.setString(1, userId);
        ResultSet rs = selectClosedOrders.executeQuery();
        List<Order> closedOrders = new ArrayList<Order>();

        try {
            while (rs.next()) {
                int orderId = rs.getInt(1);
                Calendar openDate = StockTraderUtility.convertToCalendar(rs.getDate(4));
                Calendar completionDate = null;
                try {
                    completionDate = StockTraderUtility.convertToCalendar(rs.getDate(5));
                } catch (SQLException e) {
                    logger.debug("", e);
                    completionDate = Calendar.getInstance();
                    completionDate.setTimeInMillis(0);
                }
                Order closedOrderBean = new Order(orderId, rs.getString(2), rs.getString(3), openDate,
                        completionDate, rs.getDouble(6), rs.getBigDecimal(7), rs.getBigDecimal(8),
                        rs.getString(9));
                closedOrderBean.setOrderStatus(StockTraderUtility.ORDER_STATUS_CLOSED);
                closedOrders.add(closedOrderBean);
            }
        } finally {
            try {
                rs.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }

        if (!closedOrders.isEmpty()) {
            updateClosedOrders = sqlConnection.prepareStatement(SQL_UPDATE_CLOSED_ORDERS);
            updateClosedOrders.setString(1, userId);
            updateClosedOrders.executeUpdate();
        }

        return closedOrders;
    } catch (SQLException e) {
        throw new DAOException("", e);
    } finally {
        if (selectClosedOrders != null) {
            try {
                selectClosedOrders.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
        if (updateClosedOrders != null) {
            try {
                selectClosedOrders.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }

    }
}

From source file:com.spvp.dal.MySqlDatabase.java

@Override
public ArrayList<Prognoza> dajHistorijskePodatkePrognozaZaGrad(String grad, int brojDana) throws SQLException {

    Grad x = this.dajGradPoImenu(grad);

    this.osvjeziHistorijuPrognozaZaGrad(x, webService);

    ArrayList<Prognoza> listaPrognoza = new ArrayList<>();

    int idGrada = x.getIdGrada();

    if (idGrada == -1)
        return listaPrognoza;

    try (Connection conn = getConnection()) {

        PreparedStatement s = conn
                .prepareStatement("SELECT vrijeme, temp, pritisak, brzina_vjetra, vlaznost_zraka, datum "
                        + "FROM historija_prognoze p, gradovi_prognoze "
                        + "WHERE p.id = prognoza_id AND grad_id =? " + "ORDER BY datum DESC " + "LIMIT ?");

        s.setInt(1, idGrada);/*w  w w  .  j a  v a 2  s. co  m*/
        s.setInt(2, brojDana);

        ResultSet rs = s.executeQuery();

        Grad g = dajGradPoImenu(grad);
        Prognoza p;
        while (rs.next()) {

            p = new Prognoza(g, rs.getString("temp"), rs.getString("vlaznost_zraka"), rs.getString("pritisak"),
                    rs.getString("brzina_vjetra"), rs.getString("vrijeme"));
            p.setDatum(rs.getDate("datum"));
            listaPrognoza.add(p);

        }

        conn.close();
        s.close();
        rs.close();
    }

    return listaPrognoza;

}

From source file:com.adito.jdbc.JDBCUserDatabase.java

public User getAccount(String username) throws UserNotFoundException, Exception {
    // Get the allowed roles from the database
    JDBCPreparedStatement ps = db.getStatement("select.user");
    try {/*from   w  w  w . j  a v a  2s  . c o  m*/
        ps.setString(1, username);
        ResultSet results = ps.executeQuery();
        try {
            while (results.next()) {
                Realm usersRealm = UserDatabaseManager.getInstance().getRealm(results.getInt("realm_ID"));
                if (usersRealm.equals(realm)) {
                    DefaultUser u = new DefaultUser(results.getString("username"), results.getString("email"),
                            results.getString("fullname"), results.getDate("last_password_change"), usersRealm);
                    addRoles(u);
                    return u;
                }
            }
            throw new UserNotFoundException(
                    "No user exists with username " + username + " in realm " + realm.getResourceDisplayName());
        } finally {
            results.close();
        }
    } finally {
        ps.releasePreparedStatement();
    }
}

From source file:no.polaric.aprsdb.MyDBSession.java

/**
 * Get an APRS item at a given point in time.
 *//*  w  w w  . j a va2s . com*/
public AprsPoint getItem(String src, java.util.Date at) throws java.sql.SQLException {
    _log.debug("MyDbSession", "getItem:  " + src + ", " + df.format(at));
    PreparedStatement stmt = getCon().prepareStatement(
            " SELECT * FROM \"PosReport\"" + " WHERE src=? AND time <= ?" + " ORDER BY time DESC LIMIT 1",
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    stmt.setString(1, src);
    stmt.setTimestamp(2, new Timestamp(at.getTime()));
    ResultSet rs = stmt.executeQuery();

    String name[] = src.split("@", 2);
    AprsPoint x = null;
    if (name.length > 1) {
        Station owner = _api.getDB().getStation(name[1], null);
        x = new AprsObject(owner, name[0]);
    } else
        x = new Station(src);

    if (rs.next())
        x.update(
                rs.getDate("time"), new AprsHandler.PosData(getRef(rs, "position"), rs.getInt("course"),
                        rs.getInt("speed"), rs.getString("symbol").charAt(0), rs.getString("symtab").charAt(0)),
                null, null);
    return x;
}

From source file:nl.tudelft.stocktrader.mysql.MySQLCustomerDAO.java

public Account login(String userId, String password) throws DAOException {
    PreparedStatement selectCustomerProfileByUserId = null;
    PreparedStatement updateCustomerLogin = null;
    PreparedStatement selectCustomerLogin = null;
    try {//  w ww. j  ava  2 s .  c o  m
        selectCustomerProfileByUserId = sqlConnection.prepareStatement(SQL_SELECT_CUSTOMER_PROFILE_BY_USERID);
        selectCustomerProfileByUserId.setString(1, userId);
        ResultSet customerProfileRS = selectCustomerProfileByUserId.executeQuery();
        if (customerProfileRS.next()) {
            try {
                String userPassword = customerProfileRS.getString(2);
                if (userPassword.equals(password)) {
                    try {
                        updateCustomerLogin = sqlConnection.prepareStatement(SQL_UPDATE_CUSTOMER_LOGIN);
                        updateCustomerLogin.setString(1, userId);
                        updateCustomerLogin.executeUpdate();
                        selectCustomerLogin = sqlConnection.prepareStatement(SQL_SELECT_CUSTOMER_LOGIN);
                        selectCustomerLogin.setString(1, userId);
                        ResultSet rs = selectCustomerLogin.executeQuery();
                        if (rs.next()) {
                            try {
                                Account accountData = new Account(rs.getInt(1), userId,
                                        StockTraderUtility.convertToCalendar(rs.getDate(2)),
                                        rs.getBigDecimal(3), rs.getInt(4), rs.getBigDecimal(5),
                                        StockTraderUtility.convertToCalendar(rs.getDate(6)), rs.getInt(7) + 1);
                                return accountData;
                            } finally {
                                try {
                                    rs.close();
                                } catch (SQLException e) {
                                    logger.debug("", e);
                                }
                            }
                        }
                    } catch (SQLException e) {
                        throw new DAOException("", e);
                    } finally {
                        if (updateCustomerLogin != null) {
                            try {
                                updateCustomerLogin.close();
                            } catch (SQLException e) {
                                logger.debug("", e);
                            }
                        }
                        if (selectCustomerLogin != null) {
                            try {
                                selectCustomerLogin.close();
                            } catch (SQLException e) {
                                logger.debug("", e);
                            }
                        }
                    }
                }
            } finally {
                try {
                    customerProfileRS.close();
                } catch (SQLException e) {
                    logger.debug("", e);
                }
            }
        }

    } catch (SQLException e) {
        throw new DAOException("", e);
    } finally {
        if (selectCustomerProfileByUserId != null) {
            try {
                selectCustomerProfileByUserId.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
    }
    return null;
}

From source file:edu.clemson.cs.nestbed.server.adaptation.sql.MoteSqlAdapter.java

public Map<Integer, Mote> readMotes() throws AdaptationException {
    Map<Integer, Mote> motes = new HashMap<Integer, Mote>();
    Connection connection = null;
    Statement statement = null;// ww w  .  ja v  a  2  s  . com
    ResultSet resultSet = null;

    try {
        String query = "SELECT * FROM Motes";

        connection = DriverManager.getConnection(CONN_STR);
        statement = connection.createStatement();
        resultSet = statement.executeQuery(query);

        while (resultSet.next()) {
            int id;
            String moteSerialID;
            int moteTypeID;
            Date timestamp;
            int bus;
            int device;
            int port;
            UsbDeviceInformation usbDevInfo;

            id = resultSet.getInt(Index.ID.index());
            moteSerialID = resultSet.getString(Index.MOTESERIALID.index());
            moteTypeID = resultSet.getInt(Index.MOTETYPEID.index());
            timestamp = resultSet.getDate(Index.TIMESTAMP.index());

            usbDevInfo = new UsbDeviceInformation(moteSerialID);
            bus = usbDevInfo.getBus();
            device = usbDevInfo.getDevice();
            port = usbDevInfo.getPort();

            Mote mote = new Mote(id, moteSerialID, moteTypeID, timestamp, bus, device, port);

            motes.put(id, mote);
        }
    } catch (SQLException ex) {
        String msg = "SQLException in readMotes";
        log.error(msg, ex);
        throw new AdaptationException(msg, ex);
    } finally {
        try {
            resultSet.close();
        } catch (Exception ex) {
        }
        try {
            statement.close();
        } catch (Exception ex) {
        }
        try {
            connection.close();
        } catch (Exception ex) {
        }
    }

    return motes;
}

From source file:org.apache.phoenix.end2end.index.IndexMaintenanceIT.java

private void verifyResult(ResultSet rs, int i) throws SQLException {
    assertTrue(rs.next());/*w  w  w.j a va 2  s . c o m*/
    assertEquals("VARCHAR" + String.valueOf(i) + "_" + StringUtils.rightPad("CHAR" + String.valueOf(i), 10, ' ')
            + "_A.VARCHAR" + String.valueOf(i) + "_"
            + StringUtils.rightPad("B.CHAR" + String.valueOf(i), 10, ' '), rs.getString(1));
    assertEquals(i * 3, rs.getInt(2));
    Date date = new Date(DateUtil.parseDate("2015-01-01 00:00:00").getTime() + (i) * MILLIS_IN_DAY);
    assertEquals(date, rs.getDate(3));
    assertEquals(date, rs.getDate(4));
    assertEquals(date, rs.getDate(5));
    assertEquals("varchar" + String.valueOf(i), rs.getString(6));
    assertEquals("char" + String.valueOf(i), rs.getString(7));
    assertEquals(i, rs.getInt(8));
    assertEquals(i, rs.getLong(9));
    assertEquals(i * 0.5d, rs.getDouble(10), 0.000001);
    assertEquals(i, rs.getLong(11));
    assertEquals(i, rs.getLong(12));
}