下面介绍一下如何往数据库中加入数据。
<!-- 添加主人信息 --> <form action="add_Admin.jsp" method="post" > <table align="center"> <tr> <td>添加书籍:</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="提交" style="margin-left:0px;color:red;background:#8D0023"></td> <td><input type="reset" value="重置" style="margin-left:50px;color:red;background:#8D0023"></td> </tr> </table> </form>
处理提交的语句:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@page import="jdbc.sqlServer"%><%@page import="java.sql.Connection"%><%@page import="java.sql.ResultSet"%><%@page import="java.sql.PReparedStatement"%><% 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="insert into Dog values (?,?,?,?)"; //得到数据库操作对象 PreparedStatement st =con.prepareStatement(sql); st.setString(1,name); st.setString(2,health); st.setString(3,request.getParameter("love")); st.setString(4,strain); // 得到查询的结果 if(st.executeUpdate()>0){ %> <script type="text/Javascript"> alert("添加成功"); location="showLog.jsp"; </script> <% }else{ %> <script type="text/javascript"> alert("添加失败"); location="showLog.jsp"; </script> <% } %>
新闻热点
疑难解答