通过动态网页和JavaBean的应用来对数据库(SQLServer)来进行操作
本次操作是对于新手的综合练习,通过代码来实现动态网页通过调用Java类来对数据库进行编辑。
1 :首先在SQL server创建自己所需要的数据库(本此博客是以创建3个(pId,pName,pNum)三个为例
1:创建Javabean来实现对数据库的连接(如果不知道如何配置SQL server请到我的主页查看动态网页连接数据库博客)**
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.创建JavaBean“myc.java”,用于设置、存放、获取产品信息。 (快捷键 Alt + shift +s,之后选择需要的即可)
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 自动生成的构造函数存根
}
public myc(String pId, String pName, int pNum) {
super();
this.pId = pId;
this.pName = pName;
this.pNum = pNum;
}
}
3:创建JavaBean“mycDAO.java”,建立以下方法,提交程序代码截图。
(1)建立方法findmyc (String pID),功能是在数据表myc中查找产品编号为pID的产品是否存在,若存在则返回true,否则返回false。
(2)建立方法addmyc(myc myc),功能是在数据表myc中添加产品myc的信息,不需要返回值。
(3)建立方法querymyc(),功能是查询数据表myc全部记录,返回ResutSet对象。
(4)建立方法querymyc(String pID),功能是查询数据表product中产品编号是pID的记录,返回ResutSet对象。
(5)建立方法updatemyc (myc myc),功能是修改指定产品编号的产品信息,不需要返回值。
(6)建立方法deletemyc(String pID),功能是删除指定产品编号的产品记录,不需要返回值。
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);
}
}
1.创建网页addmyc.html,能输入产品编号、名称、数量,并提交到addmyc.jsp网页。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="addmyc.jsp" method="post">
<P>请输入要添加的信息</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="提交">
</form>
</body>
</html>
2.创建网页addmyc.jsp,判断产品是否存在,如果存在,则跳转到addmycFail.jsp网页;如果产品不存在,则调用JavaBean“mycDAO.java”的addmyc
(myc myc)方法添加产品信息到myc表,然后跳转到listmyc.jsp网页。
<%@page import="java.sql.ResultSet"%>
<%@page import="综合练习.mycDAO"%>
<%@page import="综合练习.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="综合练习.myc"></jsp:useBean>
<jsp:useBean id="m2" class="综合练习.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("添加成功");} %>
</body>
</html>
3.创建网页addmycFail.jsp,显示输入的产品名称及文本“产品已存在,添加失败!”。
<%@ 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("添加失败");}
%>
</body>
</htm
4.创建产品浏览网页listmyc.jsp,能通过调用JavaBean“mycDAO.java”的querymyc()方法,显示所有产品信息,在每条记录创建“修改”、“删除”超链接,分别链接到updatemyc.jsp和deletemyc.jsp网页并传递产品编号。
<%@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="综合练习.mycDAO"></jsp:useBean>
<jsp:useBean id="m2" class="综合练习.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") %>">修改</a>
<td><a href="deletemyc.jsp?pId=<%=rs.getString("pId") %>">删除</a>
</tr>
<%} %>
</table>
</body>
</html>
5.创建产品信息修改网页updatemyc.jsp,能通过调用JavaBean“mycDAO.java”的querymyc (String pID)方法显示产品原信息并修改,提交到updatemycDeo.jsp网页。
<%@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="综合练习.mycDAO"></jsp:useBean>
<jsp:useBean id="m2" class="综合练习.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="提交">
<%} %>
</form>
</body>
</html>
6.创建updatemycDeo.jsp网页,取得产品信息并调用JavaBean“mycDAO.java”的updatemyc(myc myc)方法修改myc表中相应产品信息,跳回listmyc.jsp网页。
<%@ 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="综合练习.myc"></jsp:useBean>
<jsp:useBean id="m2" class="综合练习.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.创建deletemyc.jsp网页,取得产品编号,应用JavaBean“productDAO.java”的deletemyc(String pID)方法删除对应产品记录,跳回listmyc.jsp网页。
<%@ 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="综合练习.mycDAO"></jsp:useBean>
<% m1.deletema(pId);
response.setHeader("refresh","3;url=listmyc.jsp");
%>
</body>
</html>