List of usage examples for org.apache.commons.dbutils DbUtils closeQuietly
public static void closeQuietly(Statement stmt)
Statement
, avoid closing if null and hide any SQLExceptions that occur. From source file:DbUtilsUseBeanMySQL.java
public static void main(String[] args) { Connection conn = null;//from w ww . j a va2 s. c o m String jdbcURL = "jdbc:mysql://localhost/octopus"; String jdbcDriver = "com.mysql.jdbc.Driver"; String user = "root"; String password = "root"; try { DbUtils.loadDriver(jdbcDriver); conn = DriverManager.getConnection(jdbcURL, user, password); QueryRunner qRunner = new QueryRunner(); List beans = (List) qRunner.query(conn, "select id, name from animals_table", new BeanListHandler(Employee.class)); for (int i = 0; i < beans.size(); i++) { Employee bean = (Employee) beans.get(i); bean.print(); } } catch (SQLException e) { // handle the exception e.printStackTrace(); } finally { DbUtils.closeQuietly(conn); } }
From source file:DbUtilsUseMapMySQL.java
public static void main(String[] args) { Connection conn = null;/*from w ww . j av a2s . c o m*/ String jdbcURL = "jdbc:mysql://localhost/octopus"; String jdbcDriver = "com.mysql.jdbc.Driver"; String user = "root"; String password = "root"; try { DbUtils.loadDriver(jdbcDriver); conn = DriverManager.getConnection(jdbcURL, user, password); QueryRunner qRunner = new QueryRunner(); List mapList = (List) qRunner.query(conn, "select id, name from animals_table", new MapListHandler()); for (int i = 0; i < mapList.size(); i++) { Map map = (Map) mapList.get(i); System.out.println("id=" + map.get("id")); System.out.println("name=" + map.get("name")); System.out.println("-----------------"); } System.out.println("DbUtils_UseMap_MySQL: end."); } catch (SQLException e) { // handle the exception e.printStackTrace(); } finally { DbUtils.closeQuietly(conn); } }
From source file:com.intelius.iap4.TigerLineHit.java
public static void main(String[] args) { String _tigerDs = "jdbc:h2:/home/sxu/playground/tiger"; ResultSet rs = null;//from w w w. j a v a 2 s .c om PreparedStatement ps = null; List<TigerLineHit> ret = new ArrayList<TigerLineHit>(); try { // if (_tigerDs instanceof JdbcDataSource) { // JdbcDataSource ds = (JdbcDataSource) _tigerDs; // conn = ds.getPooledConnection().getConnection(); // }else{ // conn = _tigerDs.getConnection(); // } //try address "540 westerly parkway, state college, pa 16801" Class.forName("org.h2.Driver"); Connection conn = DriverManager.getConnection(_tigerDs, "sa", ""); ps = conn.prepareStatement(generateSelectQuery("PA")); int i = 1; String streetNum = "540"; String zip = "16801"; ps.setString(i++, "Westerly"); ps.setString(i++, streetNum); ps.setString(i++, streetNum); ps.setString(i++, streetNum); ps.setString(i++, streetNum); ps.setString(i++, streetNum); ps.setString(i++, streetNum); ps.setString(i++, streetNum); ps.setString(i++, streetNum); ps.setString(i++, zip); ps.setString(i++, zip); rs = ps.executeQuery(); while (rs.next()) { TigerLineHit hit = new TigerLineHit(); hit.streetNum = streetNum; hit.tlid = rs.getLong("tlid"); hit.frAddL = rs.getString("fraddl"); hit.frAddR = rs.getString("fraddr"); hit.toAddL = rs.getString("toaddl"); hit.toAddR = rs.getString("toaddr"); hit.zipL = rs.getString("zipL"); hit.zipR = rs.getString("zipR"); hit.toLat = rs.getFloat("tolat"); hit.toLon = rs.getFloat("tolong"); hit.frLat = rs.getFloat("frlat"); hit.frLon = rs.getFloat("tolong"); hit.lat1 = rs.getFloat("lat1"); hit.lat2 = rs.getFloat("lat2"); hit.lat3 = rs.getFloat("lat3"); hit.lat4 = rs.getFloat("lat4"); hit.lat5 = rs.getFloat("lat5"); hit.lat6 = rs.getFloat("lat6"); hit.lat7 = rs.getFloat("lat7"); hit.lat8 = rs.getFloat("lat8"); hit.lat9 = rs.getFloat("lat9"); hit.lat10 = rs.getFloat("lat10"); hit.lon1 = rs.getFloat("long1"); hit.lon2 = rs.getFloat("long2"); hit.lon3 = rs.getFloat("long3"); hit.lon4 = rs.getFloat("long4"); hit.lon5 = rs.getFloat("long5"); hit.lon6 = rs.getFloat("long6"); hit.lon7 = rs.getFloat("long7"); hit.lon8 = rs.getFloat("long8"); hit.lon9 = rs.getFloat("long9"); hit.lon10 = rs.getFloat("long10"); hit.fedirp = rs.getString("fedirp"); hit.fetype = rs.getString("fetype"); hit.fedirs = rs.getString("fedirs"); ret.add(hit); // System.out.println(ret.toString()); // } } catch (Exception e) { e.printStackTrace(); } finally { //DbUtils.closeQuietly(conn); DbUtils.closeQuietly(rs); DbUtils.closeQuietly(ps); } //return ret; }
From source file:com.sql.DBBackupScript.java
/** * Backup database command/*from ww w. j av a 2 s . co m*/ * * @param databaseName String */ public static void backupDB(String databaseName) { Connection conn = null; PreparedStatement ps = null; try { conn = DBConnection.connectToDBforBackup(); String sql = "BACKUP DATABASE " + databaseName + " TO DISK = '" + Global.getDatabaseBackupPath() + "' "; ps = conn.prepareStatement(sql); ps.execute(); } catch (SQLException ex) { ExceptionHandler.Handle(ex); } finally { DbUtils.closeQuietly(ps); DbUtils.closeQuietly(conn); } }
From source file:com.sql.ServerEmailControl.java
/** * Update completion time of current thread. * /* ww w .ja v a 2s. c o m*/ * @param column */ public static void updateCompletionTime(String column) { Connection conn = null; PreparedStatement ps = null; try { conn = DBConnection.connectToDB(); String sql = "UPDATE ServerEmailControl SET " + column + " = GETDATE() WHERE " + "id = (SELECT TOP 1 id FROM ServerEmailControl)"; ps = conn.prepareStatement(sql); ps.executeUpdate(); } catch (SQLException ex) { ExceptionHandler.Handle(ex); } finally { DbUtils.closeQuietly(ps); DbUtils.closeQuietly(conn); } }
From source file:com.sql.EmailAttachment.java
/** * Insert attachement into received email attachment database table. * //from w w w.j a v a 2 s . c o m * @param EmailID Integer * @param fileName String */ public static void insertEmailAttachment(int EmailID, String fileName) { Connection conn = null; PreparedStatement ps = null; try { conn = DBConnection.connectToDB(); String sql = "INSERT INTO EmailAttachment (" + "emailID, " + "fileName " + ") VALUES (" + "?, " + "?)"; ps = conn.prepareStatement(sql); ps.setInt(1, EmailID); ps.setString(2, fileName); ps.executeUpdate(); } catch (SQLException ex) { ExceptionHandler.Handle(ex); } finally { DbUtils.closeQuietly(conn); DbUtils.closeQuietly(ps); } }
From source file:com.sql.Audit.java
/** * Removes old audits based on specific time frame. *///from w w w. j a va2s . c o m public static void removeOldAudits() { Connection conn = null; PreparedStatement ps = null; try { conn = DBConnection.connectToDB(); String sql = "DELETE FROM Audit WHERE " + "date < dateadd(" + Global.getAuditTimeFrame() + ", -" + Global.getAuditTimeAmount() + ", getdate())"; ps = conn.prepareStatement(sql); ps.executeUpdate(); } catch (SQLException ex) { ExceptionHandler.Handle(ex); } finally { DbUtils.closeQuietly(conn); DbUtils.closeQuietly(ps); } }
From source file:com.sql.SystemErrorEmailList.java
/** * Gathers a list of active email addresses to send to for the email * for the daily crash report email.//from www. j a v a 2 s. c o m * * @return */ public static List<String> getActiveEmailAddresses() { List<String> list = new ArrayList(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = DBConnection.connectToDB(); String sql = "SELECT EmailAddress " + "FROM SystemErrorEmailList " + "WHERE active = 1"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { if (rs.getString("EmailAddress") != null) { list.add(rs.getString("EmailAddress")); } } } catch (SQLException ex) { ExceptionHandler.Handle(ex); } finally { DbUtils.closeQuietly(conn); DbUtils.closeQuietly(ps); DbUtils.closeQuietly(rs); } return list; }
From source file:com.manydesigns.portofino.database.DbUtil.java
public static void closeResultSetAndStatement(ResultSet rs) { try {//from ww w . j a v a2 s . c o m Statement st = rs.getStatement(); DbUtils.closeQuietly(st); } catch (Throwable e) { logger.debug("Could not close statement", e); } try { DbUtils.closeQuietly(rs); } catch (Throwable e) { logger.debug("Could not close result set", e); } }
From source file:com.sql.EmailOutAttachment.java
/** * Gathers a list of attachments for a specific email address. * //from w w w .j a v a2 s. co m * @param emailID Integer * @return List (EmailOutAttachmentModel) */ public static List<EmailOutAttachmentModel> getAttachmentsByEmail(int emailID) { List<EmailOutAttachmentModel> list = new ArrayList(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = DBConnection.connectToDB(); String sql = "SELECT * FROM EmailOutAttachment WHERE emailOutID = ?"; ps = conn.prepareStatement(sql); ps.setInt(1, emailID); rs = ps.executeQuery(); while (rs.next()) { EmailOutAttachmentModel item = new EmailOutAttachmentModel(); item.setId(rs.getInt("id")); item.setEmailOutID(rs.getInt("emailOutID")); item.setFileName(rs.getString("fileName")); list.add(item); } } catch (SQLException ex) { ExceptionHandler.Handle(ex); } finally { DbUtils.closeQuietly(conn); DbUtils.closeQuietly(ps); DbUtils.closeQuietly(rs); } return list; }