关注观看!
小型例题:
创建数据库,和表,初始化为:
1.创建第一个jsp页面查询数据库中的内容显示到页面上,创建form.jsp页面,代码如下:
<%@ page language="java" import="java.util.*" import="java.sql.*" 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>My JSP 'form.jsp' starting page</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>
<p style="font-size:35px; " align="center">学生成绩输入页面</p>
<div style="border:solid 2px black; height:250px;width:600px;margin:0 auto;">
<form action="update.jsp" method="post">
<table align="center">
<tr bgcolor="yellow">
<span style="padding-left:150px;">输入学生成绩:</span>
<th>学号</th>
<th>姓名</th>
<th>考试类型</th>
<th>分数</th>
</tr>
<%
Class.forName("com.mysql.jdbc.Driver");//加载驱动
Connection con=DriverManager.getConnection("jdbc:mysql:///user", "root", "123456");//连接数据库
String s1="select * from stu";
Statement st=con.createStatement();//创建Statement对象,执行SQL语句
ResultSet rs=st.executeQuery(s1);//
while(rs.next()){
String id=rs.getString(1);
String name=rs.getString(2);
String type=rs.getString(3);
int score=rs.getInt(4);%>
<tr bgcolor="pink">
<td><%=id %></td>
<td><%=name %></td>
<td><%=type %></td>
<td>
<%
if(score==0){%>
<input name="score" type="text">
<input name="id" type="hidden" value="<%=id%>">
<%}else{
out.print(score);
}
%>
</td>
</tr>
<input type="submit" value="提交成绩" style="position:absolute;top:250px;left:400px;">
<input type="button" value="返回主页面" style="position:absolute;top:250px;left:500px;">
<%}
%>
</table>
</form>
</div>
</body>
</html>
2.创建更新数据页面,update.jsp页面,代码如下:
<%@ page language="java" import="java.util.*" import="java.sql.*" 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>My JSP 'update.jsp' starting page</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>
<%
request.setCharacterEncoding("utf-8");
String[] score=request.getParameterValues("score");//利用数据接收,成绩不只一个
String[] id=request.getParameterValues("id");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql:///user", "root", "123456");
String s1="update stu set score=? where id=?";
PreparedStatement ps=con.prepareStatement(s1);
for(int i=0;i<id.length;i++){
if(!score[i].equals("")){
ps.setString (1, score[i]);
ps.setString(2, id[i]);
ps.executeUpdate();
}
}
ps.close();
con.close(); //关闭连接
%>
<jsp:forward page="form.jsp"></jsp:forward>
</body>
</html>
结果:
输入成绩:
点击提交成绩:
数据库中成绩变化: