jsp相关简单应用

阅读更多
DAO:

package DAO;


public class dao {
	public static java.sql.Connection getconn(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String jdurl = ("jdbc:mysql://localhost:3306/exa");
			java.sql.Connection conn = java.sql.DriverManager.getConnection(jdurl, "root", "93694264");
			System.out.println("数据库连接成功");
			return conn;
		} catch (Exception e) {
			System.out.println("数据库连接失败");
			e.printStackTrace();
		}
		return null;
	}
	
	public static void insert(int id,String name){
		java.sql.Connection conn = getconn();
		String sqlinsert = "insert into exa(id,name) values(?,?);";
		java.sql.PreparedStatement pst = null;
		try{
			pst = conn.prepareStatement(sqlinsert);
			pst.setInt(1, id);
			pst.setString(2,name);
			pst.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void delete(int id) throws Exception{
		java.sql.Connection conn = getconn();
		conn.setAutoCommit(false);
		String sqldelete = "delete from exa where id = "+id;
		try{
			java.sql.Statement st = conn.createStatement();
			st.execute(sqldelete);
			conn.commit();
			}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void update(int id,String name){
		java.sql.Connection conn = getconn();
		String sqlupdate = "update exa set name = ? where id = ?;";
		java.sql.PreparedStatement pst = null;
		try{
			pst = conn.prepareStatement(sqlupdate);
			pst.setString(1,name);
			pst.setInt(2, id);
			pst.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}



index:

<%@page import="DAO.*"%>
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
  
  
  
  <%
  	Connection conn = dao.getconn();
  	String sql = "select * from exa;";
  	ResultSet rs = null;
  	Statement st = null;
  	try{
  		st = conn.createStatement();
  		rs = st.executeQuery(sql);
  	}catch(Exception e){
  		e.printStackTrace();
  	}
   %>
  总表:
  	
  		<%
  			while(rs.next()){
  			Mess mess = new Mess();
  			mess.setId(rs.getInt(1));
  			mess.setName(rs.getString(2));
  		 %>
  		 
  		 <%
  		 	}
  		 	rs.close();
  		 	st.close();
  		 	conn.close();
  		  %>
  	
id name 操作
<%=mess.getId() %> <%=mess.getName() %> 删除
添加信息:
id name 操作
修改信息:
id name 操作
servlet: package Myservlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import DAO.dao; public class serv_Servlet extends HttpServlet { /** * Constructor of the object. */ public serv_Servlet() { super(); } /** * The doGet method of the servlet.
* * This method is called when a form has its tag value method equals to get. * * @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 doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); String sid = request.getParameter("id"); String name = request.getParameter("name"); String action = request.getParameter("action"); int id = Integer.parseInt(sid); if(action.equals("login")){ if(id==1&name.equals("123456")){ response.sendRedirect("index.jsp"); } }else if(action.equals("add")){ try{ dao.insert(id, name); response.sendRedirect("index.jsp"); }catch(Exception e){ e.printStackTrace(); } }else if(action.equals("change")){ try{ dao.update(id, name); response.sendRedirect("index.jsp"); }catch(Exception e){ e.printStackTrace(); } }else if(action.equals("delete")){ try { dao.delete(id); response.sendRedirect("index.jsp"); } catch (Exception e) { e.printStackTrace(); } }else{ System.out.println("error!!"); } } /** * The doPost method of the servlet.
* * 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 { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println(" A Servlet"); out.println(" "); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" "); out.println(""); out.flush(); out.close(); } } login: <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> My JSP 'login.jsp' starting page
用户名
密码

 

你可能感兴趣的:(jsp,dao,servlet)