编程知识 cdmana.com

Dynamic web page (JSP) through calling JavaBean class to add, change, delete and other operations on the database (novice dynamic comprehensive exercise)

Through dynamic web pages and JavaBean To the database (SQLServer) To operate
This operation is a comprehensive exercise for novices , Through the code to achieve dynamic web pages, through the call Java Class to edit the database .

( One ): Connect to database

1 : First, in the SQL server Create your own database ( This blog is to create 3 individual (pId,pName,pNum) Three examples

 Insert picture description here

( Two ) Create a complete Javabean

1: establish Javabean To realize the connection to the database ( If you don't know how to configure SQL server Please go to my home page to view the dynamic web connection database blog )**

import java.sql.Connection;
import java.sql.DriverManager;

public class DB {
   
   
	public  Connection conn() throws Exception {
   
   
		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		String strcon="jdbc:sqlserver://localhost:1433;DatabaseName=ma";
		String struser="sa";
		String strpassword="123456";
		Connection conn =DriverManager.getConnection(strcon,struser,strpassword);
		return conn;
	}
}

2. establish JavaBean“myc.java”, Used for setting up 、 Deposit 、 Get product information . ( Shortcut key Alt + shift +s, After the need to choose )

public class myc {
   
   
	private String pId;
	private String pName;
	private int pNum;
	public String getpId() {
   
   
		return pId;
	}
	public void setpId(String pId) {
   
   
		this.pId = pId;
	}
	public String getpName() {
   
   
		return pName;
	}
	public void setpName(String pName) {
   
   
		this.pName = pName;
	}
	public int getpNum() {
   
   
		return pNum;
	}
	public void setpNum(int pNum) {
   
   
		this.pNum = pNum;
	}
	public myc() {
   
   
		super();
		// TODO  Auto generated constructor stub 
	}
	public myc(String pId, String pName, int pNum) {
   
   
		super();
		this.pId = pId;
		this.pName = pName;
		this.pNum = pNum;
	}
	

}

3: establish JavaBean“mycDAO.java”, Establish the following method , Submit program code screenshot .
(1) Build methods findmyc (String pID), The function is in the data table myc The product number is pID Whether or not there is a product of , If it exists, it returns true, Otherwise return to false.
(2) Build methods addmyc(myc myc), The function is in the data table myc Add products to myc Information about , You don't need to return a value .
(3) Build methods querymyc(), The function is to query the data table myc All records , return ResutSet object .
(4) Build methods querymyc(String pID), The function is to query the data table product The product number is pID The record of , return ResutSet object .
(5) Build methods updatemyc (myc myc), The function is to modify the product information of the specified product number , You don't need to return a value .
(6) Build methods deletemyc(String pID), The function is to delete the product record with the specified product number , You don't need to return a value .





import java.sql.ResultSet;
import java.sql.*;

public class mycDAO {
   
   
	public boolean findmyc(String pId) throws Exception{
   
   
		DB db=new DB();
		Statement sta = db.conn().createStatement();
		String select="select * from test2 where pId='"+pId+"'";
		  ResultSet rs= sta.executeQuery(select);
		  if(rs.next()){
   
   
			  return true;
		  }
		  else{
   
   
			  return false;
		  }
	}
	public void addmyc(myc myc) throws Exception{
   
   
		DB db=new DB();
		Statement sta = db.conn().createStatement();
		String add="insert into test2 (pId,pName,pNum) values('"+myc.getpId()+"','"+myc.getpName()+"','"+myc.getpNum()+"')";
		sta.executeUpdate(add);
	}
	public ResultSet queryma() throws Exception{
   
   
		DB db=new DB();
		Statement sta = db.conn().createStatement();
		String select="select * from test2";
		return sta.executeQuery(select);
	}
	public ResultSet querymyc(String pId) throws Exception{
   
   
		DB db=new DB();
		Statement sta = db.conn().createStatement();
		String select="select * from test2 where pId='"+pId+"'";
		return sta.executeQuery(select);
		}
	public void uppdatema(myc myc)throws Exception {
   
   
		DB db=new DB();
		Statement sta = db.conn().createStatement();
		String update="update test2 set pId='"+myc.getpId()+"',pName="+myc.getpName()+",pNum='"+myc.getpNum()+"' where pId='"+myc.getpId()+"'";
		sta.executeUpdate(update);
	}
	public void deletema(String pId)throws Exception{
   
   
		DB db=new DB();
		Statement sta = db.conn().createStatement();
		String delete ="delete from test2 where pId='"+pId+"'";
		sta.executeUpdate(delete);
	}
}

( 3、 ... and ): Create a web page

1. Create a web page addmyc.html, Can input product number 、 name 、 Number , And submit to addmyc.jsp Webpage .

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="addmyc.jsp" method="post">
<P> To add information for </p>
<P>pId<input type="text" name="pId"></P>
<p>pName<input type="text" name="pName"></p>
<p>pNum<input type="text" name="pNum"></p>
<input type="submit" value=" Submit ">


</form>
</body>
</html>

2. Create a web page addmyc.jsp, Determine whether the product exists , If there is , The jump to addmycFail.jsp Webpage ; If the product does not exist , Call JavaBean“mycDAO.java” Of addmyc
(myc myc) Method to add product information to myc surface , And then jump to listmyc.jsp Webpage .

<%@page import="java.sql.ResultSet"%>
<%@page import=" Comprehensive practice .mycDAO"%>
<%@page import=" Comprehensive practice .myc"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%  request.setCharacterEncoding("utf-8"); 

%>
<jsp:useBean id="myc" class=" Comprehensive practice .myc"></jsp:useBean>
<jsp:useBean id="m2" class=" Comprehensive practice .mycDAO"></jsp:useBean>
<jsp:setProperty property="*" name="myc"/>

<% System.out.print(myc.getpId());
boolean ss= m2.findmyc(myc.getpId());
if(ss==true){
   
   
	session.setAttribute("ls","ok");
	response.sendRedirect("addmycfail.jsp");
}
else{
   
   
 m2.addmyc(myc);
 System.out.print(" Add success ");} %>



</body>
</html>

3. Create a web page addmycFail.jsp, Display the entered product name and text “ The product already exists , Add failure !”.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% request.setCharacterEncoding("utf-8");
String ss=(String)session.getAttribute("ls");
if("ok".equals(ss)){
   
   
out.println(" Add failure ");}
%>
</body>
</htm

4. Create product browsing pages listmyc.jsp, By calling JavaBean“mycDAO.java” Of querymyc() Method , Show all product information , Create... On each record “ modify ”、“ Delete ” Hyperlinks , Link to updatemyc.jsp and deletemyc.jsp Web page and pass the product number .

<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1px">
<% request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="m1" class=" Comprehensive practice .mycDAO"></jsp:useBean>
<jsp:useBean id="m2" class=" Comprehensive practice .myc"></jsp:useBean>
<tr>
<td>pId</td>
<td>pName</td>
<td>pNum</td>
</tr>
<% ResultSet rs = m1.queryma();
	while (rs.next()){
   
   
		%>

<tr>

<td><%=rs.getString("pId") %></td>
<td><%=rs.getString("pName") %></td>
<td><%=rs.getString("pNum") %></td>
<td><a href="updatemyc.jsp?pId=<%=rs.getString("pId") %>"> modify </a>
<td><a href="deletemyc.jsp?pId=<%=rs.getString("pId") %>"> Delete </a>
</tr>

<%} %>
</table>
</body>
</html>

5. Create a product information modification page updatemyc.jsp, By calling JavaBean“mycDAO.java” Of querymyc (String pID) Methods display the original product information and modify it , Submitted to the updatemycDeo.jsp Webpage .

<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% request.setCharacterEncoding("utf-8"); 
 String pId=request.getParameter("pId");%>
<jsp:useBean id="m1" class=" Comprehensive practice .mycDAO"></jsp:useBean>
<jsp:useBean id="m2" class=" Comprehensive practice .myc"></jsp:useBean>
<jsp:setProperty property="*" name="m2"/>
<% ResultSet rs = m1.querymyc(pId); 
while(rs.next()){
   
   
%>
<form action="updatemycDeo.jsp" method="post">
<p>pId<input type="text" value=<%=rs.getString("pId") %> name="pId" readonly></p>
<p>pName<input type="text" value=<%=rs.getString("pName") %> name="pName" ></p>
<p>pNum<input type="text" value=<%=rs.getString("pNum") %> name="pNum" ></p>
<input type="submit" value=" Submit ">
<%} %>
</form>
</body>
</html>

6. establish updatemycDeo.jsp Webpage , Get product information and call JavaBean“mycDAO.java” Of updatemyc(myc myc) Methods to modify myc Corresponding product information in the table , Jump back listmyc.jsp Webpage .

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%  request.setCharacterEncoding("utf-8");
	//String pId=request.getParameter("pId");
	//String pName=request.getParameter("pName");
	//String pNum=request.getParameter("pNum");
%>
<jsp:useBean id="m1" class=" Comprehensive practice .myc"></jsp:useBean>
<jsp:useBean id="m2" class=" Comprehensive practice .mycDAO"></jsp:useBean>
<jsp:setProperty property="*" name="m1"/>
<% System.out.print(m1.getpNum());
m2.uppdatema(m1);
response.setHeader("refresh","3;url=listmyc.jsp");
%>
</body>
</html>

7. establish deletemyc.jsp Webpage , Get the product number , application JavaBean“productDAO.java” Of deletemyc(String pID) Method to delete the corresponding product record , Jump back listmyc.jsp Webpage .

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% request.setCharacterEncoding("utf-8");
 String pId = request.getParameter("pId");
%>
<jsp:useBean id="m1" class=" Comprehensive practice .mycDAO"></jsp:useBean>
<% m1.deletema(pId);
response.setHeader("refresh","3;url=listmyc.jsp");
%>
</body>
</html>

This is just my personal note , Share it with you , If you can , Leave a note ( perhaps ) Go ahead . Crab !!

版权声明
本文为[osc_ sju4uxml]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225092906525b.html

Scroll to Top