JSP的增删改查part1

运用Myeclisp对数据库进行增删改查

要建立6个库


1).其中dao层用与连接数据库和对数据库进行相关操作;

2).entity层用于存放数据库连接后的实体数据;

3.)service层是在mcv三层模式中新添加一层,能够更加清晰的定义应用程序的边界,需要操作数据的时候,通过service层访问DAO层来实现。service层做的事情,不仅仅是调用DAO操作数据,还会包含了一定的业务逻辑。整个程序的设计,也变成了针对服务进行设计。

4).servlet层:Servlet是用Java编写的Server端程序,它与协议和平台无关。Servlet运行于Java-enabled Web Server中。Java Servlet可以动态地扩展Server的能力,并采用请求-响应模式提供Web服务。

1.数据库的建立
》运用数据库软件Navicat Premium软件,在test数据库建立一名为cdsn的表格




2.DAO层的建立
首先建立一个BaseDao.java用于对数据库进行连接
public class BaseDao {
	//定义驱动
	private static String DRIVER="com.mysql.jdbc.Driver";
	//定义访问地址
	private static String URL="jdbc:mysql://localhost:3306/test";
	//定义数据库访问账户和密码
	private static String USER="root";
	private static String PWD="root";
	
	//设置连接
	public static  Connection getCon(){
		Connection conn=null;
		try {
			Class.forName(DRIVER);
			conn=DriverManager.getConnection(URL,USER,PWD);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	//设置关闭连接
	public static void getClo(Connection con,PreparedStatement ps,ResultSet rs){		
			try {
				if(rs!=null)
				rs.close();
				if(ps!=null)
				ps.close();
				if(con!=null)
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
	}	
3,在entity层中建立实体库
private int id;
private String username;
private String password;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
4.在dao层中建立CdsnDao接口,并在dao.impl层建立接口方法对数据库中数据进行处理
public interface CdsnDao {
	
	//获取cdsn用户数据列表
	public List<cdsn> getCdsnList();
	//获取cdsn中的用户名和密码并进行判断,用于登录
	public boolean getCdsnPass(String username,String password);
	
}
Impl
public class CdsnDaoImpl implements CdsnDao {
	Connection con = null;
	PreparedStatement ps =null;
	ResultSet rs = null;
	BaseDao bdao = new BaseDao();
//获取用户名和密码,并判断布尔值,用于登录
		public boolean getCdsnPass(String username,String password){
			boolean flag = false;
			//连接
			con=bdao.getCon();
			String sql = "select * from cdsn";
			try {				
				ps=con.prepareStatement(sql);
				rs=ps.executeQuery();
				//获取用户名和密码并进行判断
				while(rs.next()){
					cdsn cn = new cdsn();
					String uname=rs.getString("username");
					String upassword=rs.getString("password");
					if((uname.equals(username))&&(upassword.equals(password))){
						flag=true;
						break;
					}
				}	
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				bdao.getClo(con, ps, rs);
			}			
			return flag;			
		}		
5.设置service层, 同样先建立service接口,在建立接口方法
public interface CdsnService {
	//获得数据库cdsn表中所有数据
	public List<cdsn> getCdsnList();
	//获得数据库cdsn表中登录名和密码
	public boolean getCdsnPass(String username,String password);
}
Impl
public class CdsnServiceImpl implements CdsnService {
	//调用Cdsn的方法
	CdsnDao cdao = new CdsnDaoImpl();
public boolean getCdsnPass(String username,String password){
		boolean flag = cdao.getCdsnPass(username, password);
		return flag;
	}
}
6.建立servlet层,并建立LoginServlet方法对页面数据进行处理,其中重要代码如下:
public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获取登录界面的username和password
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		CdsnService cservice = new CdsnServiceImpl();
		boolean flag = cservice.getCdsnPass(username, password);
		request.getSession().setAttribute("username", username);
		request.getSession().setAttribute("password", password);
		System.out.println(username);
		if(flag){
			response.sendRedirect("Success.jsp");
		}else{
			response.sendRedirect("failure.jsp");
		}
	}
7,建立三个页面 index.jsp,Success.jsp,failure.jsp
index.jsp
<body>
  	<form action="LoginServlet" method="post">
    	用户名:<input type="text" name="username">
    	密码:<input type="password" name="password">
    	<input type="submit" value="登录">
    	<input type="reset" value="重置">
   	</form>
  </body>
Success.jsp
<body>
    This is my JSP page. <br>
  </body>
failure.jsp
<body>
    <a href="index.jsp">登录失败</a>
  </body>
登录页面

登录成功




登录失败














  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值