SQL Server对数据进行修改,修改数据库中的数据。
<table border=1px style="500px;height:200px;text-align:center;margin: 0 auto"> <tr style="background:red"> <td>编号</td> <td>书名</td> <td>价格</td> <td>喜爱度</td> <td>作者</td> <td>删除</td> <td>修改</td> </tr> <% //引用连接数据库方法 sqlServer sq=new sqlServer(); //得到数据库连接 Connection con = sq.getConnection(); //编写sql语句 String sql="select * from Dog"; //得到数据库对象操作 Statement st=con.createStatement(); // 得到查询的结果 ResultSet rs = st.executeQuery(sql); while(rs.next()){ %> <tr> <td> <%out.print(rs.getInt(1) + " "); %></td> <td> <%out.print(rs.getString(2) + " "); %></td> <td> <%out.print(rs.getInt(3) + " "); %></td> <td> <%out.print(rs.getInt(4)+"%" + " "); %></td> <td> <%out.println(rs.getString(5)+ " "); %></td> <td> <a href="Delect.jsp?id=<%out.print(rs.getInt(1) + " "); %>">删除</a></td> <td> <a href="Update.jsp?id=<%out.print(rs.getInt(1) + " "); %>">修改</a></td> </tr><% } //释放资源 sq.ShiFang(rs,st,con); %> </table>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>更新书的信息</title> </head> <body> <form action="update_Dog.jsp" method="post"> <table align="center"> <tr> <td>请输入编号:</td> <td><input type="text" name="id" readonly="readonly" value=" <%=request.getParameter("id") %>"></td> </tr> <tr> <td>请输入新的的书名:</td> <td><input type="text" name="name" ></td> </tr> <tr> <td>请输入书的价格:</td> <td><input type="text" name="health" ></td> </tr> <tr> <td>请输入书的喜爱度:</td> <td><input type="text" name="love" ></td> </tr> <tr> <td>请输入书的作者:</td> <td><input type="text" name="strain" ></td> </tr> <tr colspan=2> <td><input type="submit" value="提交"></td> <td><input type="reset" value="重置"></td> </tr> </table> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="jdbc.sqlServer"%> <%@page import="java.sql.Connection"%> <%@page import="java.sql.ResultSet"%> <% String id = request.getParameter("id"); String name = request.getParameter("name"); name = new String(name.getBytes("ISO-8859-1"),"utf-8"); String health = request.getParameter("health"); String love = request.getParameter("love"); String strain = request.getParameter("strain"); strain = new String(strain.getBytes("ISO-8859-1"),"utf-8"); //引用连接数据库方法 sqlServer sq=new sqlServer(); //得到数据库连接 Connection con = sq.getConnection(); //编写sql语句 String sql="update Dog set name=?, health=? ,love=?, strain=? where id=?"; //得到数据库操作对象 PreparedStatement st=null; st =con.prepareStatement(sql); st.setString(1,name); st.setString(2,health); st.setString(3,love); st.setString(4,strain); st.setString(5,id); // 得到查询的结果 if(st.executeUpdate()>0){ %> <script type="text/javascript"> alert("更新成功"); location="showLog.jsp"; </script> <% }else{ %> <script type="text/javascript"> alert("更新失败"); location="showLog.jsp"; </script> <% } %>