java web jdbc 数据更新

update1

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<form action="update2.jsp"method="post">
<br><br><br><br><br><br><br><br><br><br>
<div align="center">请输入学号:<input type="text" name="id"></div><br>
<div align="center"><input type="submit" value="查找"></div>
</form>
</body>

</html>

update2

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*" %> 
<!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>Insert title here</title>
</head>
<body>
<%! int id; %>
<%! int id1; %>
<%! String name; %>
<%! double weight;%>
<%! String tel; %>
<%
 id=Integer.parseInt(request.getParameter("id"));
String drivername="com.mysql.jdbc.Driver";
String user="root";
String usepwd="test";
String dbname="sys";
String url1="jdbc:mysql://localhost:3306/"+dbname;
String url2="?user="+user+"&password="+usepwd;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3;
Class.forName(drivername);
Connection conn=DriverManager.getConnection(url);
String sql="select * from java where id=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,id);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
id1=rs.getInt("id");
name=rs.getString("name");
    weight=rs.getDouble("weight");
    tel=rs.getString("tel");
}
%>
<form action="update3.jsp"method="post">
<br><br><br><br><br><br><br><br><br><br>
<div align="center">
学号:<input type="text" name="id" value=<%=id1%>><br>
姓名:<input type="text" name="name" value=<%=name%>><br>
体重:<input type="text" name="weight" value=<%=weight%>><br>
电话:<input type="text" name="tel"  value=<%=tel%>><br>
<input type="submit" value="提交">
</div>
</form>
</body>

</html>

update3

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*" %> 
<!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>Insert title here</title>
</head>
<body>
<%! int id; %>
<%! int id1; %>
<%! String name; %>
<%! double weight;%>
<%! String tel; %>
<%
String drivername="com.mysql.jdbc.Driver";
String user="root";
String usepwd="test";
String dbname="sys";
String url1="jdbc:mysql://localhost:3306/"+dbname;
String url2="?user="+user+"&password="+usepwd;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3;
Class.forName(drivername);
Connection conn=DriverManager.getConnection(url);
String sql="update java set weight=?,tel=? where id=?";
id=Integer.parseInt(request.getParameter("id"));
name=request.getParameter("name");
weight=Double.parseDouble(request.getParameter("weight"));
tel=request.getParameter("tel");
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setDouble(1, weight);
pstmt.setString(2,tel);
pstmt.setInt(3, id);
int n=pstmt.executeUpdate();
if(n>=1)out.print("数据更改成功"+"<br>");
else out.print("数据更改失败");
%>
</body>
</html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值