/** * 使用 ResultSet 中的第 i 行给 Schema 赋值 * * @param: rs ResultSet * @param: i int * @return: boolean */ public boolean setSchema(ResultSet rs, int i) { try { // rs.absolute(i); // 非滚动游标 if (rs.getString("RiskCode") == null) this.riskCode = null; else this.riskCode = rs.getString("RiskCode").trim(); if (rs.getString("RiskVer") == null) this.riskVer = null; else this.riskVer = rs.getString("RiskVer").trim(); if (rs.getString("DutyCode") == null) this.dutyCode = null; else this.dutyCode = rs.getString("DutyCode").trim(); if (rs.getString("ChoFlag") == null) this.choFlag = null; else this.choFlag = rs.getString("ChoFlag").trim(); if (rs.getString("SpecFlag") == null) this.specFlag = null; else this.specFlag = rs.getString("SpecFlag").trim(); } catch (SQLException sqle) { System.out.println( "数据库中的LEPRiskDuty表字段个数和Schema中的字段个数不一致,或者执行db.executeQuery查询时没有使用select * from tables"); // @@错误处理 CError tError = new CError(); tError.moduleName = "LEPRiskDutySchema"; tError.functionName = "setSchema"; tError.errorMessage = sqle.toString(); this.mErrors.addOneError(tError); return false; } return true; }
/** * 使用 ResultSet 中的第 i 行给 Schema 赋值 * * @param: rs ResultSet * @param: i int * @return: boolean */ public boolean setSchema(ResultSet rs, int i) { try { // rs.absolute(i); // 非滚动游标 if (rs.getString("CalCode") == null) this.calCode = null; else this.calCode = rs.getString("CalCode").trim(); if (rs.getString("RiskCode") == null) this.riskCode = null; else this.riskCode = rs.getString("RiskCode").trim(); if (rs.getString("Type") == null) this.type = null; else this.type = rs.getString("Type").trim(); if (rs.getString("CalSQL") == null) this.calSQL = null; else this.calSQL = rs.getString("CalSQL").trim(); if (rs.getString("Remark") == null) this.remark = null; else this.remark = rs.getString("Remark").trim(); } catch (SQLException sqle) { System.out.println( "数据库中的LEPCalMode表字段个数和Schema中的字段个数不一致,或者执行db.executeQuery查询时没有使用select * from tables"); // @@错误处理 CError tError = new CError(); tError.moduleName = "LEPCalModeSchema"; tError.functionName = "setSchema"; tError.errorMessage = sqle.toString(); this.mErrors.addOneError(tError); return false; } return true; }
/** * 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); } }
/** * 使用 ResultSet 中的第 i 行给 Schema 赋值 * * @param: rs ResultSet * @param: i int * @return: boolean */ public boolean setSchema(ResultSet rs, int i) { try { // rs.absolute(i); // 非滚动游标 if (rs.getString("recommend") == null) this.recommend = null; else this.recommend = rs.getString("recommend").trim(); if (rs.getString("recommended") == null) this.recommended = null; else this.recommended = rs.getString("recommended").trim(); this.sendtimes = rs.getInt("sendtimes"); if (rs.getString("success") == null) this.success = null; else this.success = rs.getString("success").trim(); if (rs.getString("registered") == null) this.registered = null; else this.registered = rs.getString("registered").trim(); if (rs.getString("field1") == null) this.field1 = null; else this.field1 = rs.getString("field1").trim(); if (rs.getString("field2") == null) this.field2 = null; else this.field2 = rs.getString("field2").trim(); if (rs.getString("field3") == null) this.field3 = null; else this.field3 = rs.getString("field3").trim(); if (rs.getString("makedate") == null) this.makedate = null; else this.makedate = rs.getString("makedate").trim(); if (rs.getString("maketime") == null) this.maketime = null; else this.maketime = rs.getString("maketime").trim(); if (rs.getString("modifydate") == null) this.modifydate = null; else this.modifydate = rs.getString("modifydate").trim(); if (rs.getString("modifytime") == null) this.modifytime = null; else this.modifytime = rs.getString("modifytime").trim(); } catch (SQLException sqle) { System.out.println( "数据库中的LECRecommendation表字段个数和Schema中的字段个数不一致,或者执行db.executeQuery查询时没有使用select * from tables"); // @@错误处理 CError tError = new CError(); tError.moduleName = "LECRecommendationSchema"; tError.functionName = "setSchema"; tError.errorMessage = sqle.toString(); this.mErrors.addOneError(tError); return false; } return true; }
public static void main(String ag[]) { Connection con; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:A"); Statement st = con.createStatement(); st.executeUpdate( "CREATE TABLE COFEE1" + "(COF_Name VARCHAR(32),SUB_ID INTEGER,PRICE FLOAT,SALES INTEGER,TOTAL INTEGER)"); } catch (ClassNotFoundException e) { System.out.println(e.toString()); } catch (SQLException e) { System.out.println(e.toString()); } }
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // create the workbook, its worksheet, and its title row Workbook workbook = new HSSFWorkbook(); Sheet sheet = workbook.createSheet("User table"); Row row = sheet.createRow(0); row.createCell(0).setCellValue("The User table"); // create the header row row = sheet.createRow(2); row.createCell(0).setCellValue("UserID"); row.createCell(1).setCellValue("LastName"); row.createCell(2).setCellValue("FirstName"); row.createCell(3).setCellValue("Email"); try { // read database rows ConnectionPool pool = ConnectionPool.getInstance(); Connection connection = pool.getConnection(); Statement statement = connection.createStatement(); String query = "SELECT * FROM User ORDER BY UserID"; ResultSet results = statement.executeQuery(query); // create spreadsheet rows int i = 3; while (results.next()) { row = sheet.createRow(i); row.createCell(0).setCellValue(results.getInt("UserID")); row.createCell(1).setCellValue(results.getString("LastName")); row.createCell(2).setCellValue(results.getString("FirstName")); row.createCell(3).setCellValue(results.getString("Email")); i++; } results.close(); statement.close(); connection.close(); } catch (SQLException e) { this.log(e.toString()); } // set response object headers response.setHeader("content-disposition", "attachment; filename=users.xls"); response.setHeader("cache-control", "no-cache"); // get the output stream String encodingString = request.getHeader("accept-encoding"); OutputStream out; if (encodingString != null && encodingString.contains("gzip")) { out = new GZIPOutputStream(response.getOutputStream()); response.setHeader("content-encoding", "gzip"); // System.out.println("User table encoded with gzip"); } else { out = response.getOutputStream(); // System.out.println("User table not encoded with gzip"); } // send the workbook to the browser workbook.write(out); out.close(); }
/** * 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; if (username == null || password == null) { out.println("<h1>Invalid Register Request</h1>"); out.println("<a href=\"register.html\">Register</a>"); return; } Connection con = null; try { Class.forName("com.mysql.jdbc.Driver"); String connectionUrl = "jdbc:mysql://localhost/project3?" + "user=root&password=marouli"; con = DriverManager.getConnection(connectionUrl); if (con != null) { System.out.println("Ola ok me 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(); rs = stmt.executeQuery("SELECT * FROM users WHERE username='******'"); if (rs.next()) { out.println("<h1>Username exists</h1>"); out.println("<a href=\"register.html\">Register</a>"); stmt.close(); rs.close(); con.close(); return; } stmt.close(); rs.close(); stmt = con.createStatement(); if (!stmt.execute("INSERT INTO users VALUES('" + username + "', '" + password + "')")) { out.println("<h1>You are now registered " + username + "</h1>"); out.println("<a href=\"index.jsp\">Login</a>"); int i; for (i = 0; i < listeners.size(); i++) listeners.get(i).UserRegistered(username); } else { out.println("<h1>Could not add your username to the db</h1>"); out.println("<a href=\"register.html\">Register</a>"); } stmt.close(); con.close(); } catch (SQLException e) { throw new ServletException("Servlet Could not display records.", e); } }
private void executeParsedFile(NameListMessage nameListMessage, ArrayList cmds) { BufferedWriter buffer = null; int connectionId = nameListMessage.connectionId; boolean commaSeparated = nameListMessage.commaSeparated; // get java.sql.Connection from connectionId Connection connection = ConnectionService.getConnection(connectionId); if (connection == null) { nameListMessage.errorMessage = kConnectionClosed; } else { try { Thread th = null; buffer = new BufferedWriter(writer); // if reader exists, create reading thread if (reader != null) { final ObjectNameReader objectReader = new ObjectNameReader(nameListMessage.nameList); // This thread will read stream until meets EOL (ie. when // writer is closed) th = new Thread() { public void run() { objectReader.readInputFile(reader); } }; } // end if // if no reader (offline reverse), never start the reading // thread // (because output is written down directly to the disk without // processing in counterpart). if (reader != null) th.start(); // execute each SQL statement // we skip objects that don't need to be reversed Iterator iter = cmds.iterator(); int index = -1; while (iter.hasNext()) { String cmd = (String) iter.next(); index++; // check if this statement id is marked as <to skip> if (nameListMessage.ignoredStatementIds != null && nameListMessage.ignoredStatementIds.contains(new Integer(index))) { if (commaSeparated) { buffer.flush(); buffer.newLine(); } continue; } if (writer != null) { if (cmd != null) { try { execute(connection, cmd, buffer, commaSeparated); } catch (SQLException e) { if (Debug.isDebug()) e.printStackTrace(); nameListMessage.errorMessage = nameListMessage.errorMessage == null ? e.toString() : nameListMessage.errorMessage.concat("\n" + e.toString()); // NOT // LOCALIZABLE, // escape // code } } if (commaSeparated) { buffer.flush(); buffer.newLine(); } } } } /* * catch (SQLException ex) { if (Debug.isDebug()) ex.printStackTrace(); * nameListMessage.errorMessage = nameListMessage.errorMessage == null ? ex.toString() : * nameListMessage.errorMessage.concat("\n" + ex.toString()); //NOT LOCALIZABLE, escape * code } */ catch (IOException ex) { if (Debug.isDebug()) ex.printStackTrace(); nameListMessage.errorMessage = nameListMessage.errorMessage == null ? ex.toString() : nameListMessage.errorMessage.concat( "\n" + ex.toString()); // NOT LOCALIZABLE, escape // code } // close writer: the reading thread terminates. try { // WARNING: we must make a pause after closing the buffer // otherwise // the nameList variable won't be well initialized...strange! // [FG] buffer.close(); Thread.sleep(400); } catch (IOException ex) { } catch (InterruptedException ex) { } } // end if } // end parseSqlFile()
/** * 使用 ResultSet 中的第 i 行给 Schema 赋值 * * @param: rs ResultSet * @param: i int * @return: boolean */ public boolean setSchema(ResultSet rs, int i) { try { // rs.absolute(i); // 非滚动游标 if (rs.getString("tradeNo") == null) this.tradeNo = null; else this.tradeNo = rs.getString("tradeNo").trim(); if (rs.getString("contNo") == null) this.contNo = null; else this.contNo = rs.getString("contNo").trim(); if (rs.getString("UserCode") == null) this.userCode = null; else this.userCode = rs.getString("UserCode").trim(); if (rs.getString("Name") == null) this.name = null; else this.name = rs.getString("Name").trim(); if (rs.getString("Gender") == null) this.gender = null; else this.gender = rs.getString("Gender").trim(); this.birthday = rs.getDate("Birthday"); if (rs.getString("Province") == null) this.province = null; else this.province = rs.getString("Province").trim(); if (rs.getString("City") == null) this.city = null; else this.city = rs.getString("City").trim(); if (rs.getString("Mobile") == null) this.mobile = null; else this.mobile = rs.getString("Mobile").trim(); if (rs.getString("UserIDType") == null) this.userIDType = null; else this.userIDType = rs.getString("UserIDType").trim(); if (rs.getString("UserIDNo") == null) this.userIDNo = null; else this.userIDNo = rs.getString("UserIDNo").trim(); if (rs.getString("riskname") == null) this.riskname = null; else this.riskname = rs.getString("riskname").trim(); if (rs.getString("riskcode") == null) this.riskcode = null; else this.riskcode = rs.getString("riskcode").trim(); if (rs.getString("UserState") == null) this.userState = null; else this.userState = rs.getString("UserState").trim(); if (rs.getString("UserSource") == null) this.userSource = null; else this.userSource = rs.getString("UserSource").trim(); if (rs.getString("field1") == null) this.field1 = null; else this.field1 = rs.getString("field1").trim(); if (rs.getString("field2") == null) this.field2 = null; else this.field2 = rs.getString("field2").trim(); if (rs.getString("field3") == null) this.field3 = null; else this.field3 = rs.getString("field3").trim(); if (rs.getString("field4") == null) this.field4 = null; else this.field4 = rs.getString("field4").trim(); if (rs.getString("field5") == null) this.field5 = null; else this.field5 = rs.getString("field5").trim(); if (rs.getString("field6") == null) this.field6 = null; else this.field6 = rs.getString("field6").trim(); this.searchDate = rs.getDate("searchDate"); if (rs.getString("searchTime") == null) this.searchTime = null; else this.searchTime = rs.getString("searchTime").trim(); this.makedate = rs.getDate("makedate"); if (rs.getString("maketime") == null) this.maketime = null; else this.maketime = rs.getString("maketime").trim(); } catch (SQLException sqle) { System.out.println( "数据库中的SearchRecord表字段个数和Schema中的字段个数不一致,或者执行db.executeQuery查询时没有使用select * from tables"); // @@错误处理 CError tError = new CError(); tError.moduleName = "SearchRecordSchema"; tError.functionName = "setSchema"; tError.errorMessage = sqle.toString(); this.mErrors.addOneError(tError); return false; } return true; }
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))); }