上一篇, 設計完成了資料庫,接下來開始做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來決定