JSP网上购物系统//用户登入数据库连接

来源:互联网 发布:我们来了网络播出时间 编辑:程序博客网 时间:2024/06/10 22:47

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*,java.sql.*" errorPage="" %>
<%
     Connection con=null;

try{
     Class.forName("com.mysql.jdbc.Driver");  //载入驱动程序
     con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test_table?useUnicode=true&characterEncoding=gb2312","root","123456");  //连接数据库
     }catch(ClassNotFoundException e){
     out.print(e);   
     }
//数据库连接对象con如果不为空的话则表示已成功连接上数据库网上销售系统数据
        //
            String id=request.getParameter("id");//获取用户名
            String pwd=request.getParameter("password"); //获取密码
            System.out.print(id+""+pwd);
        String query = "SELECT * FROM admin WHERE id=? and password=?" ;
                PreparedStatement pstmt = null ;  //创建PreparedStatement对象
            pstmt = con.prepareStatement(query) ;  //发送sql语句
            pstmt.setString(1,id) ;
            pstmt.setString(2,pwd) ;

            ResultSet resultset= pstmt.executeQuery( ) ;   //得到结果集        
            try{
            if(resultset.next()){
            session.setAttribute("userId", id);//登录成功将用户名  存入session
            response.sendRedirect("loginSuccess.jsp");//网页跳转回页面
            }else{
            request.setAttribute("errInf"," *密码与账号不匹配");
            }
            }catch(SQLException sqle){
            System.err.println("Erro with connection:"+sqle);
            }
            con.close();
            pstmt.close();
            
%>
原创粉丝点击