List of usage examples for java.sql ResultSet getInt
int getInt(String columnLabel) throws SQLException;
ResultSet
object as an int
in the Java programming language. From source file:org.spc.ofp.data.Repository.java
public static Integer readInteger(final ResultSet rs, final String columnName) throws SQLException { final int value = rs.getInt(columnName); return rs.wasNull() ? null : Integer.valueOf(value); }
From source file:com.dangdang.ddframe.rdb.sharding.example.jdbc.Main.java
private static void printGroupBy(final DataSource dataSource) throws SQLException { String sql = "SELECT o.user_id, COUNT(*) FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id GROUP BY o.user_id"; try (Connection conn = dataSource.getConnection(); PreparedStatement preparedStatement = conn.prepareStatement(sql)) { ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { System.out.println("user_id: " + rs.getInt(1) + ", count: " + rs.getInt(2)); }/*w w w. j a v a2s .c o m*/ } }
From source file:com.concursive.connect.web.modules.activity.utils.ProjectHistoryUtils.java
public static int findNextThreadPosition(Connection db, ProjectHistory parentProjectHistory) throws SQLException { int count = 0; PreparedStatement pst = db/* ww w .j av a 2s. c om*/ .prepareStatement("SELECT count(*) AS ccount " + "FROM project_history " + "WHERE lineage LIKE ? "); pst.setString(1, parentProjectHistory.getLineage() + parentProjectHistory.getId() + "/%"); ResultSet rs = pst.executeQuery(); if (rs.next()) { count = rs.getInt("ccount"); } rs.close(); pst.close(); return (parentProjectHistory.getThreadPosition() + count + 1); }
From source file:com.concursive.connect.web.modules.activity.utils.ProjectHistoryUtils.java
public static int findNextPosition(Connection db, int projectHistoryId) throws SQLException { int position = 0; PreparedStatement pst = db.prepareStatement("SELECT max(position) AS position " + "FROM project_history " + "WHERE history_id = ? OR top_id = ? "); pst.setInt(1, projectHistoryId);/*from ww w . j av a 2s . c o m*/ pst.setInt(2, projectHistoryId); ResultSet rs = pst.executeQuery(); if (rs.next()) { position = rs.getInt("position"); } rs.close(); pst.close(); return (position + 1); }
From source file:com.concursive.connect.web.modules.activity.utils.ProjectHistoryUtils.java
public static int queryAdditionalCommentsCount(Connection db, ProjectHistory projectHistory) throws SQLException { int count = 0; int topId = projectHistory.getTopId(); if (topId == -1) { topId = projectHistory.getId();//w w w.j a v a2 s.co m } PreparedStatement pst = db.prepareStatement("SELECT count(*) AS comment_count " + "FROM project_history " + "WHERE top_id = ? AND position > ? "); pst.setInt(1, topId); pst.setInt(2, projectHistory.getPosition()); ResultSet rs = pst.executeQuery(); if (rs.next()) { count = rs.getInt("comment_count"); } rs.close(); pst.close(); return count; }
From source file:com.sql.EMail.java
/** * Inserts email message into email table. * * @param eml EmailMessageModel//w w w . j a va 2 s. c o m * @return Integer - generated key of the email */ public static int InsertEmail(EmailMessageModel eml) { Connection conn = null; PreparedStatement ps = null; try { conn = DBConnection.connectToDB(); String sql = "INSERT INTO EMail (" + "section, " + "emailFrom, " + "emailTo, " + "emailSubject, " + "sentDate, " + "receivedDate, " + "emailCC, " + "emailBCC, " + "emailBody, " + "emailBodyFileName, " + "readyToFile " + ") VALUES (" + "?, " //1 + "?, " //2 + "?, " //3 + "?, " //4 + "?, " //5 + "?, " //6 + "?, " //7 + "?, " //8 + "?, " //9 + "?, " //10 + "0)"; // Ready to File False ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setString(1, StringUtils.left(eml.getSection(), 4)); ps.setString(2, StringUtils.left(eml.getEmailFrom(), 200)); ps.setString(3, eml.getEmailTo()); ps.setString(4, eml.getEmailSubject()); ps.setTimestamp(5, eml.getSentDate()); ps.setTimestamp(6, eml.getReceivedDate()); ps.setString(7, eml.getEmailCC()); ps.setString(8, eml.getEmailBCC()); ps.setString(9, eml.getEmailBody()); ps.setString(10, eml.getEmailBodyFileName()); ps.executeUpdate(); ResultSet newRow = ps.getGeneratedKeys(); if (newRow.next()) { return newRow.getInt(1); } } catch (SQLException ex) { ExceptionHandler.Handle(ex); } finally { DbUtils.closeQuietly(conn); DbUtils.closeQuietly(ps); } return 0; }
From source file:com.oracle.tutorial.jdbc.FilteredRowSetSample.java
public static void viewTable(Connection con) throws SQLException { Statement stmt = null;//from w ww .jav a2s . com String query = "select * from COFFEE_HOUSES"; try { stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); while (rs.next()) { System.out.println(rs.getInt("STORE_ID") + ", " + rs.getString("CITY") + ", " + rs.getInt("COFFEE") + ", " + rs.getInt("MERCH") + ", " + rs.getInt("TOTAL")); } } catch (SQLException e) { JDBCTutorialUtilities.printSQLException(e); } finally { if (stmt != null) { stmt.close(); } } }
From source file:net.sf.sprockets.sql.Statements.java
/** * Execute the query, get the int value in the first row and column of the result set, and close * the statement.//from w w w . ja va2 s . c o m * * @param stmt * must already have parameters set * @return {@link Integer#MIN_VALUE} if the result set is empty */ public static int firstInt(PreparedStatement stmt) throws SQLException { ResultSet rs = stmt.executeQuery(); int i = rs.next() ? rs.getInt(1) : Integer.MIN_VALUE; stmt.close(); return i; }
From source file:com.thoughtworks.go.server.datamigration.M001.java
static boolean required(Connection cxn) throws SQLException { try (Statement s = cxn.createStatement()) { final ResultSet rs = s .executeQuery("SELECT COUNT(*) as remaining FROM pipelineselections WHERE version = 0"); rs.next();/*from www. j av a2 s . co m*/ return rs.getInt("remaining") > 0; } }
From source file:com.oracle.tutorial.jdbc.SuppliersTable.java
public static void viewTable(Connection con) throws SQLException { Statement stmt = null;/* www . jav a2s . c o m*/ String query = "select SUP_ID, SUP_NAME, STREET, CITY, STATE, ZIP from SUPPLIERS"; try { stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); while (rs.next()) { int supplierID = rs.getInt("SUP_ID"); String supplierName = rs.getString("SUP_NAME"); String street = rs.getString("STREET"); String city = rs.getString("CITY"); String state = rs.getString("STATE"); String zip = rs.getString("ZIP"); System.out.println( supplierName + "(" + supplierID + "): " + street + ", " + city + ", " + state + ", " + zip); } } catch (SQLException e) { JDBCTutorialUtilities.printSQLException(e); } finally { if (stmt != null) { stmt.close(); } } }