List of usage examples for java.sql SQLException getMessage
public String getMessage()
From source file:jfutbol.com.jfutbol.GcmSender.java
public static void updateNotificationAsSent(int userId) { Connection conn = null;// w w w. j a va 2 s. c o m Statement stmt = null; try { // STEP 2: Register JDBC driver Class.forName(JDBC_DRIVER); // STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); // STEP 4: Execute a query System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql; sql = "UPDATE notifications SET sentByGCM=1 WHERE userId=" + userId; int rs = stmt.executeUpdate(sql); // STEP 5: Extract data from result set if (rs > 0) { // System.out.print("Notifications sent to userId: "+userId); } // STEP 6: Clean-up environment stmt.close(); conn.close(); } catch (SQLException se) { // Handle errors for JDBC log.error(se.getMessage()); se.printStackTrace(); } catch (Exception e) { // Handle errors for Class.forName log.error(e.getMessage()); e.printStackTrace(); } finally { // finally block used to close resources try { if (stmt != null) stmt.close(); } catch (SQLException se2) { log.error(se2.getMessage()); } // nothing we can do try { if (conn != null) conn.close(); } catch (SQLException se) { log.error(se.getMessage()); se.printStackTrace(); } // end finally try } // end try }
From source file:edu.mayo.cts2.uriresolver.dao.DAOUtiltities.java
public static boolean tableExists(DatabaseMetaData metaData, String tablename) { //ResultSet tables = metaData.getTables(null, "public", "%" ,new String[] {"TABLE"} ); ResultSet tables = null;//from w w w . j a va 2 s. co m try { tables = metaData.getTables(null, null, tablename, null); if (tables.next()) { tables.close(); return true; } } catch (SQLException e) { return false; } finally { if (tables != null) { try { tables.close(); } catch (SQLException e1) { logger.error("Error while colsing result set: " + e1.getMessage()); } } } return false; }
From source file:com.cws.esolutions.security.listeners.SecurityServiceInitializer.java
/** * Shuts down the running security service process. *//*from w ww. j av a 2 s . com*/ public static void shutdown() { final String methodName = SecurityServiceInitializer.CNAME + "#shutdown()"; if (DEBUG) { DEBUGGER.debug(methodName); } final SecurityConfigurationData config = SecurityServiceInitializer.svcBean.getConfigData(); Map<String, DataSource> dsMap = SecurityServiceInitializer.svcBean.getDataSources(); if (DEBUG) { DEBUGGER.debug("SecurityConfigurationData: {}", config); } try { DAOInitializer.closeAuthConnection( new FileInputStream(FileUtils.getFile(config.getSecurityConfig().getAuthConfig())), false, svcBean); if (dsMap != null) { for (String key : dsMap.keySet()) { if (DEBUG) { DEBUGGER.debug("Key: {}", key); } BasicDataSource dataSource = (BasicDataSource) dsMap.get(key); if (DEBUG) { DEBUGGER.debug("BasicDataSource: {}", dataSource); } if ((dataSource != null) && (!(dataSource.isClosed()))) { dataSource.close(); } } } } catch (SQLException sqx) { ERROR_RECORDER.error(sqx.getMessage(), sqx); } catch (FileNotFoundException fnfx) { ERROR_RECORDER.error(fnfx.getMessage(), fnfx); } }
From source file:de.griffel.confluence.plugins.plantuml.DatasourceHelper.java
/** * Returns Database meta data.//w ww. ja v a2 s. c om * * @param datasource data source * @param attributes attributes to get * @return Map with requested attributes */ public static Map<String, String> getDatabaseMetadata(DataSource datasource, List<String> attributes) { final Map<String, String> databaseMetadata = new HashMap<String, String>(); Connection con = null; try { con = datasource.getConnection(); final DatabaseMetaData dbmd = con.getMetaData(); for (String attribute : attributes) { if (CATALOGS.equals(attribute)) { databaseMetadata.put(CATALOGS, StringUtils.join(getCatalogs(dbmd), ",")); } else if (TABLE_TYPES.equals(attribute)) { databaseMetadata.put(TABLE_TYPES, StringUtils.join(getTableTypes(dbmd), ",")); } else { fillValue(dbmd, attribute, databaseMetadata); } } } catch (SQLException e) { log.debug("SQLException accessing metadata", e); databaseMetadata.put(ERROR, e.getMessage()); } finally { try { if (con != null) { con.close(); } } catch (SQLException ex) { log.debug("SQLException closing connection", ex); } } return databaseMetadata; }
From source file:bizlogic.Sensors.java
public static void list(Connection DBcon) throws IOException, ParseException, SQLException { Statement st = null;/* ww w. ja va 2s .com*/ ResultSet rs = null; try { st = DBcon.createStatement(); rs = st.executeQuery("SELECT * FROM USERCONF.SENSORLIST"); } catch (SQLException ex) { Logger lgr = Logger.getLogger(Sensors.class.getName()); lgr.log(Level.SEVERE, ex.getMessage(), ex); } try { FileWriter sensorsFile = new FileWriter("/var/lib/tomcat8/webapps/ROOT/Records/sensors.json"); sensorsFile.write(""); sensorsFile.flush(); JSONParser parser = new JSONParser(); JSONObject Records = new JSONObject(); JSONObject operation_Obj = new JSONObject(); JSONObject operand_Obj = new JSONObject(); JSONObject unit_Obj = new JSONObject(); JSONObject name_Obj = new JSONObject(); JSONObject ip_Obj = new JSONObject(); JSONObject port_Obj = new JSONObject(); int _total = 0; JSONArray sensorList = new JSONArray(); while (rs.next()) { JSONObject sensor_Obj = new JSONObject(); int id = rs.getInt("sensor_id"); String operation = rs.getString("operation"); int operand = rs.getInt("operand"); String unit = rs.getString("unit"); String name = rs.getString("name"); String ip = rs.getString("IP"); int port = rs.getInt("port"); sensor_Obj.put("recid", id); sensor_Obj.put("operation", operation); sensor_Obj.put("operand", operand); sensor_Obj.put("unit", unit); sensor_Obj.put("name", name); sensor_Obj.put("IP", ip); sensor_Obj.put("port", port); sensorList.add(sensor_Obj); _total++; } rs.close(); Records.put("total", _total); Records.put("records", sensorList); sensorsFile.write(Records.toJSONString()); sensorsFile.flush(); sensorsFile.close(); } catch (IOException ex) { Logger.getLogger(Sensors.class.getName()).log(Level.WARNING, null, ex); } }
From source file:helpers.database.DBPrivnoteManager.java
/** * Sets the private note of a users bibtex entry. * //from ww w. j a va 2 s . c om * @param bean used to get the note, the hash and the user name * @return <code>true</code> if exactly one database row got updated */ public static boolean setPrivnoteForUser(final String privnote, final String username, final String hash) { DBContext c = new DBContext(); try { if (c.init()) { // initialize database // prepare Statement c.stmt = c.conn.prepareStatement("UPDATE bibtex SET privnote = ? WHERE user_name = ? AND simhash" + constants.INTRA_HASH + " = ?"); c.stmt.setString(1, privnote); c.stmt.setString(2, username); c.stmt.setString(3, hash); return c.stmt.executeUpdate() == 1; // return true, if exactly one row got updated } } catch (SQLException e) { /* * TODO: first attempt to do logging when exceptions are thrown - code "stolen" from Jens' * Database backend classes */ log.fatal("could not set the private not for the user " + e.getMessage()); } finally { c.close(); // close database connection } return false; }
From source file:com.wso2telco.dep.subscriptionvalidator.util.ValidatorDBUtils.java
public static void closeStatement(PreparedStatement preparedStatement) { if (preparedStatement != null) { try {/* www. j ava 2 s . c om*/ preparedStatement.close(); } catch (SQLException var2) { log.warn("Database error. Could not close PreparedStatement. Continuing with others. - " + var2.getMessage(), var2); } } }
From source file:com.netradius.hibernate.support.HibernateUtil.java
private static List<String[]> getTables() { final List<String[]> rows = new ArrayList<String[]>(); rows.add(new String[] { "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "TABLE_TYPE", "REMARKS", "TYPE_CAT", "TYPE_SCHEM", "TYPE_NAME", "SELF_REFERENCING_COL_NAME", "REF_GENERATION " }); Connection con = null;/*from ww w. j a va 2 s.com*/ ResultSet rs = null; try { con = getConnection(); final DatabaseMetaData md = con.getMetaData(); rs = md.getTables(null, null, "%", null); while (rs.next()) { List<String> s = new ArrayList<String>(10); for (int i = 10; i > 0; i--) s.add(rs.getString(i)); rows.add(s.toArray(new String[10])); } } catch (SQLException x) { log.error("Error listing tables: " + x.getMessage(), x); } finally { close(con, null, rs); } return rows; }
From source file:com.wso2telco.dep.subscriptionvalidator.util.ValidatorDBUtils.java
private static void closeConnection(Connection dbConnection) { if (dbConnection != null) { try {/*w w w. java 2 s .co m*/ dbConnection.close(); } catch (SQLException var2) { log.warn("Database error. Could not close database connection. Continuing with others. - " + var2.getMessage(), var2); } } }
From source file:cit360.sandbox.BackEndMenu.java
public final static void connect() { Connection conn = null;//from www. j a v a 2s.c o m try { conn = DriverManager.getConnection("jdbc:mysql://localhost/cit361-sandbox?" + "user=root&password="); // Do something with the Connection } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } if (null != conn) { System.out.println("Connected to database!"); } else { System.out.println("Failed to make connection!"); } try { Statement stmt = conn.createStatement(); String query = "select * from movies ;"; //movies is the table name ResultSet rs = stmt.executeQuery(query); while (rs.next()) { String name = rs.getObject(2).toString(); String Start_Time = rs.getObject(3).toString(); System.out.println(name + ": " + Start_Time); //movies table has name and price columns } } catch (SQLException e) { for (Throwable ex : e) { System.err.println("Error occurred " + ex); } System.out.println("Error in fetching data"); } }