Nothing to do this afternoon. The main thesis is not much progress, alas! Read the grind to the bitter force ah. Suddenly want to play other, remember last time with JSP and Servlet was a few months ago. So the wayward play a return report.
First of all, I'm the default. You're not a rookie, and using JSPs and Servlets, the basics of Java Web will be used to connect to the database using JDBC. These are all necessary knowledge, otherwise the next content will not be able to explain.
I. Key elements of report generation
1, the Background data extraction
2. Logical operation of data items
3. The front desk uses the table to display
Second, the use of the form of a flowchart to display 3 key elements
1, the background data extraction process
Let's talk about the technology needed for this process.
Get report Parameters: The main is the foreground to the background to pass the parameter, note can not throw parameters.
Connection to database: JDBC Connection database
Synthetic SQL statements: Standard SQL statements, concatenation of strings
Execute SQL statement: How Java operates the database
Get result set: Process the result set, where the container in Java is used to store the data
Returning a result set: sending a parameter to a servlet
2. Logical operation flow of data item
Let's talk about the technology needed for this process.
Get the original dataset for the database: value to the servlet.
The operation transformation of the data: common subtraction and other arithmetic functions.
Reassemble the data: You need to select the appropriate container for the appropriate data format.
Return a logical DataSet: to the foreground page to pass the parameter.
3. The front desk uses the form to display the flow
Let's talk about the technology needed for this process.
Gets the logical dataset: The value of the servlet.
Set the style of the table: Html, Css, JS and other technologies.
Traversing data items: Js, AJax.
Preparation of production report.
First, you need to install the Tomcat server on your own computer. How to install, this everyone own Google tutorial. Then you need to install MyEclipse on your own computer, which is the necessary IDE for Java Web development. Well, then you need a database (either MySQL or Oracle) to provide you with the data you want.
Here's a look at the report I'm going to make:
I'm going to record the real-time voltage data from the substation in the school database (well, this is a project of the school's energy-saving platform.) ), which is displayed in the foreground page.
The data for the database is presented, using the Oracle database.
The first is the database information: contains the database name, SID, and IP address, user name, password, port number
Information for the table that the database is querying: This is the information for the Tb_trans_collection table. I'll show all the records in the Bk,ua,ub,uc fields on the front page.
Specific record values in the table:
Iv. code implementation of my report
First I want to show you the structure of my project's approximate file:
I'm actually using one of the most primitive MVC models. The model layer is represented by the Trans.java in the beans file, and the View view layer uses INDEX.JSP to complete the controller control layer
is a Showreport.java file under the servlet file. The JDBC file is used to connect to the database, and the service file is the logical layer, primarily for business logic processing.
The first step is to complete the code for the model layer:
Package beans;//This is the creation of the data model public class Trans {//board serial number private String bk;//three-phase voltage value private double ua;private double ub;private Double uc;//The value of the three-phase current private double ia;private double ib;private double ic;public String getbk () {return BK;} public void Setbk (String bk) {THIS.BK = BK;} Public double Getua () {return UA;} public void Setua (double ua) {this.ua = UA;} Public double getub () {return UB;} public void Setub (double ub) {this.ub = UB;} Public double Getuc () {return UC;} public void SETUC (double uc) {this.uc = UC;} Public double Getia () {return ia;} public void SetIa (double ia) {This.ia = IA;} Public double Getib () {return IB;} public void Setib (double ib) {This.ib = IB;} Public double getic () {return IC;} public void setic (double ic) {this.ic = IC;}}
The second step is to complete the connection operation of the database and return the database connection in the method.
Package Jdbc;import java.sql.*;p ublic class Jdbcconn {//Set the path to the connection database, including the IP address, port number, sidprivate static String url= for the database Jdbc:oracle:thin:@172.16.254.185:1521:ahuservice ";//the user name and password of the database private static String user=" CS2013 ";p rivate static String password= "m123";//Establish a connection to a database public static Connection conn;//connection to a particular database (session). Executes the SQL statement in the connection context and returns the result. public static PreparedStatement ps;public static ResultSet rs;///represents a data table for a database result set, typically generated by executing a query database statement. public static Statement st;//is used to execute a static SQL statement and return the object to which it produces results. Create a method to connect to the database public static Connection getconnection () {try {class.forname ("oracle.jdbc.driver.OracleDriver");// Load the database driver conn=drivermanager.getconnection (Url,user,password);//Connection database} catch (Exception e) {//TODO auto-generated Catch Blocke.printstacktrace ();} Return conn;}}
The third step is to realize the design of the business logic layer.
Package Service;import Java.sql.connection;import Java.sql.resultset;import java.sql.sqlexception;import Java.sql.statement;import java.util.arraylist;import java.util.list;import beans. Trans;import jdbc. Jdbcconn;public class Services {private Connection dbconnection;//database connection Private Statement st;//Execute SQL statement private ResultSet rs;//the container used to hold the data private String sql;//The statement used to hold the database to be executed the private List list;//list container is used to store the dataset returned from the database private Trans TS ;//define a Model object public List Gettrans () {list=new ArrayList ();//Initialize container//Get connection to Database Dbconnection=jdbcconn.getconnection ();// Synthesize the SQL statement and execute the SQL statement try {st= (Statement) dbconnection.createstatement ();//Generate a <span style= "font-family:arial, Helvetica, Sans-serif; " > The object that executes the static SQL statement and returns the result it produces </span><span style= "White-space:pre" ></span>//the SQL statement that has been determined sql= " Select Tb_trans_collection.bk bk,tb_trans_collection.ua ua, "+" Tb_trans_collection.ub ub,tb_trans_collection.uc UC From "+" tb_trans_collection order by TB_TRANS_COLLECTION.BK "; rs=st.executequery (sql);//Execute SQL statement and save the resultIn the ResultSet object//traversing the elements in RS, it is necessary to convert the elements in the resultset into trans objects. while (Rs.next ()) {ts=new trans ();//Note that each time a trans object must be new//Set the value Ts.setbk (rs.getstring ("BK")); Ts.setua (rs.getdouble ("UA")); Ts.setub (rs.getdouble ("UB")) TS.SETUC (rs.getdouble ("UC")); List.add (ts);//Add element to list container}} catch (SQLException e) {// TODO auto-generated catch Blocke.printstacktrace ();} Return list;//returns the list container}}
The fourth step is to implement the content of our controller layer. Here is the writing of a servlet.
Package Servlet;import Java.io.ioexception;import Java.io.printwriter;import java.util.list;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Service. Services;public class Showreport extends HttpServlet {/** * Constructor of the object. */public Showreport () {super ();} /** * Destruction of the servlet. <br> */public void Destroy () {Super.destroy ();//Just puts "destroy" string in log//Put your code here}/** * the D Oget method of the servlet. <br> * This method was called when a form have its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the Client * @throws servletexception If an error occurred * @throws IOException If an error occurred */public void Doget (Http ServletRequest request, HttpServletResponse response) throws Servletexception, IOException{This.dopost (request, response);} /** * The DoPost method of the servlet. <br> * This method was called when a form have its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the Client * @throws servletexception If an error occurred * @throws IOException If an error occurred */public void DoPost (HTT Pservletrequest request, HttpServletResponse response) throws Servletexception, IOException {
<span style= "White-space:pre" ></span>//add your own code here list list;//define a container to store data from the backend//declare a serviceservices Services=new Services (); List=services.gettrans ()///data container for background access if (list!=null) {//data is passed to the foreground, the session object is used here. We use the session to request.getsession the data (). SetAttribute ("TRANS", list); Response.sendredirect (".. /index.jsp ");//request redirection, equivalent to two requests from the client}}/** * initialization of the servlet. <br> * * @throws servletexception If an error occurs */public void init () throws Servletexception {//Put your code Here}}
The final step is to design the front page. The background data needs to be shown here.
<!--first remember to bring in the required class pack
<%@ page language= "java" import= "java.util.*,beans.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
All my code is over here. Next look at the effect of the execution:Click Generate report, the page switch is as follows:
Here, you can say that a simple report is generated. I didn't use CSS to render the interface, and I felt it was too time-consuming. And that should be what the Web front ends up doing. By the way, there is a doubt that everyone found that my tomcat default port is 8000, the initial default is not 8080? This is because my 8080 port is used by other software, so I can only change the default port to 8000. This does not affect the reports that we generate.
How about, simple. A web version of the report is implemented in a total of five steps. In fact, as long as you understand the JSP and the servlet, I believe you have the spring and struct these frameworks. These frameworks have evolved from Servlets. It is simply to shield the user from some of the underlying code implementation, is the user can not consider the underlying code. Just take it and use it.
Teach you to use Java to make the original ecological report