上一篇, 设计完成了数据库,接下来开始做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 { Connection conn = 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 exception out.write("2"); out.flush(); out.close(); } } } }
这里就完成了servlet的设计,这里是一个注册的servlet。请注意看。
等到一个serlvet的地址为:
"http://193.168.1.122:8080/JsonDemo/servlet/RegisterServlet 地址根据自己服务器IP来决定