当前位置: 代码迷 >> Java Web开发 >> 怎么在JSP表单中添加数据库表中相关记录?附代码
  详细解决方案

怎么在JSP表单中添加数据库表中相关记录?附代码

热度:9291   发布时间:2013-02-25 21:20:00.0
如何在JSP表单中添加数据库表中相关记录???附代码~
HTML code
<%@ page language="java" import="java.util.*,java.sql.*,bao.*"  pageEncoding="UTF-8"%><%        request.setCharacterEncoding("UTF-8");        //从上一页面获得用户提交表单        String sSchlNumber,sPassword;        sSchlNumber = request.getParameter("schlNumber");        sSchlNumber = sSchlNumber.replaceAll("'","''");        sPassword = request.getParameter("password");        sPassword =  sPassword.replaceAll("'","''");                        //检索该用户输入的信息是否正确        String query = "select * from student where 学号='" + sSchlNumber + " ' and  密码 = '" + sPassword + " '";        Connection conn=null;        Statement stmt=null;        ResultSet rs=null;                     try{            //获得数据库连接            Class.forName("com.mysql.jdbc.Driver").newInstance();            conn = DriverManager.getConnection("jdbc:mysql://localhost/wode?user=root&password=123456");                        //创建statement            stmt = conn.createStatement();             //执行查询            rs = stmt.executeQuery(query);                                  /*  String num = rs.getString(1);           String name = rs.getString(2);          String  sex = rs.getString(4);           String pwd = rs.getString(3);          String   xueyuan = rs.getString(6);          String  zhuanye = rs.getString(7);          String   jiguan = rs.getString(8);          String   note = rs.getString(9);          String  room = rs.getString(5);     这样定义也不对,显示有错误 */                                              if(rs.next())            {                   //往session里设置相关属性                 session.setAttribute("学号",sSchlNumber);                                            // System.out.println(rs.getString(1));  这句话也显示不出来            }                       else{                                response.sendRedirect("error.jsp");                                }        }        catch(SQLException ex)             {                    ex.printStackTrace();                    response.sendRedirect("error.jsp");                    return;                            }finally{                            //关闭数据库                            stmt.close();                            conn.close();                }%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>            <title>学生宿舍管理系统登录验证界面-by8421</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">      </head>    <body>      <br><br><br>      <center><h2>你的基本信息如下,请检查,如有错误请及时联系管理员!</h2>          <table border ="1" cellpadding="0" cellspacing="0">             <tr>                <td>学号:</td><td><%=System.out.println(rs.getString(1))%></td>            </tr>            <tr>                <td>密码:</td><td><%="pwd"%></td>            </tr>            <tr>                <td>姓名:</td><td><%="name"%></td>            </tr>            <tr>                <td>性别:</td><td><%="sex"%></td>            </tr>            <tr>                <td>院系:</td><td><%="xueyuan"%></td>            </tr>            <tr>                <td>寝号:</td><td><%="room"%></td>            </tr>            <tr>                 <td>专业:</td><td><%="zhuanye"%></td>            </tr>            <tr>                 <td>籍贯:</td><td><%="jiguan"%></td>            </tr>            <tr>                 <td>备注:</td><td><%="note"%></td>            </tr>        </table><input type="button" value="返回" onclick="window.location = 'login-stu.jsp'"/><br><hr>                                                                  </center>                     </body></html>
  相关解决方案