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