@WdprMethodLog private static void loadCustomerDetails(Customer customer) throws ClassNotFoundException, IOException, SQLException, Exception { Connection conn = null; PreparedStatement statement = null; ResultSet rs = null; try { logging.debug(" Start loadCustomer"); DBConnectionFactory dbFactory = DBConnectionFactory.getInstance(); conn = dbFactory.getConnection(); long queryStartTime = System.nanoTime(); queryStartTime = System.nanoTime(); String sql = buildInsertQuery(customer); statement = conn.prepareStatement(sql); statement = populateValues(customer, statement); rs = statement.executeQuery(); long queryEndTime = System.nanoTime(); logging.info( "Total_time_PS_loadCustomer : taken to execute loadCustomer query:=" + TimeUnit.MILLISECONDS.convert( (queryEndTime - queryStartTime), TimeUnit.NANOSECONDS)); logging.debug(" End time of loadCustomer Query : "); } catch (Exception ex) { logging.error(ex.getLocalizedMessage()); throw ex; } finally { if (rs != null) rs.close(); if (statement != null) statement.close(); if (conn != null) conn.close(); } }
@Override public User getUserDetails(String uname) { Connection con = null; try { con = DBConnectionFactory.getConnection(); Statement st = con.createStatement(); String sql = "select fname, lname, email, mobile, usertype " + "from user_details " + "where uname='" + uname + "'"; ResultSet rs = st.executeQuery(sql); if (rs.next()) { User u = new User(); u.setUname(uname); u.setFname(rs.getString(1)); u.setLname(rs.getString(2)); u.setEmail(rs.getString(3)); u.setMobile(rs.getString(4)); u.setUsertype(rs.getString(5)); return u; } } catch (SQLException e) { e.printStackTrace(); } finally { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } return null; }
public boolean findUser(String un, String pwd) { Connection con = null; try { con = DBConnectionFactory.getConnection(); String sql = "select count(uname) from user_details" + " where uname='" + un + "' and pass='******'"; PreparedStatement st = con.prepareStatement(sql); ResultSet rs = st.executeQuery(); rs.next(); int count = rs.getInt(1); return (count == 1); } catch (SQLException e) { e.printStackTrace(); } finally { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } // finally return false; }
private Set<Integer> loadScheduleIds() { Connection connection = null; Statement statement = null; java.sql.ResultSet resultSet = null; try { connection = dbConnectionFactory.newConnection(); statement = connection.createStatement(); resultSet = statement.executeQuery( "SELECT s.id FROM rhq_measurement_sched s INNER JOIN rhq_measurement_def d on s.definition = d.id " + "WHERE d.data_type = 0"); Set<Integer> scheduleIds = new HashSet<Integer>(); while (resultSet.next()) { scheduleIds.add(resultSet.getInt(1)); } return scheduleIds; } catch (SQLException e) { throw new RuntimeException( "Cannot migrate aggregate metrics. There was an error loading schedule ids", e); } finally { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { log.info("There was an error closing the SQL result set", e); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { log.info("There was an error closing the SQL statement", e); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { log.info("There was an error closing the SQL connection", e); } } } }
public DerbyDAOImpl() throws ServiceLocatorException, SQLException { connection = DBConnectionFactory.getConnection(); logger.info("Got connection"); }
public static void main(String[] args) { AssociationDataDumpDao dao = new AssociationDataDumpDao(); try { System.out.println("Enter offset max-record db-password"); if (args.length < 2) { System.out.println("Not entered all parameters...."); return; } dao.offset = Integer.parseInt(args[0]); dao.MAX_RECORD = Integer.parseInt(args[1]); dao.LOG_FILE = dao.LOG_FILE + dao.offset + ".log"; dao.errorWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dao.LOG_FILE), "UTF8")); if (args.length > 2) { dao.password = args[2]; } Connection conn = DBConnectionFactory.getInstance(dao.password).getConnection(); dao.pstmt = conn.prepareStatement("select pageId from Page LIMIT " + dao.LIMIT + " offset ?"); PreparedStatement linkOverlapStmt = conn.prepareStatement(dao.SELECT_ASS_OVERLAP_QUERY); PreparedStatement linkCatOverlapStmt = conn.prepareStatement(dao.ASS_PARENT_OVERLAP_QUERY); PreparedStatement selectLinkCatOverlapStmt = conn.prepareStatement(dao.SELECT_ASS_PARENT_OVERLAP); PreparedStatement selectOutlinksStmt = conn.prepareStatement(dao.SELECT_OUTLINKS); PreparedStatement selectOutlinkOverlapStmt = conn.prepareStatement(dao.SELECT_IS_EXISTS_PAGE_OVERLAP); int pageId = 0; int outLinkId = 0; while (dao.offset + dao.LIMIT <= dao.MAX_RECORD) { final List<Integer> pageIdList = dao.getPageIdList(dao.offset); for (int i = 0, size = pageIdList.size(); i < size; i++) { pageId = pageIdList.get(i); selectOutlinksStmt.setInt(1, pageId); ResultSet result = selectOutlinksStmt.executeQuery(); List<Integer> outLinks = new ArrayList<Integer>(); while (result.next()) { outLinks.add(result.getInt("outLinks")); } result.close(); for (int j = 0, outSize = outLinks.size(); j < outSize; j++) { outLinkId = outLinks.get(j); selectOutlinkOverlapStmt.setInt(1, pageId); selectOutlinkOverlapStmt.setInt(2, outLinkId); selectOutlinkOverlapStmt.setInt(3, outLinkId); selectOutlinkOverlapStmt.setInt(4, pageId); result = selectOutlinkOverlapStmt.executeQuery(); if (!(result != null && result.next())) { linkOverlapStmt.setInt(1, pageId); linkOverlapStmt.setInt(2, outLinkId); linkOverlapStmt.addBatch(); try { String[] catOverlap = dao.getParentOverlap( selectLinkCatOverlapStmt, dao.errorWriter, pageId, outLinkId); if (catOverlap != null) { linkCatOverlapStmt.setInt(1, pageId); linkCatOverlapStmt.setInt(2, outLinkId); linkCatOverlapStmt.setString(3, catOverlap[0]); linkCatOverlapStmt.setString(4, catOverlap[1]); linkCatOverlapStmt.addBatch(); } } catch (Exception e) { try { dao.errorWriter.write( "getting overlap falied" + pageId + " " + outLinkId + e.getMessage() + " " + e.getCause() + "\n"); } catch (Exception exp) { exp.printStackTrace(); } } } if (result != null) result.close(); } System.out.println("done .." + (i + dao.offset)); } try { linkCatOverlapStmt.executeBatch(); } catch (Exception e) { try { dao.errorWriter.write( "linkCatOverlapStmt falied" + pageId + " " + outLinkId + e.getMessage() + " " + e.getCause() + "\n"); } catch (Exception exp) { exp.printStackTrace(); } } try { linkOverlapStmt.executeBatch(); } catch (Exception e) { try { dao.errorWriter.write( "linkOverlapStmt falied" + pageId + " " + outLinkId + e.getMessage() + " " + e.getCause() + "\n"); } catch (Exception exp) { exp.printStackTrace(); } } dao.offset = dao.offset + dao.LIMIT; try { linkOverlapStmt.close(); linkCatOverlapStmt.close(); selectLinkCatOverlapStmt.close(); selectOutlinksStmt.close(); selectOutlinkOverlapStmt.close(); conn.close(); conn = DBConnectionFactory.getInstance(dao.password).getConnection(); dao.pstmt = conn.prepareStatement("select pageId from Page LIMIT " + dao.LIMIT + " offset ?"); linkOverlapStmt = conn.prepareStatement(dao.SELECT_ASS_OVERLAP_QUERY); linkCatOverlapStmt = conn.prepareStatement(dao.ASS_PARENT_OVERLAP_QUERY); selectLinkCatOverlapStmt = conn.prepareStatement(dao.SELECT_ASS_PARENT_OVERLAP); selectOutlinksStmt = conn.prepareStatement(dao.SELECT_OUTLINKS); selectOutlinkOverlapStmt = conn.prepareStatement(dao.SELECT_IS_EXISTS_PAGE_OVERLAP); } catch (Exception e) { try { dao.errorWriter.write( "connection closed falied after" + dao.offset + e.getMessage() + " " + e.getCause() + "\n"); } catch (Exception exp) { exp.printStackTrace(); } } } } catch (Exception e) { e.printStackTrace(); try { dao.errorWriter.write("initalization falied" + e.getMessage() + " " + e.getCause() + "\n"); } catch (Exception exp) { exp.printStackTrace(); } } try { dao.errorWriter.flush(); dao.errorWriter.close(); } catch (Exception e) { e.printStackTrace(); } }