【伺服器+手機端項目】Android+Servlet+JSON綜合案例之Servlet設計【二】

來源:互聯網
上載者:User

上一篇, 設計完成了資料庫,接下來開始做Servlet啦, 這裡主要是用到的技術就是JDBC和JSON的封裝返回。

先看JDBC工具類:

package com.ktvtop.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;/** * 訪問資料工具類 * @author David kun */public class JDBCUtil {/** * 資料庫連接路徑,賬戶與密碼,並設定編碼 */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";/** * 串連資料庫 *  * @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;}/** * 得到一個PreparedStatement *  * @param conn *            資料庫連接 * @param sql *            sql語句 * @return PreparedStatement * @throws Exception *  */public static PreparedStatement getPs(Connection conn, String sql)throws Exception {PreparedStatement ps = conn.prepareStatement(sql);return ps;}/** * 建立Statement *  * @param conn *            資料庫連接 * @return Statement * @throws Exception */public static Statement getSt(Connection conn) throws Exception {Statement st = conn.createStatement();return st;}/** * 返回一個Statement *  * @param st *            Statement * @param sql *            sql資料 * @return ResultSet * @throws Exception */public static ResultSet getRs(Statement st, String sql) throws Exception {ResultSet rs = st.executeQuery(sql);return rs;}/** * 關閉所有資源 *  * @param conn *            資料庫連接 * @param st *            Statement * @param rs *            ResultSet * @throws Exception */public static void close(Connection conn, Statement st, ResultSet rs)throws Exception {if (rs != null) {rs.close();}if (st != null) {st.close();}if (conn != null) {conn.close();}}/** * 關閉所有資源 *  * @param conn *            資料庫連接 * @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();}}}

這裡是關鍵啦:注意看doPost這個方法中是怎麼做的。

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();}}}}

這裡就完成了servlet的設計,這裡是一個註冊的servlet。請注意看。

等到一個serlvet的地址為:

"http://193.168.1.122:8080/JsonDemo/servlet/RegisterServlet   地址根據自己伺服器IP來決定

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.