本文主要是介绍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 blocke.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 blocke.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;
}
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和passwordString 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>
登录页面
登录成功
登录失败
这篇关于JSP的增删改查part1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!