Write a universal interface to PHP with Java

Source: Internet
Author: User
Tags stmt

 PackageHelloWorld;Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.json.JSONArray;Importorg.json.JSONException;ImportOrg.json.JSONObject;ImportJava.io.PrintWriter;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;ImportJava.sql.ResultSetMetaData;Importjava.sql.SQLException;Importjava.sql.Statement;/** */@WebServlet ("/helloworld") Public classHelloWorldextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; PrivateString Diverclass; PrivateString UserName; PrivateString password; PrivateString URL; /*** Default constructor. */     PublicHelloWorld () {//TODO auto-generated Constructor stub        }         PublicString Resultsettojson (ResultSet rs)throwssqlexception,jsonexception {//JSON ArrayJsonarray array =NewJsonarray (); //get number of columnsResultSetMetaData MetaData =Rs.getmetadata (); intColumnCount =Metadata.getcolumncount (); //traverse each piece of data in the ResultSet         while(Rs.next ()) {Jsonobject jsonobj=NewJsonobject (); //iterate through each column             for(inti = 1; I <= ColumnCount; i++) {String columnName=Metadata.getcolumnlabel (i); String value=rs.getstring (columnName);              Jsonobj.put (columnName, value);           } array.put (Jsonobj); }                 returnarray.tostring (); }                  /**     * @seeHttpservlet#doget (httpservletrequest request, httpservletresponse response)*/@Overrideprotected voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {Connection conn=NULL; Statement stmt=NULL; ResultSet RS=NULL; Response.setcontenttype ("Text/html"); Response.setcharacterencoding ("GB2312"); PrintWriter out=Response.getwriter (); Diverclass=/*getservletconfig ().*/Getservletcontext (). Getinitparameter ("Driver"); UserName=/*getservletconfig ().*/Getservletcontext (). Getinitparameter ("username"); Password=/*getservletconfig ().*/Getservletcontext (). Getinitparameter ("Password"); URL=/*getservletconfig ().*/Getservletcontext (). Getinitparameter ("url"); String SQL= Request.getparameter ("SQL"); if(sql==NULL) {SQL= "SELECT * FROM ANIMALS"; }                                           Try{class.forname (diverclass); //conn = Drivermanager.getconnection ("Jdbc:mysql://localhost/bbs?user=root&password=12345678 ");Conn=drivermanager.getconnection (URL, username,password); stmt=conn.createstatement (); RS=stmt.executequery (SQL); HelloWorld Hello=NewHelloWorld (); Try{String Data=Hello.resultsettojson (RS);            OUT.PRINTLN (data); } Catch(jsonexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }                                                                                        } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); } finally {            Try {                if(rs! =NULL) {rs.close (); RS=NULL; }                if(stmt! =NULL) {stmt.close (); stmt=NULL; }                if(Conn! =NULL) {conn.close (); Conn=NULL; }            } Catch(SQLException e) {e.printstacktrace (); }        }    }    /**     * @seeHttpservlet#dopost (httpservletrequest request, httpservletresponse response)*/    protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {printwriter out=Response.getwriter (); Out.println ("<div style= ' width:100px;height:100px;background-color:red ' >123123</div>"); }}

Write a universal interface to PHP with Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.