Example usage for java.sql ResultSet getInt

List of usage examples for java.sql ResultSet getInt

Introduction

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

Prototype

int getInt(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as an int in the Java programming language.

Usage

From source file:com.aerothai.database.user.UserService.java

/**
 * Method to check whether uname and pwd combination are correct
 * //from  w  w w.j  av a  2s.  c  o  m
 * @param uname
 * @param pwd
 * @return
 * @throws Exception
 */
public static String checkLogin(String uname, String pwd) throws Exception {
    boolean isUserAvailable = false;
    Connection dbConn = null;
    JSONObject obj = new JSONObject();
    JSONObject jsonData = new JSONObject();
    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        String query = "SELECT * FROM user WHERE username = '" + uname + "' AND pwd=" + "'" + pwd + "'";
        System.out.println(query);
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {
            //System.out.println(rs.getString(1) + rs.getString(2) + rs.getString(3));
            isUserAvailable = true;
            //JSONObject jsonData = new JSONObject();
            jsonData.put("iduser", rs.getInt("iduser"));
            jsonData.put("name", rs.getString("name"));
            jsonData.put("lastname", rs.getString("lastname"));
            jsonData.put("idposition", rs.getInt("idposition"));
            jsonData.put("iddept", rs.getInt("iddept"));
            jsonData.put("idunit", rs.getInt("idunit"));
            jsonData.put("idrole", rs.getInt("idrole"));
            jsonData.put("address", rs.getString("address"));
            jsonData.put("email", rs.getString("email"));
            jsonData.put("tel", rs.getString("tel"));
            jsonData.put("actunit", rs.getString("actunit"));
            jsonData.put("username", rs.getString("username"));
            jsonData.put("photo", rs.getString("photo"));
            jsonData.put("actcust", rs.getString("actcust"));
            //objList.add(rs.getString("pwd"));

        }
        if (isUserAvailable) {

            obj.put("tag", "login");
            obj.put("msg", "done");
            obj.put("status", true);
            obj.put("data", jsonData.toJSONString());
        } else {
            obj.put("tag", "login");
            obj.put("msg", "Incorrect Email or Password");
            obj.put("status", false);
        }
    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj.toJSONString();
}

From source file:com.sql.SystemError.java

/**
 * Gathers a list of errors based on type and count total of them
 *
 * @return/*  w w  w  .j av a  2s . c  o  m*/
 */
public static List<SystemErrorModel> getErrorCounts() {
    List<SystemErrorModel> list = new ArrayList();
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "SELECT exceptionType, COUNT(*) AS 'num' " + "FROM SystemError "
                + "WHERE timeOccurred >= CAST(CURRENT_TIMESTAMP AS DATE) " + "AND username != 'andrew.schmidt' "
                + "AND username != 'anthony.perk' " + "GROUP BY exceptionType";
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()) {
            SystemErrorModel item = new SystemErrorModel();
            item.setExceptionType(rs.getString("exceptionType") == null ? "" : rs.getString("exceptionType"));
            item.setNumber(rs.getInt("num"));
            list.add(item);
        }
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(rs);
    }
    return list;
}

From source file:agendavital.modelo.data.Noticia.java

public static ArrayList<Noticia> getTodasNoticias() throws ConexionBDIncorrecta {
    ResultSet rs = null;
    ArrayList<Noticia> noticias = null;
    try (Connection conexion = ConfigBD.conectar()) {
        noticias = new ArrayList<>();
        String consulta = String.format("SELECT id_noticia from Noticias;");
        rs = conexion.createStatement().executeQuery(consulta);
        while (rs.next()) {
            noticias.add(new Noticia(rs.getInt("id_noticia")));
        }//www .  j  ava2s  . c  o m
        return noticias;
    } catch (SQLException e) {
        throw new ConexionBDIncorrecta();
    }
}

From source file:at.alladin.rmbt.db.dao.QoSTestResultDao.java

/**
 * /*  w  ww.  j ava 2 s .  co m*/
 * @param rs
 * @return
 * @throws SQLException 
 */
private static QoSTestResult instantiateItem(ResultSet rs) throws SQLException {
    QoSTestResult result = new QoSTestResult();

    result.setUid(rs.getLong("uid"));
    result.setTestType(rs.getString("test"));
    result.setResults(rs.getString("result"));
    result.setTestUid(rs.getLong("test_uid"));
    result.setQoSTestObjectiveId(rs.getLong("qos_test_uid"));
    result.setTestDescription(rs.getString("test_desc"));
    result.setTestSummary(rs.getString("test_summary"));
    result.setSuccessCounter(rs.getInt("success_count"));
    result.setFailureCounter(rs.getInt("failure_count"));

    final String results = rs.getString("results");
    try {
        result.setExpectedResults(results != null ? new JSONArray(results) : null);
    } catch (JSONException e) {
        result.setExpectedResults(null);
        e.printStackTrace();
    }

    return result;
}

