List of usage examples for java.sql ResultSet getString
String getString(String columnLabel) throws SQLException;
ResultSet
object as a String
in the Java programming language. From source file:com.oracle.tutorial.jdbc.CachedRowSetSample.java
public static void viewTable(Connection con) throws SQLException { Statement stmt = null;/*from ww w . j av a 2 s. co m*/ String query = "select * from MERCH_INVENTORY"; try { stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); while (rs.next()) { System.out.println("Found item " + rs.getInt("ITEM_ID") + ": " + rs.getString("ITEM_NAME") + " (" + rs.getInt("QUAN") + ")"); } } catch (SQLException e) { JDBCTutorialUtilities.printSQLException(e); } finally { if (stmt != null) { stmt.close(); } } }
From source file:org.usip.osp.graphs.GraphServer.java
public static Chart getChartByNameAndRound(String chart_id, String game_round, String sim_id, String values_table) {//from w ww . j a va 2s . c o m Chart cci = new Chart(); String selectChartInfo = "SELECT * FROM `charts` where sf_id = '" //$NON-NLS-1$ + chart_id + "'"; //$NON-NLS-1$ try { Connection connection = MultiSchemaHibernateUtil.getConnection(); Statement stmt = connection.createStatement(); ResultSet rst = stmt.executeQuery(selectChartInfo); if (rst.next()) { String chart_type = rst.getString("type"); //$NON-NLS-1$ String chart_title = rst.getString("title"); //$NON-NLS-1$ String x_axis_title = rst.getString("x_axis_title"); //$NON-NLS-1$ String y_axis_title = rst.getString("y_axis_title"); //$NON-NLS-1$ //cci.height = rst.getInt("height"); //cci.width = rst.getInt("width"); String howToGetData = rst.getString("first_data_source"); //$NON-NLS-1$ howToGetData = howToGetData.replace("[simulation_id]", sim_id); //$NON-NLS-1$ howToGetData = howToGetData.replace("[sim_value_table_name]", values_table); //$NON-NLS-1$ Statement stmt2 = connection.createStatement(); ResultSet rst2 = stmt.executeQuery(howToGetData); JFreeChart chart = null; if (chart_type.equalsIgnoreCase("LineChart")) { //$NON-NLS-1$ DefaultCategoryDataset cd = DataGatherer.getChartData(chart_type, game_round, howToGetData); chart = ChartFactory.createLineChart(chart_title, // chart // title x_axis_title, // domain axis label y_axis_title, // range axis label cd, // data PlotOrientation.VERTICAL, // orientation false, // include legend true, // tooltips false // urls ); } else if (chart_type.equalsIgnoreCase("BarChart")) { //$NON-NLS-1$ DefaultPieDataset dataset = DataGatherer.getPieData(chart_id, game_round, howToGetData); chart = ChartFactory.createPieChart(chart_title, dataset, true, // legend? true, // tooltips? false // URLs? ); } cci.setThis_chart(chart); } // End of loop if found chart info connection.close(); } catch (Exception e) { e.printStackTrace(); } return cci; }
From source file:com.linkedin.databus.bootstrap.utils.BootstrapDBCleanerMain.java
private static List<String> getSourceNames(Connection conn) throws SQLException { List<String> srcNames = new ArrayList<String>(); String sql = "select src from bootstrap_sources"; ResultSet rs = null; PreparedStatement stmt = null; try {//from ww w .j a va 2 s .co m stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); while (rs.next()) { String name = rs.getString(1); srcNames.add(name); } } finally { DBHelper.close(rs, stmt, null); } return srcNames; }
From source file:ch.newscron.referral.ReferralManager.java
/** * Helper function: given a resultSet, processes the data into a list of CustomerShortURL objects * @param resultSet a ResultSet returned from a database query * @return a List of CustomerShortURL objects *///from w ww. ja v a2 s. com private static List<CustomerShortURL> parseResultSet(ResultSet resultSet) { List<CustomerShortURL> shortURLList = new ArrayList<>(); try { while (resultSet.next()) { CustomerShortURL newShortURL = new CustomerShortURL(Long.parseLong(resultSet.getString("custId")), resultSet.getString("shortUrl")); shortURLList.add(newShortURL); } return shortURLList; } catch (SQLException ex) { Logger.getLogger(ReferralManager.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:com.l2jfree.gameserver.util.TableOptimizer.java
public static void optimize() { Connection con = null;/*from w w w. j a v a 2s . c o m*/ try { con = L2DatabaseFactory.getInstance().getConnection(); Statement st = con.createStatement(); final ArrayList<String> tables = new ArrayList<String>(); { ResultSet rs = st.executeQuery("SHOW FULL TABLES"); while (rs.next()) { String tableType = rs.getString(2/*"Table_type"*/); if (tableType.equals("VIEW")) continue; tables.add(rs.getString(1)); } rs.close(); } { ResultSet rs = st.executeQuery("CHECK TABLE " + StringUtils.join(tables, ",")); while (rs.next()) { String table = rs.getString("Table"); String msgType = rs.getString("Msg_type"); String msgText = rs.getString("Msg_text"); if (msgType.equals("status")) if (msgText.equals("OK")) continue; _log.warn("TableOptimizer: CHECK TABLE " + table + ": " + msgType + " -> " + msgText); } rs.close(); _log.info("TableOptimizer: Database tables have been checked."); } { ResultSet rs = st.executeQuery("ANALYZE TABLE " + StringUtils.join(tables, ",")); while (rs.next()) { String table = rs.getString("Table"); String msgType = rs.getString("Msg_type"); String msgText = rs.getString("Msg_text"); if (msgType.equals("status")) if (msgText.equals("OK") || msgText.equals("Table is already up to date")) continue; _log.warn("TableOptimizer: ANALYZE TABLE " + table + ": " + msgType + " -> " + msgText); } rs.close(); _log.info("TableOptimizer: Database tables have been analyzed."); } { ResultSet rs = st.executeQuery("OPTIMIZE TABLE " + StringUtils.join(tables, ",")); while (rs.next()) { String table = rs.getString("Table"); String msgType = rs.getString("Msg_type"); String msgText = rs.getString("Msg_text"); if (msgType.equals("status")) if (msgText.equals("OK") || msgText.equals("Table is already up to date")) continue; if (msgType.equals("note")) if (msgText.equals("Table does not support optimize, doing recreate + analyze instead")) continue; _log.warn("TableOptimizer: OPTIMIZE TABLE " + table + ": " + msgType + " -> " + msgText); } rs.close(); _log.info("TableOptimizer: Database tables have been optimized."); } st.close(); } catch (Exception e) { _log.warn("TableOptimizer: Cannot optimize database tables!", e); } finally { L2DatabaseFactory.close(con); } }
From source file:com.nortal.petit.converter.util.ResultSetHelper.java
public static String getString(ResultSet rs, ColumnPosition column) throws SQLException { return column.isNamed ? rs.getString(column.getName()) : rs.getString(column.getIndex()); }
From source file:jdbc.JdbcUtils.java
/** * Retrieve a JDBC column value from a ResultSet, using the most appropriate * value type. The returned value should be a detached value object, not * having any ties to the active ResultSet: in particular, it should not be * a Blob or Clob object but rather a byte array respectively String * representation.// ww w. java 2 s . c o m * <p> * Uses the <code>getObject(index)</code> method, but includes additional * "hacks" to get around Oracle 10g returning a non-standard object for its * TIMESTAMP datatype and a <code>java.sql.Date</code> for DATE columns * leaving out the time portion: These columns will explicitly be extracted * as standard <code>java.sql.Timestamp</code> object. * * @param rs * is the ResultSet holding the data * @param index * is the column index * @return the value object * @see java.sql.Blob * @see java.sql.Clob * @see java.sql.Timestamp * @see oracle.sql.TIMESTAMP */ public static Object getResultSetValue(ResultSet rs, int index) throws SQLException { Object obj = rs.getObject(index); if (obj instanceof Blob) { obj = rs.getBytes(index); } else if (obj instanceof Clob) { obj = rs.getString(index); } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.TIMESTAMP")) { obj = rs.getTimestamp(index); } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.DATE")) { String metaDataClassName = rs.getMetaData().getColumnClassName(index); if ("java.sql.Timestamp".equals(metaDataClassName) || "oracle.sql.TIMESTAMP".equals(metaDataClassName)) { obj = rs.getTimestamp(index); } else { obj = rs.getDate(index); } } else if (obj != null && obj instanceof Date) { if ("java.sql.Timestamp".equals(rs.getMetaData().getColumnClassName(index))) { obj = rs.getTimestamp(index); } } return obj; }
From source file:net.ontopia.topicmaps.cmdlineutils.rdbms.RDBMSIndexTool.java
protected static Map getPrimaryKeys(String table_name, DatabaseMetaData dbm) throws SQLException { // returns { table_name(colname,...) : index_name } Map result = new HashMap(5); ResultSet rs = dbm.getPrimaryKeys(null, null, table_name); String prev_index_name = null; String columns = null;/*from w w w. j a v a 2 s. c o m*/ while (rs.next()) { String index_name = rs.getString(6); if (prev_index_name != null && !prev_index_name.equals(index_name)) { result.put(table_name + '(' + columns + ')', prev_index_name); columns = null; } // column_name might be quoted, so unquote it before proceeding String column_name = unquote(rs.getString(4), dbm.getIdentifierQuoteString()); if (columns == null) columns = column_name; else columns = columns + "," + column_name; prev_index_name = index_name; } rs.close(); if (prev_index_name != null) result.put(table_name + '(' + columns + ')', prev_index_name); return result; }
From source file:de.sqlcoach.db.jdbc.DBAppUser.java
/** * Sets the model./* w w w .j a v a 2 s .c o m*/ * * @param resultset * the resultset * @param model * the model * * @throws SQLException * the SQL exception */ private static void setModel(ResultSet resultset, AppUser model) throws SQLException { model.setId(resultset.getInt("id")); model.setNickname(resultset.getString("nickname")); model.setPassword(resultset.getString("password")); model.setTitle(resultset.getString("title")); model.setFirstname(resultset.getString("firstname")); model.setLastname(resultset.getString("lastname")); model.setEmail(resultset.getString("email")); model.setRole(resultset.getString("role")); model.setDatecreate(resultset.getTimestamp("datecreate")); model.setDatelastmod(resultset.getTimestamp("datelastmod")); }
From source file:at.alladin.rmbt.db.dao.QoSTestResultDao.java
/** * // w ww .j a va 2s .c om * @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; }