List of usage examples for java.sql ResultSet getRow
int getRow() throws SQLException;
From source file:com.tera.common.database.query.CQueryService.java
@Override public int[] getUsedIds(String query, String idColumn) { PreparedStatement statement = prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = null; try {// w w w .j ava 2 s . c o m resultSet = statement.executeQuery(); resultSet.last(); int count = resultSet.getRow(); resultSet.beforeFirst(); int[] ids = new int[count]; for (int i = 0; i < count; i++) { resultSet.next(); ids[i] = resultSet.getInt(idColumn); } return ids; } catch (SQLException e) { log.error("Can't get id's using query {}", query, e); } finally { close(resultSet, statement); } return new int[0]; }
From source file:com.vigglet.util.ModelUtilBase.java
public Collection<T> getPkMap() { Collection<T> result = null; try {/*from ww w . j av a 2 s . c om*/ PreparedStatement stmt = getSelectAllQuery(); ResultSet rs = stmt.executeQuery(); rs.last(); result = new ArrayList<>(rs.getRow()); rs.beforeFirst(); while (rs.next()) { result.add(readModel(rs)); } rs.close(); stmt.close(); } catch (SQLException ex) { logger.log(Level.SEVERE, null, ex); } return result; }
From source file:com.vigglet.util.ModelUtilBase.java
public Collection<T> findByCompany(int company) { Collection<T> result = null; try {//from ww w. j av a 2s.c o m PreparedStatement stmt = getFindByCompanyQuery(); stmt.setInt(1, company); ResultSet rs = stmt.executeQuery(); rs.last(); result = new ArrayList<>(rs.getRow()); rs.beforeFirst(); while (rs.next()) { result.add(readModel(rs)); } rs.close(); stmt.close(); } catch (SQLException ex) { logger.log(Level.SEVERE, null, ex); } return result; }
From source file:Connexion.Charts.java
public Charts() { try {/*from w w w . j a v a 2s . c o m*/ Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { /* Grer les ventuelles erreurs ici. */ } int a = 0; int b = 0; int c = 0; try { ResultSet resultat1 = this.connect .createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE) .executeQuery("SELECT code_service FROM hospitalisation WHERE code_service = 'REA'"); // on rcupre le nombre de lignes de la requte if (resultat1.last()) { a = resultat1.getRow(); } System.out.println(a); } catch (SQLException e) { e.printStackTrace(); } try { ResultSet resultat2 = this.connect .createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE) .executeQuery("SELECT code_service FROM hospitalisation WHERE code_service = 'CHG'"); // on rcupre le nombre de lignes de la requte if (resultat2.last()) { b = resultat2.getRow(); } System.out.println(b); } catch (SQLException e) { e.printStackTrace(); } try { ResultSet resultat3 = this.connect .createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE) .executeQuery("SELECT code_service FROM hospitalisation WHERE code_service = 'CAR'"); // on rcupre le nombre de lignes de la requte if (resultat3.last()) { c = resultat3.getRow(); } } catch (SQLException e) { e.printStackTrace(); } DefaultPieDataset union = new DefaultPieDataset(); //remplir l'ensemble union.setValue("REA", a); union.setValue("CAR", b); union.setValue("CHG", c); JFreeChart repart = ChartFactory.createPieChart3D("Nombre d'hospitalisation par service", union, true, true, false); ChartPanel crepart = new ChartPanel(repart); this.add(crepart); this.pack(); this.setVisible(true); }
From source file:com.vigglet.util.ModelUtilBase.java
public List<T> getLimitedList(int company, int limit) { long startMs = System.currentTimeMillis(); logger.log(Level.INFO, "Fetching limited (" + limit + ") list..."); List<T> result = null;// w w w.j av a 2s .co m try { PreparedStatement stmt = getLimitedQuery(); stmt.setInt(1, company); stmt.setInt(2, limit); ResultSet rs = stmt.executeQuery(); rs.last(); result = new ArrayList<>(rs.getRow()); rs.beforeFirst(); while (rs.next()) { result.add(readModel(rs)); } rs.close(); stmt.close(); } catch (SQLException ex) { logger.log(Level.SEVERE, null, ex); } long endMs = System.currentTimeMillis(); logger.log(Level.INFO, "Done in " + (endMs - startMs) + " ms (" + (endMs - startMs) / 1000 + " s)"); return result; }
From source file:com.vigglet.util.ModelUtilBase.java
public List<T> getOrderdList(int company, String orderField, String orderDirection) { List<T> result = null;/* ww w .j av a 2 s.c om*/ try { PreparedStatement stmt = null; if (orderDirection != null && orderDirection.length() > 0) { stmt = getOrderByQuery(orderField, orderDirection); } else { stmt = getOrderByQuery(orderField); } stmt.setInt(1, company); ResultSet rs = stmt.executeQuery(); rs.last(); result = new ArrayList<>(rs.getRow()); rs.beforeFirst(); while (rs.next()) { result.add(readModel(rs)); } rs.close(); stmt.close(); } catch (SQLException ex) { logger.log(Level.SEVERE, null, ex); } return result; }
From source file:agendavital.modelo.data.Noticia.java
/** * Funcion modificadora de noticias//from w ww . ja va 2s. c om * * @throws agendavital.modelo.excepciones.ConexionBDIncorrecta */ public void Update() throws ConexionBDIncorrecta { try (Connection conexion = ConfigBD.conectar()) { String update = String.format( "UPDATE noticias SET titulo = %s, link = %s, fecha = %s, categoria = %s, cuerpo = %s WHERE id_noticia = %d;", ConfigBD.String2Sql(getTitulo(), false), ConfigBD.String2Sql(getLink(), false), ConfigBD.String2Sql(getFecha(), false), ConfigBD.String2Sql(getCategoria(), false), ConfigBD.String2Sql(getCuerpo(), false), getId()); conexion.createStatement().executeUpdate(update); String eliminaMNE = String.format("DELETE from momentos_noticias_etiquetas WHERE id_noticia = %d", id); conexion.createStatement().executeUpdate(eliminaMNE); for (String tag : tags) { String consultaTag = String.format("SELECT id_etiqueta from etiquetas WHERE Nombre = %s;", ConfigBD.String2Sql(tag, false)); ResultSet rs = conexion.createStatement().executeQuery(consultaTag); rs.next(); int idTag = (rs.getRow() == 1) ? rs.getInt("id_etiqueta") : -1; if (idTag == -1) { String insertTag = String.format("INSERT INTO etiquetas (nombre) VALUES (%s);", ConfigBD.String2Sql(tag, false)); conexion.createStatement().executeUpdate(insertTag); idTag = ConfigBD.LastId("etiquetas"); } String insertaMNE = String.format( "INSERT INTO momentos_noticias_etiquetas (id_noticia, id_etiqueta) VALUES(%d, %d)", id, idTag); conexion.createStatement().executeUpdate(insertaMNE); } } catch (SQLException e) { throw new ConexionBDIncorrecta(); } }
From source file:com.micromux.cassandra.jdbc.MetadataResultSetsTest.java
private String toString(ResultSet result) throws SQLException { StringBuilder sb = new StringBuilder(); while (result.next()) { ResultSetMetaData metadata = result.getMetaData(); int colCount = metadata.getColumnCount(); sb.append(String.format("(%d) ", result.getRow())); for (int i = 1; i <= colCount; i++) { sb.append(" " + showColumn(i, result)); }/*from www . ja v a 2 s .co m*/ sb.append("\n"); } return sb.toString(); }
From source file:at.stefanproell.ResultSetVerification.ResultSetVerificationAPI.java
public int getResultSetRowCount(ResultSet rs) { int rows = 0; try {//from ww w . j av a 2 s.c o m if (rs.last()) { rows = rs.getRow(); // Move to beginning rs.beforeFirst(); } } catch (SQLException e) { e.printStackTrace(); } this.logger.info("Returned rows: " + rows); return rows; }
From source file:edu.caltechUcla.sselCassel.projects.jMarkets.server.data.DBConnector.java
/** Checks if name='value' exists in the given table. If it does, returns its id. If not * inserts it into the table and returns the generated id. Works only for tables that * generate IDs, only for VARCHAR values, and only for tables that have only two columns: * ID and some VARCHAR column (given by name) */ public int insertIfDNE(String table, String name, String value) { Connection conn = null;/*from w w w . ja v a 2 s. co m*/ Object[] results = null; try { conn = getConnection(); String query = "select id from " + table + " where " + name + "='" + value + "'"; results = executeQuery(query, conn); int id = -1; //Check if name=value is there -- if so, return its id if (results != null) { ResultSet rs = (ResultSet) results[0]; if (!rs.wasNull()) { rs.last(); int size = rs.getRow(); if (size > 0) { id = rs.getInt("id"); return id; } } } //If the security was not found then add it and return the generated id String update = "insert into " + table + " values(0, '" + value + "')"; results = executeUpdate(update, conn); ResultSet rs = (ResultSet) results[0]; rs.next(); id = rs.getInt(1); return id; } catch (SQLException e) { log.error("Failed to perform a 'add if does not exist' table insert: " + e); return -1; } finally { closeQuery(results, conn); } }