2024年更新:
鉴于大家对源码的需求,所以去翻了以前的电脑以前的作业,打包上传。
源码下载
年代久远,我已经忘了,也不会。自行参考吧。
题目:
对于具体怎么写,我的分析:
数据表设计
手机信息表
销售信息表
前台登录
后台登录
没代码不好,先放个登录代码吧。分为前台和后台登录。
前台登录
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>前台登录</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body >
<%
String userid="";
String userpwd="";
try {
Class.forName("com.mysql.jdbc.Driver"); 驱动程序名
String url = "jdbc:mysql://127.0.0.1:3306/myphone"; //数据库名
String username = "root"; //数据库用户名
String password = ""; //数据库用户密码
Connection conn = DriverManager.getConnection(url, username, password); //连接状态
if(conn != null){
Statement stmt = null;
ResultSet rs = null;
String sql = "SELECT * FROM user_login;"; //查询语句
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
userpwd=rs.getString("pwd");
userid=rs.getString("id");
}
stmt.close();
conn.close();
}
else{
out.print("连接失败!");
}
}catch (Exception e) {
out.print("数据库连接异常!");
e.printStackTrace();
}
%>
<form name="loginForm" action="home.jsp" method="post">
<table align="center">
<tr>
<th>
用户名:
</th>
<th>
<input name="account" type="text">
</th>
</tr>
<tr>
<th>
密码:
</th>
<th>
<input name="password" type="password">
</th>
</tr>
<tr>
<th>
<input type="button" value="登录" onClick="validate()">
</th>
</tr>
</table>
</form>
<script type="text/javascript">
function validate(){
var aid=<%=userid%>;
var apwd=<%=userpwd%>;
var bid= loginForm.account.value;
var bpwd= loginForm.password.value;
if(aid==bid&&apwd==bpwd){
<%session.setAttribute("user_login","ok");%>
alert("登录成功");
loginForm.submit();
}
else{
alert("账号或密码错误");
return;
}
}
</script>
</body>
</html>
后台登录
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>后台登录</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body >
后台登录
<%
String userid="";
String userpwd="";
try {
Class.forName("com.mysql.jdbc.Driver"); 驱动程序名
String url = "jdbc:mysql://127.0.0.1:3306/myphone"; //数据库名
String username = "root"; //数据库用户名
String password = ""; //数据库用户密码
Connection conn = DriverManager.getConnection(url, username, password); //连接状态
if(conn != null){
Statement stmt = null;
ResultSet rs = null;
String sql = "SELECT * FROM admin_login;"; //查询语句
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
userpwd=rs.getString("pwd");
userid=rs.getString("id");
}
stmt.close();
conn.close();
}
else{
out.print("连接失败!");
}
}catch (Exception e) {
out.print("数据库连接异常!");
e.printStackTrace();
}
%>
<form name="adminloginForm" action="admin.jsp" method="post">
<table align="center">
<tr>
<th>
用户名:
</th>
<th>
<input name="account" type="text">
</th>
</tr>
<tr>
<th>
密码:
</th>
<th>
<input name="password" type="password">
</th>
</tr>
<tr>
<th>
<input type="button" value="登录" onClick="validate()">
</th>
</tr>
</table>
</form>
<script type="text/javascript">
function validate(){
var aid=<%=userid%>;
var apwd=<%=userpwd%>;
var bid= adminloginForm.account.value;
var bpwd= adminloginForm.password.value;
if(aid==bid&&apwd==bpwd){
<%session.setAttribute("admin_login","ok");%>
alert("登录成功");
adminloginForm.submit();
}
else{
alert("账号或密码错误");
return;
}
}
</script>
</body>
</html>
手机信息录入
add_phone1.jsp 表单提交
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>添加手机信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<script type="text/javascript">
function add_one(){
//提交
AddForm.submit();
}
function toB(){
window.location="admin.jsp";
}
</script>
<input type="button" value="回到后台" onClick="toB()"/><br/><br><br>
<form name="AddForm" action="add_phone.jsp" method="post">
添加一条手机销售信息 <br>
手机编号<input type="text" value="" name="number"><br/>
手机产商<input type="text" value="" name="brand"><br/>
手机型号<input type="text" value="" name="type"><br/>
入库时间<input type="text" value="" name="in_time"><br/>
<!--
库存数量<input type="text" value="" name="total"><br/>
-->
手机进价<input type="text" value="" name="in_price"><br/>
<input type="button" value="提交" onClick="add_one()"/>
</form>
</body>
</html>
add_phone.jsp 添加信息到数据库
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>添加手机信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<script type="text/javascript">
alert("添加成功");
window.location = "add_phone1.jsp";
</script>
<body>
<%
String number1=request.getParameter("number");
String brand1=request.getParameter("brand");
String type1=request.getParameter("type");
String in_time1=request.getParameter("in_time");
//int total1=(Integer.parseInt(request.getParameter("total")));
double in_price1=(Double.parseDouble(request.getParameter("in_price")));
try {
Class.forName("com.mysql.jdbc.Driver"); 驱动程序名
String url = "jdbc:mysql://127.0.0.1:3306/myphone"; //数据库名
String username = "root"; //数据库用户名
String password = ""; //数据库用户密码
Connection conn = DriverManager.getConnection(url, username, password); //连接状态
if(conn != null){
//Statement stmt = null;
int i=0;
ResultSet rs=null;
//String sql = "INSERT INTO phone(number,brand,type,in_time,total,in_price) VALUES('" + number1 + "','"+brand1+"','"+type1+"','"+in_time1+"','"+total1+"','"+in_price1+"')";
//stmt = conn.createStatement();
//i= stmt.executeUpdate(sql);
//response.sendRedirect("add.jsp");
//stmt.close();
PreparedStatement ps = null;
//查看有无此品牌型号,找到一条即停止。有的记住库存。在其他条库存+1
String sql1="update phone set total=total+1 where brand=? and type=?";
ps = conn.prepareStatement(sql1);
ps.setString(1,brand1); ps.setString(2,type1);
i= ps.executeUpdate();
//获取库存数
String sql2="select total from phone where brand=? and type=?";
ps = conn.prepareStatement(sql2);
ps.setString(1,brand1); ps.setString(2,type1);
rs=ps.executeQuery();
int mytotal=1;//原来没有这款手机时 库存1
while(rs.next()){
mytotal=rs.getInt("total");
break;
}
//添加手机信息
String sql3 = "INSERT INTO phone VALUES(?,?,?,?,?,?)";
ps = conn.prepareStatement(sql3);
ps.setString(1,number1); ps.setString(2,brand1); ps.setString(3,type1);
ps.setString(4,in_time1); ps.setInt(5,mytotal); ps.setDouble(6,in_price1);
i= ps.executeUpdate();
ps.close();
conn.close();
}
else{
out.print("连接失败!");
}
}catch (Exception e) {
e.printStackTrace();
out.print("数据库连接异常!");
}
%>
</html>
添加销售信息
add_sales1.jsp 表单提交
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>添加销售信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<script type="text/javascript">
function add_one(){
//提交
AddForm.submit();
}
function toB(){
window.location="admin.jsp";
}
</script>
<input type="button" value="回到后台" onClick="toB()"/><br/><br><br>
<form name="AddForm" action="add_sales.jsp" method="post">
添加一条手机销售信息 <br>
手机编号<input type="text" value="" name="number"><br/>
出售价格<input type="text" value="" name="sell_price"><br/>
出售时间<input type="text" value="" name="sell_time"><br/>
<input type="button" value="提交" onClick="add_one()"/>
</form>
</body>
</html>
add_sales.jsp 数据库操作
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>添加销售信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<script type="text/javascript">
alert("添加成功");
window.location = "add_sales1.jsp";
</script>
<body>
<%
String number1=request.getParameter("number");
double sell_price1=Double.parseDouble(request.getParameter("sell_price"));
String sell_time1=request.getParameter("sell_time");
try {
Class.forName("com.mysql.jdbc.Driver"); 驱动程序名
String url = "jdbc:mysql://127.0.0.1:3306/myphone"; //数据库名
String username = "root"; //数据库用户名
String password = ""; //数据库用户密码
Connection conn = DriverManager.getConnection(url, username, password); //连接状态
if(conn != null){
/*Statement stmt = null;
int i=0;
String sql = "INSERT INTO sales(number,sell_price,sell_time) VALUES('" + number1 + "','"+sell_price1+"','"+sell_time1+"')";
stmt = conn.createStatement();
i= stmt.executeUpdate(sql);
stmt.close();
*/
int i=0;
PreparedStatement ps = null;
//添加销售表
String sql1 = "INSERT INTO sales VALUES(?,?,?)";
ps = conn.prepareStatement(sql1);
ps.setString(1,number1); ps.setDouble(2,sell_price1); ps.setString(3,sell_time1);
i= ps.executeUpdate();
//查找这款手机品牌型号
ResultSet rs=null;
String sql3="select brand,type from phone where number=?";
ps = conn.prepareStatement(sql3);
ps.setString(1,number1);
rs=ps.executeQuery();
String brand1=null;
String type1=null;
while(rs.next()){
brand1=rs.getString("brand");
type1=rs.getString("type");
break;
}
//手机信息表删除
String sql2="delete from phone where number=?";
ps = conn.prepareStatement(sql2);
ps.setString(1,number1);
i= ps.executeUpdate();
//手机信息表修改 在手机信息表中删除对应number的手机信息 其他相同品牌型号信息,库存-1
String sql4="update phone set total=total-1 where brand=? and type=?";
ps = conn.prepareStatement(sql4);
ps.setString(1,brand1); ps.setString(2,type1);
i= ps.executeUpdate();
ps.close();
conn.close();
}
else{
out.print("连接失败!");
}
}catch (Exception e) {
e.printStackTrace();
out.print("数据库连接异常!");
}
%>
</html>
是数据库课程设计的作业,打算用jsp和mysql实现。
对于不会前端,不熟练jsp,后端日常使用框架而不写sql的我来说,真是太难了。
现在是12.1了,离学期结束不久了,文档也没写,我觉得我要凉了。正将需求细分,目标一天写一个(?)
此贴为记录,写完会上传完整代码和项目所有文档。
。。预计这将是重复代码很多并且bug其多的垃圾项目