List of usage examples for java.sql SQLException getMessage
public String getMessage()
From source file:com.att.pirates.controller.GlobalDataController.java
public static List<InsightRelease> GetProjectHierarchiesPOJO(String UUID) { // logger.error(msgHeader + "GetProjectHierarchies called"); boolean hasITUProles = isATTEmployeeITUPRoleEmpty(UUID); List<InsightRelease> rc = new ArrayList<InsightRelease>(); List<InsightProject> pc = new ArrayList<InsightProject>(); ResultSet rs = null;/* w w w . j a va 2 s . c o m*/ Connection conn = null; PreparedStatement selectRow = null; try { PropertyReader reader = new PropertyReader(); String forName = reader.readPropertyByName(PiratesConstants.CLASSFORNAME); if (StringUtils.isBlank(forName) || StringUtils.isEmpty(forName)) { throw new Exception("forName is empty from property file"); } String serverName = reader.readPropertyByName(PiratesConstants.SERVERNAME); String databaseName = reader.readPropertyByName(PiratesConstants.DATABASENAME); String user = reader.readPropertyByName(PiratesConstants.USER); String password = reader.readPropertyByName(PiratesConstants.PASSWORD); if (StringUtils.isBlank(serverName) || StringUtils.isEmpty(databaseName) || StringUtils.isBlank(user) || StringUtils.isEmpty(password)) { throw new Exception("connection string is empty from property file"); } String url = serverName + ";" + "databaseName=" + databaseName; Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); conn = DriverManager.getConnection(url, user, password); // SQL query command String SQL = ""; if (!hasITUProles) { logger.error(msgHeader + "hasITUProles is false !"); SQL = " select r.ReleaseName, p.PRISMId, p.ProjectName " + " from Releases r join Projects p " + " on p.ReleaseId = r.ReleaseId " + " join DomainProjects dp on dp.PRISMId = p.PRISMId " + " join ATTEmployeeDomains ad on dp.domainId = ad.domainId " + " join ATTEmployeeProjectStatus eps on p.statusid = eps.statusid " + " where ad.UUID = ? and eps.UUID = ? " + " order by r.ReleaseId asc, p.PRISMId "; } else { logger.error(msgHeader + "hasITUProles is true !"); SQL = " select r.ReleaseName, p.PRISMId, p.ProjectName " + " from Releases r join Projects p " + " on p.ReleaseId = r.ReleaseId " + " join DomainProjects dp on dp.PRISMId = p.PRISMId " + " join ATTEmployeeDomains ad on dp.domainId = ad.domainId " + " join ATTEmployeeProjectStatus eps on p.statusid = eps.statusid " + " join " + " (" + " select distinct ao.PrismId " + " from AppProjectArtifactOwners ao join " + " ATTEmployeeArtifacts aa on aa.UUID=ao.UUID " + " and aa.ArtifactName = ao.ArtifactName " + " where aa.uuid = ? \n " + " ) as tbl on tbl.prismId = dp.PRISMId " + " where ad.UUID = ? and eps.UUID = ? order by r.ReleaseId asc, p.PRISMId "; } selectRow = conn.prepareStatement(SQL); selectRow.setString(1, UUID); selectRow.setString(2, UUID); if (hasITUProles) { selectRow.setString(3, UUID); } rs = selectRow.executeQuery(); while (rs.next()) { String releaseName = rs.getString("ReleaseName"); String prismId = rs.getString("PRISMId"); String projectName = rs.getString("ProjectName"); // create a project and add it to list InsightProject p = new InsightProject(); p.setProjectName(projectName); p.setPrismId(prismId); p.setReleaseName(releaseName); pc.add(p); boolean rlExists = false; // create a release and add it to list if it's not already added if (rc.size() > 0) { for (InsightRelease r : rc) { if (r.getReleaseName().equalsIgnoreCase(releaseName)) { rlExists = true; break; } } } if (!rlExists) { InsightRelease rl = new InsightRelease(); rl.setReleaseName(releaseName); rc.add(rl); } // logger.error(msgHeader + releaseName + "," + prismId + "," + projectName); } if (rc.size() > 0) { for (InsightRelease r : rc) { if (pc.size() > 0) { for (InsightProject p : pc) { if (p.getReleaseName().equalsIgnoreCase(r.getReleaseName())) { r.getProjects().add(p); } } } } } } catch (SQLException e) { logger.error(e.getMessage()); } catch (Exception e) { logger.error(e.getMessage()); } finally { try { if (rs != null) rs.close(); } catch (Exception e) { } ; try { if (selectRow != null) selectRow.close(); } catch (Exception e) { } ; try { if (conn != null) conn.close(); } catch (Exception e) { } ; } return rc; }
From source file:com.wso2telco.core.dbutils.DbUtils.java
/** * Close connection./* ww w .j a v a 2 s .c o m*/ * * @param dbConnection * the db connection */ private static void closeConnection(Connection dbConnection) { try { if (dbConnection != null && dbConnection.getAutoCommit() != true) { log.debug("database connection is active and auto commit is false"); dbConnection.setAutoCommit(true); dbConnection.close(); log.debug("database connection set to close and auto commit set to true"); } else if (dbConnection != null) { log.debug("database connection is active"); dbConnection.close(); log.debug("database connection set to closed"); } } catch (SQLException e) { log.error("database error. Could not close database connection. continuing with others. - " + e.getMessage(), e); } }
From source file:org.apache.drill.test.framework.Utils.java
public static int getNumberOfDrillbits(Connection connection) { String query = "select count(*) from sys.drillbits"; int numberOfDrillbits = 0; try {//ww w . ja v a 2 s. co m ResultSet resultSet = execSQL(query, connection); resultSet.next(); numberOfDrillbits = resultSet.getInt(1); } catch (SQLException e) { LOG.error(e.getMessage()); e.printStackTrace(); } return numberOfDrillbits; }
From source file:org.apache.drill.test.framework.Utils.java
public static ResultSet execSQL(String sql, Connection connection) throws SQLException { try {// ww w . j ava2s . co m Statement statement = connection.createStatement(); return statement.executeQuery(sql); } catch (SQLException e) { LOG.error(e.getMessage()); e.printStackTrace(); try { connection.close(); } catch (SQLException e1) { LOG.error(e.getMessage()); e1.printStackTrace(); } throw e; } }
From source file:com.wso2telco.dbUtil.DataBaseConnectUtils.java
/** * Close ResultSet//from w ww . j a va2 s. co m * * @param resultSet ResultSet */ private static void closeResultSet(ResultSet resultSet) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { log.error("Database error. Could not close ResultSet - " + e.getMessage(), e); } } }
From source file:com.wso2telco.dbutils.DbUtils.java
/** * Close connection./* w ww.j a v a2 s . com*/ * * @param dbConnection * the db connection */ private static void closeConnection(Connection dbConnection) { try { if (dbConnection != null && dbConnection.getAutoCommit() != true) { log.debug("database connection is active and auto commit is false"); dbConnection.setAutoCommit(true); dbConnection.close(); log.debug("database connection set to close and auto commit set to true"); } else if (dbConnection != null) { log.debug("database connection is active"); dbConnection.close(); log.debug("database connection set to closed"); } } catch (SQLException e) { log.error("database error. Could not close database connection. continuing with others. - " + e.getMessage(), e); } /* * if (dbConnection != null) { try { dbConnection.close(); } catch * (SQLException e) { log.warn( * "Database error. Could not close database connection. Continuing with " * + "others. - " + e.getMessage(), e); } } */ }
From source file:controler.DbModules.java
/** * This code is for getting connection to the db * * @return connection string/*from ww w .jav a 2 s .c om*/ */ public static Connection getConnection() { try { Common one = new Common(); DriverManager.registerDriver(new com.mysql.jdbc.Driver());//remember this con = DriverManager.getConnection(one.getDb_url(), one.getUser(), one.getPassword()); System.out.println("connected"); } catch (SQLException sql) { System.out.println(sql.getMessage()); } return con; }
From source file:helpers.database.DBAdminManager.java
public static void addGroupToSystem(AdminBean bean) { final DBContext c = new DBContext(); try {/* w ww. j a v a 2 s . c o m*/ if (bean.getUser() != null && c.init()) { // initialize database final String user = bean.getUser().trim(); // get next group id c.conn.setAutoCommit(false); // we do this in a transaction c.stmt = c.conn.prepareStatement("SELECT MAX(`group`) + 1 AS id FROM groupids"); c.rst = c.stmt.executeQuery(); if (c.rst.next()) { int next_groupid = c.rst.getInt("id"); // check, if user name exists c.stmt = c.conn.prepareStatement("SELECT user_name FROM user WHERE user_name = ?"); c.stmt.setString(1, user); c.rst = c.stmt.executeQuery(); if (c.rst.next()) { // user exists, check, if group exists c.stmt = c.conn.prepareStatement("SELECT group_name FROM groupids WHERE group_name = ?"); c.stmt.setString(1, user); c.rst = c.stmt.executeQuery(); if (c.rst.next()) { // group already exists --> do nothing bean.addError("This group already exists."); } else { // group does not exists --> add it c.stmt = c.conn.prepareStatement( "INSERT INTO groupids (`group_name`, `group`, privlevel) VALUES (?,?,?)"); c.stmt.setString(1, user); c.stmt.setInt(2, next_groupid); c.stmt.setInt(3, bean.getPrivlevel()); c.stmt.executeUpdate(); c.stmt = c.conn.prepareStatement( "INSERT INTO groups (`user_name`, `group`, `defaultgroup`) VALUES (?,?,?)"); c.stmt.setString(1, user); c.stmt.setInt(2, next_groupid); c.stmt.setInt(3, next_groupid); c.stmt.executeUpdate(); bean.addInfo("Added group with id " + next_groupid + " to the system."); } } else { bean.addError("This user does not exist."); } } c.conn.commit(); // commit transaction } } catch (SQLException e) { try { c.conn.rollback(); } catch (SQLException f) { /* * TODO: first attempt to do logging when exceptions are thrown - code "stolen" from Jens' * Database backend classes */ final Log log = LogFactory.getLog(DBAdminManager.class); log.fatal("could not roll transaction back " + e.getMessage()); } bean.addError("Sorry, an error occured: " + e); } finally { c.close(); // close database connection } }
From source file:com.axibase.tsd.driver.jdbc.strategies.Consumer.java
private static void fillErrors(List<ErrorSection> errorSections, StatementContext context) { SQLException sqlException = null; if (errorSections != null) { for (ErrorSection section : errorSections) { sqlException = new SQLException(section.getMessage(), section.getState()); List<StackTraceElement> list = getStackTrace(section); sqlException.setStackTrace(list.toArray(new StackTraceElement[list.size()])); context.addException(sqlException); }//from ww w .ja va 2s. c om } if (sqlException != null) { throw new AtsdRuntimeException(sqlException.getMessage(), sqlException); } }
From source file:com.surveypanel.dao.DBTestCase.java
/** * Executes a SQL script./*w w w . j ava 2 s.co m*/ * * @param con database connection. * @param resource an input stream for the script to execute. * @param autoreplace automatically replace jiveVersion with ofVersion * @throws IOException if an IOException occurs. * @throws SQLException if an SQLException occurs. */ private static void executeSQLScript(Connection con, InputStream resource) throws IOException, SQLException { BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(resource)); boolean done = false; while (!done) { StringBuilder command = new StringBuilder(); while (true) { String line = in.readLine(); if (line == null) { done = true; break; } // Ignore comments and blank lines. if (isSQLCommandPart(line)) { command.append(" ").append(line); } if (line.trim().endsWith(";")) { break; } } // Send command to database. if (!done && !command.toString().equals("")) { try { String cmdString = command.toString(); Statement stmt = con.createStatement(); stmt.execute(cmdString); stmt.close(); } catch (SQLException e) { // Lets show what failed logger.error("SchemaManager: Failed to execute SQL:\n" + command.toString()); throw e; } } } } finally { if (in != null) { try { in.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } }