/** * puts the value for the column aginst the column Name * * @param os * @param rows * @param oldResultSet * @param index * @param modifiedColumns * @param columnName * @throws SQLException * @throws IOException */ public void writeUpdate( Writer os, ResultSet rows, ResultSet oldResultSet, int index, HashMap modifiedColumns, String columnName, ArrayList encodedCols) throws SQLException, IOException { Object newObject = rows.getObject(index); Object oldObject = oldResultSet.getObject(index); if (newObject == null) { write(os, "NULL", encodedCols, columnName); if (oldObject != null) { modifiedColumns.put(columnName, "NULL"); } } else { write(os, newObject, encodedCols, columnName); if (oldObject != null) { if (!(newObject.equals(oldObject))) { modifiedColumns.put(columnName, newObject); } } else { modifiedColumns.put(columnName, newObject); } } }
/** private method which actually will do all of our work for the sample */ private void executeSample() { String query = "select anEmployee from staff2"; try { Statement stmt = _con.createStatement(); ; // Execute the query which will return an Employee object // We will cast this using the Person interface. Note the // Person interface class MUST be in your CLASSPATH. You // Do not need Employee in your CLASSPATH. ResultSet rs = stmt.executeQuery(query); output("***Using interface class\n"); while (rs.next()) { Person aPerson = (Person) rs.getObject(1); displayMethods(aPerson.getClass()); output( "The person is: " + aPerson.toString() + "\nFirst Name= " + aPerson.getFirstName() + "\nLast Name= " + aPerson.getLastName() + "\n"); } // Now execute the same query, but this time we will use // reflection to access the class. Again, only the interface // Person is required in the CLASSPATH rs = stmt.executeQuery(query); output("***Using reflection\n"); Object theObj = null; while (rs.next()) { theObj = rs.getObject(1); output("The person is: " + theObj.toString() + "\n"); Class theClass = theObj.getClass(); displayMethods(theClass); Method m1 = theClass.getMethod("toString", new Class[0]); Method m2 = theClass.getMethod("getFirstName", new Class[0]); Method m3 = theClass.getMethod("getLastName", new Class[0]); output( "The person is: " + (Object) m1.invoke(theObj, new Object[0]) + "\nFirst Name= " + (Object) m2.invoke(theObj, new Object[0]) + "\nLast Name= " + (Object) m3.invoke(theObj, new Object[0]) + "\n"); } rs.close(); stmt.close(); } catch (SQLException sqe) { displaySQLEx(sqe); } catch (Exception e) { error("Unexpected exception : " + e.toString() + "\n"); e.printStackTrace(); } }
// execute and get results private void execute(Connection conn, String text, Writer writer, boolean commaSeparator) throws SQLException { BufferedWriter buffer = new BufferedWriter(writer); Statement stmt = conn.createStatement(); stmt.execute(text); ResultSet rs = stmt.getResultSet(); ResultSetMetaData metadata = rs.getMetaData(); int nbCols = metadata.getColumnCount(); String[] labels = new String[nbCols]; int[] colwidths = new int[nbCols]; int[] colpos = new int[nbCols]; int linewidth = 1; // read each occurrence try { while (rs.next()) { for (int i = 0; i < nbCols; i++) { Object value = rs.getObject(i + 1); if (value != null) { buffer.write(value.toString()); if (commaSeparator) buffer.write(","); } } } buffer.flush(); rs.close(); } catch (IOException ex) { if (Debug.isDebug()) ex.printStackTrace(); // ok, exit from the loop } catch (SQLException ex) { if (Debug.isDebug()) ex.printStackTrace(); } }
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String username = request.getParameter("username"); String password = request.getParameter("password"); Statement stmt; ResultSet rs; Connection con = null; try { Class.forName("com.mysql.jdbc.Driver"); String connectionUrl = "jdbc:mysql://localhost/myflickr?" + "user=root&password=123456"; con = DriverManager.getConnection(connectionUrl); if (con != null) { System.out.println("connected to mysql"); } } catch (SQLException e) { System.out.println("SQL Exception: " + e.toString()); } catch (ClassNotFoundException cE) { System.out.println("Class Not Found Exception: " + cE.toString()); } try { stmt = con.createStatement(); System.out.println("SELECT * FROM flickrusers WHERE name='" + username + "'"); rs = stmt.executeQuery("SELECT * FROM flickrusers WHERE name='" + username + "'"); while (rs.next()) { if (rs.getObject(1).toString().equals(username)) { out.println("<h1>To username pou epileksate uparxei hdh</h1>"); out.println("<a href=\"project3.html\">parakalw dokimaste kapoio allo.</a>"); stmt.close(); rs.close(); return; } } stmt.close(); rs.close(); stmt = con.createStatement(); if (!stmt.execute("INSERT INTO flickrusers VALUES('" + username + "', '" + password + "')")) { out.println("<h1>Your registration is completed " + username + "</h1>"); out.println("<a href=\"index.jsp\">go to the login menu</a>"); registerListener.Register(username); } else { out.println("<h1>To username pou epileksate uparxei hdh</h1>"); out.println("<a href=\"project3.html\">Register</a>"); } } catch (SQLException e) { throw new ServletException("Servlet Could not display records.", e); } }
/** * Processes a particular SQL Array object and interprets its value as a ParamValue object. * * @param sqlArray SQL Array element. * @param paramValue Parameter value object initialized to contain an array of ParamValues. * @return ParamValue object representing the SQL Array. * @throws SQLException Throws an SQL Exception if the result set is not accessible. */ public static ParamValue processSQLArray(Array sqlArray, ParamValue paramValue) throws SQLException { ResultSet rs = sqlArray.getResultSet(); while (rs.next()) { Object arrayEl = rs.getObject(2); if (arrayEl instanceof Struct) { paramValue.getArrayValue().add(new ParamValue((Struct) arrayEl)); } else if (arrayEl instanceof Array) { paramValue .getArrayValue() .add(processSQLArray((Array) arrayEl, new ParamValue(ParamValue.PARAM_VALUE_ARRAY))); } else { paramValue.getArrayValue().add(new ParamValue(String.valueOf(arrayEl))); } } rs.close(); return paramValue; }
/** * This method must be overridden by the subclass to retrieve data and return the valorized value * object. * * @param valueObjectClass value object class * @return a VOResponse object if data loading is successfully completed, or an ErrorResponse * object if an error occours */ public Response loadData(Class valueObjectClass) { Statement stmt = null; try { // since this method could be invoked also when selecting another row on the linked grid, // the pk attribute must be recalculated from the grid... int row = gridFrame.getGrid().getSelectedRow(); if (row != -1) { TestVO gridVO = (TestVO) gridFrame.getGrid().getVOListTableModel().getObjectForRow(row); pk = gridVO.getStringValue(); } stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery( "select DEMO4.TEXT,DEMO4.DECNUM,DEMO4.CURRNUM,DEMO4.THEDATE,DEMO4.COMBO,DEMO4.CHECK_BOX,DEMO4.RADIO,DEMO4.CODE," + "DEMO4_LOOKUP.DESCRCODE,DEMO4.TA,DEMO4.FORMATTED_TEXT,DEMO4.URI,DEMO4.LINK_LABEL,DEMO4.YEAR,DEMO4.FILENAME " + "from DEMO4,DEMO4_LOOKUP where TEXT='" + pk + "' and DEMO4.CODE=DEMO4_LOOKUP.CODE"); if (rset.next()) { DetailTestVO vo = new DetailTestVO(); vo.setCheckValue( rset.getObject(6) == null || !rset.getObject(6).equals("Y") ? Boolean.FALSE : Boolean.TRUE); vo.setCombo(new ComboVO()); vo.getCombo().setCode(rset.getString(5)); // this is a simplification: in a real situation combo v.o. will be retrieved from the // database... Domain d = ClientSettings.getInstance().getDomain("ORDERSTATE"); if (vo.getCombo().getCode().equals("O")) vo.getCombo().setDescription("opened"); else if (vo.getCombo().getCode().equals("S")) vo.getCombo().setDescription("sospended"); else if (vo.getCombo().getCode().equals("D")) vo.getCombo().setDescription("delivered"); else if (vo.getCombo().getCode().equals("C")) vo.getCombo().setDescription("closed"); vo.setCurrencyValue(rset.getBigDecimal(3)); vo.setDateValue(rset.getDate(4)); vo.setNumericValue(rset.getBigDecimal(2)); vo.setRadioButtonValue( rset.getObject(7) == null || !rset.getObject(7).equals("Y") ? Boolean.FALSE : Boolean.TRUE); vo.setStringValue(rset.getString(1)); vo.setLookupValue(rset.getString(8)); vo.setDescrLookupValue(rset.getString(9)); vo.setTaValue(rset.getString(10)); vo.setFormattedTextValue(rset.getString(11)); vo.setUri(rset.getString(12)); vo.setLinkLabel(rset.getString(13)); vo.setYear(rset.getBigDecimal(14)); vo.setFilename(rset.getString(15)); vo.setTooltipURI(vo.getUri()); try { if (vo.getFilename() != null) { File f = new File(vo.getFilename()); BufferedInputStream in = new BufferedInputStream(new FileInputStream(f)); byte[] bytes = new byte[(int) f.length()]; in.read(bytes); in.close(); vo.setFile(bytes); } } catch (Exception ex) { ex.printStackTrace(); } stmt.close(); stmt = conn.createStatement(); rset = stmt.executeQuery( "select DEMO4_LIST_VALUES.CODE from DEMO4_LIST_VALUES where TEXT='" + pk + "'"); ArrayList codes = new ArrayList(); while (rset.next()) { codes.add(rset.getString(1)); } vo.setListValues(codes); return new VOResponse(vo); } else return new ErrorResponse("No data found."); } catch (SQLException ex) { ex.printStackTrace(); return new ErrorResponse(ex.getMessage()); } finally { try { stmt.close(); } catch (Exception ex1) { } } }
public void build() { rt = null; root = new XSAMSData(); procs = new ProcessesType(); rad = new Radiative(); atoms = new Atoms(); sources = new Sources(); procs.setRadiative(rad); states = new StatesType(); states.setAtoms(atoms); root.setStates(states); root.setProcesses(procs); try { int nr = 0; while (rs.next()) { rt = new RadiativeTransitionType(); rtp = new RadiativeTransitionProbabilityType(); rt.getProbabilities().add(rtp); atom = new AtomType(); ist = new IsotopeType(); atom.getIsotopes().add(ist); is = new IonStateType(); ist.getIonStates().add(is); aslow = new AtomicStateType(); asup = new AtomicStateType(); aslow.setStateID("Initial_" + nr); asup.setStateID("Final_" + nr); andtlow = new AtomicNumericalDataType(); andtup = new AtomicNumericalDataType(); aslow.setAtomicNumericalData(andtlow); asup.setAtomicNumericalData(andtup); is.getAtomicStates().add(aslow); is.getAtomicStates().add(asup); aqnlow = new AtomicQuantumNumbersType(); aqnup = new AtomicQuantumNumbersType(); aslow.setAtomicQuantumNumbers(aqnlow); asup.setAtomicQuantumNumbers(aqnup); aclow = new AtomicCompositionType(); acup = new AtomicCompositionType(); aslow.setAtomicComposition(aclow); asup.setAtomicComposition(acup); acmlow = new AtomicComponentType(); acmup = new AtomicComponentType(); conflow = new ConfigurationType(); acmlow.setConfiguration(conflow); confup = new ConfigurationType(); acmup.setConfiguration(confup); aclow.getComponents().add(acmlow); acup.getComponents().add(acmup); termlow = new TermType(); termup = new TermType(); acmlow.setTerm(termlow); acmup.setTerm(termup); wavelength = ((Double) (rs.getObject("wavelength"))).doubleValue(); code = rs.getObject("code").toString(); loggf = ((Double) (rs.getObject("loggf"))).doubleValue(); E_low = ((Double) (rs.getObject("E_low"))).doubleValue(); E_up = ((Double) (rs.getObject("E_up"))).doubleValue(); J_low = ((Double) (rs.getObject("J_low"))).doubleValue(); J_up = ((Double) (rs.getObject("J_up"))).doubleValue(); g_low = ((Double) (rs.getObject("g_low"))).doubleValue(); g_up = ((Double) (rs.getObject("g_up"))).doubleValue(); Connection_low = rs.getObject("Connection_low").toString(); Connection_up = rs.getObject("Connection_up").toString(); Conf_low = rs.getObject("Conf_low").toString(); Conf_up = rs.getObject("Conf_up").toString(); Term_low = rs.getObject("Term_low").toString(); Term_up = rs.getObject("Term_up").toString(); Ref = rs.getObject("Ref").toString(); Comments = rs.getObject("Ref").toString(); setWavelength(wavelength); setElement(code); setProbability(loggf); setElow(E_low); setEup(E_up); setglow(g_low); setgup(g_up); setQNlow(J_low); setQNup(J_up); // setConnlow(Connection_low); // setConnup(Connection_up); setConflow(Conf_low); setConfup(Conf_up); setComments(Comments); atoms.getAtoms().add(atom); rad.getRadiativeTransitions().add(rt); nr++; } data2xml(); con.close(); rs.close(); } catch (Exception e) { System.out.println(e.toString()); } }
private String[] getNextAlert(String[] previousKeys) { String[] keysForNextAlert = new String[2]; keysForNextAlert[0] = previousKeys[0]; keysForNextAlert[1] = previousKeys[1]; ResultSet rs = null; PreparedStatementWrapper pstat = null; PreparedStatement pstatement = null; try { // Next ENTITY try { // SELECT MIN(ENTITY) WHERE OWNERNAME="" AND SOURCE="" AND ENTITY>""//No I18N pstat = agentName.rlAPI.fetchPreparedStatement(psForSelectNEXTEntity); pstatement = pstat.getPreparedStatement(); pstatement.setString(1, previousKeys[0]); pstatement.setString(2, previousKeys[1]); // pstatement.setString(3, previousKeys[2]); rs = agentName.rlAPI.executeQuery(pstatement); rs.next(); keysForNextAlert[1] = (String) rs.getObject(1); } finally { try { rs.close(); } catch (Exception e) { } agentName.rlAPI.returnPreparedStatement(pstat); } if (keysForNextAlert[1] == null) { // Next SOURCE try { // SELECT MIN(SOURCE) WHERE OWNERNAME="" AND SOURCE>"";//No I18N pstat = agentName.rlAPI.fetchPreparedStatement(psForSelectNEXTSource); pstatement = pstat.getPreparedStatement(); pstatement.setString(1, previousKeys[0]); // pstatement.setString(2, previousKeys[0]); rs = agentName.rlAPI.executeQuery(pstatement); rs.next(); keysForNextAlert[0] = (String) rs.getObject(1); } finally { try { rs.close(); } catch (Exception e) { } agentName.rlAPI.returnPreparedStatement(pstat); } if (keysForNextAlert[0] == null) { // Next OWNERNAME /* try { //SELECT MIN(OWNERNAME) WHERE OWNERNAME>"";//No I18N pstat = agentName.rlAPI.fetchPreparedStatement(psForSelectNEXTOwnerName); pstatement = pstat.getPreparedStatement(); pstatement.setString(1, previousKeys[1]); rs = agentName.rlAPI.executeQuery(pstatement); rs.next(); keysForNextAlert[1] = (String)rs.getObject(1); if(keysForNextAlert[1] == null) return null; } finally { try{ rs.close(); }catch(Exception e){} agentName.rlAPI.returnPreparedStatement(pstat); }*/ try { // SELECT MIN(SOURCE) WHERE OWNERNAME="";//No I18N pstat = agentName.rlAPI.fetchPreparedStatement(psForSelectMINSource); pstatement = pstat.getPreparedStatement(); pstatement.setString(1, keysForNextAlert[1]); rs = agentName.rlAPI.executeQuery(pstatement); rs.next(); keysForNextAlert[0] = (String) rs.getObject(1); } finally { try { rs.close(); } catch (Exception e) { } agentName.rlAPI.returnPreparedStatement(pstat); } } try { // SELECT MIN(ENETITY) WHERE OWNERNAME="" AND SOURCE="";//No I18N pstat = agentName.rlAPI.fetchPreparedStatement(psForSelectMINEntity); pstatement = pstat.getPreparedStatement(); pstatement.setString(1, keysForNextAlert[0]); rs = agentName.rlAPI.executeQuery(pstatement); rs.next(); keysForNextAlert[1] = (String) rs.getObject(1); } finally { try { rs.close(); } catch (Exception e) { } agentName.rlAPI.returnPreparedStatement(pstat); } } } catch (Exception e) { return null; } return keysForNextAlert; }
private String[] getFirstAlert() { String[] keysForFirstAlert = new String[2]; ResultSet rs = null; PreparedStatementWrapper pstat = null; PreparedStatement pstatement = null; try { try { /*try { //SELECT MIN(ONWERNAME) pstat = agentName.rlAPI.fetchPreparedStatement(psForSelectMINOwnerName); pstatement = pstat.getPreparedStatement(); rs = agentName.rlAPI.executeQuery(pstatement); rs.next(); keysForFirstAlert[1] = (String)rs.getObject(1); } finally { try{ rs.close(); }catch(Exception e){} http://ismp-build/php/download/1466f836f17657/AlarmTable.java agentName.rlAPI.returnPreparedStatement(pstat); }*/ // SELECT MIN(SOURCE) WHERE OWNERNAME=""//No I18N pstat = agentName.rlAPI.fetchPreparedStatement(psForSelectMINSource); pstatement = pstat.getPreparedStatement(); // pstatement.setString(1, keysForFirstAlert[1]); rs = agentName.rlAPI.executeQuery(pstatement); rs.next(); keysForFirstAlert[0] = (String) rs.getObject(1); } finally { try { rs.close(); } catch (Exception e) { } agentName.rlAPI.returnPreparedStatement(pstat); } // SELECT MIN(ENTITY) WHERE OWNERNAME="" AND SOURCE=""//No I18N pstat = agentName.rlAPI.fetchPreparedStatement(psForSelectMINEntity); pstatement = pstat.getPreparedStatement(); pstatement.setString(1, keysForFirstAlert[0]); // pstatement.setString(2, keysForFirstAlert[0]); rs = agentName.rlAPI.executeQuery(pstatement); rs.next(); keysForFirstAlert[1] = (String) rs.getObject(1); } catch (Exception e) { return null; } finally { try { rs.close(); } catch (Exception e) { } agentName.rlAPI.returnPreparedStatement(pstat); } return keysForFirstAlert; }
/** * returns the Object corresponding to the index passed from the resultSet * * @param row * @param index * @return * @throws SQLException */ private Object getObject(ResultSet row, int index) throws SQLException { return row.getObject(index); }
public void go(SystemEnvironment sysEnv) throws SDMSException { Long sgId = null; Long ZERO = new Long(0); if (!sysEnv.cEnv.gid().contains(SDMSObject.adminGId)) { SDMSPrivilege p = new SDMSPrivilege(); Vector v = SDMSMemberTable.idx_uId.getVector(sysEnv, sysEnv.cEnv.uid()); for (int i = 0; i < v.size(); i++) { SDMSMember m = (SDMSMember) v.get(i); try { SDMSGrant gr = SDMSGrantTable.idx_objectId_gId_getUnique( sysEnv, new SDMSKey(ZERO, m.getGId(sysEnv))); p.addPriv(sysEnv, gr.getPrivs(sysEnv).longValue()); } catch (NotFoundException nfe) { } } try { if (sysEnv.selectGroup != null) { SDMSGroup sg = SDMSGroupTable.idx_name_getUnique(sysEnv, sysEnv.selectGroup); sgId = sg.getId(sysEnv); } } catch (NotFoundException nfe) { } if (!(p.can(SDMSPrivilege.MANAGE_SEL) || (sgId != null && sysEnv.cEnv.gid().contains(sgId)))) throw new AccessViolationException( new SDMSMessage(sysEnv, "03003081235", "Insufficient Privileges")); } int read = 0; SDMSOutputContainer d_container = null; if (cl_size > 0) { clist = new int[cl_size]; ctype = new int[cl_size]; } try { Statement stmt = sysEnv.dbConnection.createStatement(); ResultSet rset = stmt.executeQuery(selectCmd); ResultSetMetaData mdset = rset.getMetaData(); Vector desc = collist(mdset); d_container = new SDMSOutputContainer(sysEnv, "Selected Values", desc); while (rset.next()) { Vector data = new Vector(); int j = 0; for (int i = 1; i <= desc.size(); i++) { Object o = rset.getObject(i); if (cl_size > 0 && j < cl_size && i == clist[j]) { o = convert(sysEnv, o, j); j++; } data.addElement((rset.wasNull() ? null : o)); } d_container.addData(sysEnv, data); read++; } stmt.close(); sysEnv.dbConnection.commit(); } catch (SQLException sqle) { try { sysEnv.dbConnection.rollback(); } catch (SQLException sqle2) { throw new RecoverableException(new SDMSMessage(sysEnv, "03310281524", "Connection lost")); } throw new CommonErrorException( new SDMSMessage(sysEnv, "03204170024", "SQL Error : $1", sqle.toString())); } if (sv != null && sv.size() > 0) { int sca[] = new int[sv.size()]; for (int i = 0; i < sv.size(); i++) { sca[i] = ((Integer) sv.get(i)).intValue(); if (sca[i] >= d_container.columns) throw new CommonErrorException( new SDMSMessage( sysEnv, "03003081227", "The sort column specified ($1) exceeds the number of columns in the output", new Integer(sca[i]))); } Collections.sort(d_container.dataset, d_container.getComparator(sysEnv, sca)); } result.setOutputContainer(d_container); result.setFeedback( new SDMSMessage(sysEnv, "03204112153", "$1 Row(s) selected", new Integer(read))); }