[SERVER + mobile client project] servlet Design of Android + servlet + JSON comprehensive case study [2]

Source: Internet
Author: User

In the previous article, the database was designed and Servlet was started. The technology used here is JDBC and JSON encapsulation and return.

First look at the JDBC tool class:

Package COM. ktvtop. util; import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. preparedstatement; import Java. SQL. resultset; import Java. SQL. statement;/*** access data tool class * @ author David Kun */public class jdbcutil {/*** database connection path, account and password, and set the encoding */public final static string url = "JDBC: mysql: // 193.168.1.108: 3306/test? Useunicode = true & characterencoding = GBK "; public final static string username =" liaokun "; public final static string Password =" 123456 "; /*** connect to the database ** @ return connection * @ throws exception */public static connection getconnection () throws exception {class. forname ("com. mySQL. JDBC. driver "); connection conn = drivermanager. getconnection (URL, username, password); Return conn;}/*** get a preparedstatement ** @ Param conn * database connection * @ Param SQL * SQL statement * @ return preparedstatement * @ throws exception **/public static preparedstatement getps (connection Conn, string SQL) throws exception {preparedstatement PS = Conn. preparestatement (SQL); Return pS ;} /*** create statement ** @ Param conn * database connection * @ Return Statement * @ throws exception */public static statement getst (connection conn) throws exception {statement St = Conn. createstatement (); Return st ;} /*** return a statement ** @ Param st * Statement * @ Param SQL * SQL data * @ return resultset * @ throws exception */public static resultset getrs (statement St, string SQL) throws exception {resultset rs = st.exe cutequery (SQL); Return Rs ;} /*** close all resources ** @ Param conn * database connection * @ Param st * Statement * @ Param RS * resultset * @ throws exception */public static void clos E (connection Conn, statement St, resultset RS) throws exception {If (RS! = NULL) {Rs. Close () ;}if (st! = NULL) {st. Close () ;}if (Conn! = NULL) {Conn. close ();}} /*** close all resources ** @ Param conn * database connection * @ Param st * Statement * @ throws exception */public static void close (connection Conn, statement st) throws exception {If (st! = NULL) {st. Close () ;}if (Conn! = NULL) {conn. Close ();}}}

Here is the key: note thatDopostHow is this method implemented.

package com.ktvtop.servlet;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.PreparedStatement;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.ktvtop.util.JDBCUtil;public class RegisterServlet extends HttpServlet {public RegisterServlet() {super();}public void destroy() {super.destroy(); // Just puts "destroy" string in log}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}/** * The doPost method of the servlet. <br> * * This method is called when a form has 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(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");String type=request.getParameter("type");String name=request.getParameter("name");String passwd=request.getParameter("passwd");String mail=request.getParameter("mail");System.out.println(type);System.out.println(name);System.out.println(passwd);System.out.println(mail);if(type.equals("register")){response.setContentType("application/json");PrintWriter out = response.getWriter();try {Connectionconn = JDBCUtil.getConnection();String sql = " insert into user(name,passwd,mail)"+"values(?,?,?)";PreparedStatement ps=JDBCUtil.getPs(conn, sql);ps.setString(1, name);ps.setString(2, passwd);ps.setString(3, mail);if (ps.executeUpdate()!=0) {System.out.println("insert success!");out.write("1");out.flush();out.close();}else{System.out.println("insert success!");out.write("0");out.flush();out.close();}JDBCUtil.close(conn, ps);} catch (Exception e) {// TODO: handle exceptionout.write("2");out.flush();out.close();}}}}

The servlet design is completed here. Here is a registered servlet. Please note.

Wait until the address of a Serlvet is:

"Http: // 193.168.1.122: 8080/jsondemo/servlet/registerservlet address depends on the IP address of your server.

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.