JSP实现HTML网页对Mysql数据库的数据修改
Posted WillWinwin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSP实现HTML网页对Mysql数据库的数据修改相关的知识,希望对你有一定的参考价值。
- 注意获取原始Id,并将其赋值到当前的HTML中
- rs.next才能获取到数值,rs不可以
- 增加一些个人的逻辑判断,比如Id不可以修改,任何属性为空都不能成功修改
示例:
当然数据库中的数据也会相应的改变
<%@page import="java.sql.Connection,java.sql.Statement,java.util.Scanner,java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
request.setCharacterEncoding("utf-8");
String msg="";
String msg1="";
boolean error=false;
获取网址中传递的Id
String origId = request.getParameter("pid");
String value1 = null;
String value2 = null;
String value3 = null;
try
String connectString = "jdbc:mysql://localhost/test"
+ "?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8";
String user = "root";
String pwd = "mysql";
String sql = "select *from stu where id = '" + origId + "';";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(connectString, user, pwd);
Statement state = conn.createStatement();
将原始Id中的数据写入到当前的HTML中
ResultSet rs = state.executeQuery(sql);
while(rs.next()) 注意rs是空指针,要使用rs.next才能访问到数值
value1 = rs.getString("id");
value2 = rs.getString("name");
value3 = rs.getString("age");
rs.close();
state.close();
conn.close();
catch (Exception e)
e.printStackTrace();
String s1 = request.getParameter("sub");
if(s1!=null)
String Id = "";
String Name = "";
String Age = "";
String Grade = "";
String Hobby[]= "";
String Hobbies = "";
for(int j = 0 ; j < 1 ; j++)
Id = "";
Name = "";
Age = "";
Grade = "";
Hobbies = "";
if(request.getParameter("Id")!=null)
Id = request.getParameter("Id");
if(Id == "")
error = true;
msg = "ID cant be empty!";
break;
if(!Id.equals(origId))
error = true;
msg = "ID cant be changed!";
break;
if(request.getParameter("Name")!=null)
Name = request.getParameter("Name");
if(Name == "")
error = true;
msg = "Name cant be empty!";
break;
if(request.getParameter("Age")!=null)
Age = request.getParameter("Age");
if(Age == "")
error = true;
msg = "Age cant be empty!";
break;
if(request.getParameter("Grade")!=null)
Grade = request.getParameter("Grade");
if(Grade == "")
error = true;
msg = "Grade cant be empty!";
break;
if(request.getParameter("Hobby")!=null)
Hobby = request.getParameterValues("Hobby");
int len = Hobby.length;
for(int i = 0 ; i < len ; i++)
if(i==0)
if(len==1)
Hobbies = Hobby[0];
else
Hobbies = Hobbies + Hobby[i] + "+";
else if(i == (len-1))
Hobbies = Hobbies + Hobby[i];
else
Hobbies = Hobbies + Hobby[i] + "+";
if(Hobbies == "")
error = true;
msg = "Hobby cant be empty!";
break;
对数据库进行修改
if(!error)
try
String connectString = "jdbc:mysql://localhost/test"
+ "?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8";
String user = "root";
String pwd = "mysql";
String sql = null;
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(connectString, user, pwd);
Statement state = conn.createStatement();
sql = "update stu set name = '" + Name + "', age = '" + Age + "', grade = '" +
Grade + "', hobby = '" + Hobbies +"';";
if(Hobbies=="")
msg1 = "yes";
else
msg1 = "no";
int cnt = state.executeUpdate(sql);
if(cnt > 0)
msg = "Update success!";
else
msg = "Add Error!";
state.close();
conn.close();
catch (Exception e)
e.printStackTrace();
if(msg == "Update success!")
value1 = Id;
value2 = Name;
value3 = Age;
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改学生记录</title>
<style>
a:link,a:visited color:blue
.container
margin:0 auto;
width:500px;
text-align:center;
</style>
<body>
<div class="container">
<h1>修改学生记录</h1>
<form action="updateStu.jsp?pid=<%=origId %>"
method="post" name="f">
学号:<input name="Id" type="text" value= "<%=value1 %>" />
<br/><br/>
姓名:<input name="Name" type="text" value= "<%=value2 %>" />
<br/><br/>
年龄:<input type = "text" name = "Age" value= "<%=value3 %>" />
<br />
<br />
请选择你的年级:
<br />
<select name="Grade">
<option value="freshman">大学一年级</option>
<option value="shophomore">大学二年级</option>
<option value="junior" selected="selected">大学三年级</option>
<option value="senior">大学四年级</option>
</select>
<br />
<br />
请选择你的爱好:
<br />
<input type="checkbox" name="Hobby" value="sports">
sports
<input type="checkbox" name="Hobby" value="travel">
travel
<input type="checkbox" name="Hobby" value="music">
music
<br />
<br />
<input type="submit" name="sub" value="修改"/>
<input type="reset" name="rset" value="清空"/>
<br/><br/>
</form>
id is required!<br/><br/>
<p><%=msg %></p>
<p><%=msg1 %></p>
<a href='browseStu.jsp'>返回</a>
</div>
</body>
</html>
以上是关于JSP实现HTML网页对Mysql数据库的数据修改的主要内容,如果未能解决你的问题,请参考以下文章
同事打开两个JSP网页A和B,在其中一个A网页中修改数据,按"提交"之后,B网页要立即刷新,更新数据。
招新系统(jsp+servlet,实现简略前端网页注册登录+后台增删改查,分学生和管理员,Java语言,mysql数据库连接,tomcat服务器)