From source file:com.keybox.manage.db.ProfileSystemsDB.java

/**
 * returns a list of systems for a given profile
 *
 * @param con       DB connection//from www .j  a v  a  2s  .  c o  m
 * @param profileId profile id
 * @return list of host systems
 */
public static List<HostSystem> getSystemsByProfile(Connection con, Long profileId) {

    List<HostSystem> hostSystemList = new ArrayList<HostSystem>();
    try {
        PreparedStatement stmt = con.prepareStatement(
                "select * from  system s, system_map m where s.id=m.system_id and m.profile_id=? order by display_nm asc");
        stmt.setLong(1, profileId);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            HostSystem hostSystem = new HostSystem();
            hostSystem.setId(rs.getLong("id"));
            hostSystem.setDisplayNm(rs.getString("display_nm"));
            hostSystem.setUser(rs.getString("user"));
            hostSystem.setHost(rs.getString("host"));
            hostSystem.setPort(rs.getInt("port"));
            hostSystem.setAuthorizedKeys(rs.getString("authorized_keys"));
            hostSystem.setEnabled(rs.getBoolean("enabled"));
            hostSystemList.add(hostSystem);
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return hostSystemList;
}

From source file:com.sf.ddao.orm.RSMapperFactoryRegistry.java

public static RowMapperFactory getScalarMapper(final Type itemType, final int idx, boolean req) {
    if (itemType == String.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getString(idx);
            }/* w  w  w . j  a va 2  s  .  c o  m*/
        };
    }
    if (itemType == Integer.class || itemType == Integer.TYPE) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getInt(idx);
            }
        };
    }
    if (itemType == URL.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getURL(idx);
            }
        };
    }
    if (itemType == BigInteger.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                final BigDecimal res = rs.getBigDecimal(idx);
                return res == null ? null : res.toBigInteger();
            }
        };
    }
    if (itemType == BigDecimal.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBigDecimal(idx);
            }
        };
    }
    if (itemType == InputStream.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBinaryStream(idx);
            }
        };
    }
    if (itemType == Boolean.class || itemType == Boolean.TYPE) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBoolean(idx);
            }
        };
    }
    if (itemType == Blob.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBlob(idx);
            }
        };
    }
    if (itemType == java.sql.Date.class || itemType == java.util.Date.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getTimestamp(idx);
            }
        };
    }
    if (itemType instanceof Class) {
        final Class itemClass = (Class) itemType;
        final ColumnMapper columnMapper = ColumnMapperRegistry.lookup(itemClass);
        if (columnMapper != null) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    return columnMapper.map(rs, idx);
                }
            };
        }
        final Converter converter = ConvertUtils.lookup(itemClass);
        if (converter != null) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    String s = rs.getString(idx);
                    if (s == null) {
                        return null;
                    }
                    return converter.convert(itemClass, s);
                }
            };
        }
        if (Enum.class.isAssignableFrom((Class<?>) itemType)) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    String s = rs.getString(idx);
                    if (s == null) {
                        return null;
                    }
                    //noinspection unchecked
                    return Enum.valueOf((Class<Enum>) itemType, s);
                }
            };
        }
    }
    if (req) {
        throw new IllegalArgumentException("no mapping defined for " + itemType);
    }
    return null;
}

From source file:com.sf.ddao.orm.RSMapperFactoryRegistry.java

public static RowMapperFactory getScalarRowMapperFactory(final Type itemType, final String name, boolean req) {
    if (itemType == String.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getString(name);
            }/*w  w  w.  j  ava 2  s .c  om*/
        };
    }
    if (itemType == Integer.class || itemType == Integer.TYPE) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getInt(name);
            }
        };
    }
    if (itemType == URL.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getURL(name);
            }
        };
    }
    if (itemType == BigInteger.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                final BigDecimal res = rs.getBigDecimal(name);
                return res == null ? null : res.toBigInteger();
            }
        };
    }
    if (itemType == BigDecimal.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBigDecimal(name);
            }
        };
    }
    if (itemType == InputStream.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBinaryStream(name);
            }
        };
    }
    if (itemType == Boolean.class || itemType == Boolean.TYPE) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBoolean(name);
            }
        };
    }
    if (itemType == Blob.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBlob(name);
            }
        };
    }
    if (itemType == java.sql.Date.class || itemType == java.util.Date.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getTimestamp(name);
            }
        };
    }
    if (itemType instanceof Class) {
        final Class itemClass = (Class) itemType;
        final ColumnMapper columnMapper = ColumnMapperRegistry.lookup(itemClass);
        if (columnMapper != null) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    return columnMapper.map(rs, name);
                }
            };
        }
        final Converter converter = ConvertUtils.lookup(itemClass);
        if (converter != null) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    String s = rs.getString(name);
                    if (s == null) {
                        return null;
                    }
                    return converter.convert(itemClass, s);
                }
            };
        }
        if (Enum.class.isAssignableFrom((Class<?>) itemType)) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    String s = rs.getString(name);
                    if (s == null) {
                        return null;
                    }
                    //noinspection unchecked
                    return Enum.valueOf((Class<Enum>) itemType, s);
                }
            };
        }
    }
    if (req) {
        throw new IllegalArgumentException("no mapping defined for " + itemType);
    }
    return null;
}

From source file:module.entities.NameFinder.DB.java

public static void InsertNewLemma2DB(String lemma, String category) throws SQLException {
    String selectSQL = "SELECT * FROM  WHERE  = ? ";
    PreparedStatement preparedStatement = connection.prepareStatement(selectSQL);
    preparedStatement.setString(1, lemma);
    preparedStatement.setString(2, category);
    ResultSet rs = preparedStatement.executeQuery();
    String insertSQL = "INSERT INTO  " + "(lemma_text,lemma_category) VALUES" + "(?,?)";
    PreparedStatement prepStatement = connection.prepareStatement(insertSQL);
    int id = -1;// www.ja v  a 2 s  . co m
    if (rs.next()) {
        id = rs.getInt(1);
    } else {
        prepStatement.setString(1, lemma);
        prepStatement.setString(2, category);
        prepStatement.addBatch();
    }
    prepStatement.executeBatch();
    prepStatement.close();
}

From source file:com.concursive.connect.web.modules.profile.utils.ProjectUtils.java

public static int retrieveWebcastIdFromProjectId(Connection db, int projectId) throws SQLException {
    int webcastId = -1;
    PreparedStatement pst = db//from  w  w w  .  j ava  2 s .  c  o m
            .prepareStatement("SELECT webcast_id " + "FROM project_webcast " + "WHERE project_id = ? ");
    pst.setInt(1, projectId);
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
        webcastId = rs.getInt("webcast_id");
    }
    rs.close();
    pst.close();

    return webcastId;
}

From source file:com.aurel.track.dbase.InitReportTemplateBL.java

private static void addReportTemplateToDatabase(Integer oid, String name, String expfmt, String description) {

    String stmt = "INSERT INTO TEXPORTTEMPLATE (OBJECTID,NAME,EXPORTFORMAT,REPOSITORYTYPE,DESCRIPTION,PROJECT,PERSON,REPORTTYPE)"
            + "VALUES (" + oid + ",'" + name + "','" + expfmt + "',2,'" + description
            + "',NULL,1,'Jasper Report')";

    Connection coni = null;/* ww w .j av  a  2  s  .c  o m*/
    Connection cono = null;
    ResultSet rs = null;
    try {
        coni = InitDatabase.getConnection();
        cono = InitDatabase.getConnection();
        PreparedStatement istmt = coni
                .prepareStatement("SELECT MAX(OBJECTID) FROM TEXPORTTEMPLATE WHERE OBJECTID < 100");
        Statement ostmt = cono.createStatement();

        rs = istmt.executeQuery();
        Integer maxInt = 0;
        if (rs != null) {
            rs.next();
            maxInt = rs.getInt(1);
        }
        if (oid.intValue() <= maxInt.intValue()) {
            return;
        }

        istmt = coni.prepareStatement("SELECT * FROM TEXPORTTEMPLATE WHERE OBJECTID = ?");
        istmt.setInt(1, oid);

        rs = istmt.executeQuery();
        if (rs == null || !rs.next()) {
            LOGGER.info("Adding report template with OID " + oid + ": " + name);
            try {
                ostmt.executeUpdate(stmt);
            } catch (Exception exc) {
                LOGGER.error("Problem...: " + exc.getMessage());
            }
        }
    } catch (Exception e) {
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
    } finally {
        try {
            if (rs != null)
                rs.close();
            if (coni != null)
                coni.close();
            if (cono != null)
                cono.close();
        } catch (Exception e) {
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
    }
}