代码改变世界

数据库的连接和登录

2017-02-26 18:14  ovols  阅读(419)  评论(0编辑  收藏  举报

软件工程概论—用户登录界面

程序所需的代码:

 

<%@ page language="java" import="java.sql.*" 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>登录结果</title>
</head>
<body>
    <%
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String driverName = "com.mysql.jdbc.Driver";
        String userName = "root";
        String userPwd = "123";
        String dbName = "hkz";
        String url1 = "jdbc:mysql://localhost:3306/" + dbName;
        String url2 = "?user=" + userName + "&password=" + userPwd;
        String url3 = "&useUnicode=true&characterEncoding=UTF-8";
        String url = url1 + url2 + url3;
        request.setCharacterEncoding("UTF-8");
        Class.forName(driverName);
        conn = DriverManager.getConnection(url);
        String sql = "select * from userinfo where user=?";
        pstmt = conn.prepareStatement(sql);
        String user = request.getParameter("userName");
        String password = request.getParameter("userPwd");
        pstmt.setString(1, user);
        rs = pstmt.executeQuery();
        if(rs.next()) {
            sql = "select * from userinfo where user=? and pwd=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();
            if(rs.next()) {
                %><center><h1>用户 <%=rs.getString("user")%>登陆成功!</h1></center><%
                
            }
            else {
                %><center><h1>密码错误!</h1></center><%
            }
        }    
        else {
            %><center><h1>用户名不存在!</h1></center>
        <%}
        if(rs != null) {
            rs.close();
        }
        if(pstmt != null) {
            pstmt.close();
        }
        if(conn != null) {
            conn.close();
        }
    %>
</body>
</html>
<%@ 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">
<script type="text/javascript">
    function $(id) {
        return document.getElementById(id);
    }
    
    function check() {
        var user = $("user").value;
        var pwd = $("pwd").value;
        
        $("userInfo").innerHTML="";
        $("pwdInfo").innerHTML="";
        
        if(user == "") {
            $("userInfo").innerHTML="用户名不能为空!";
            $("user").focus();
            return false;
        }
        
        if(pwd == "") {
            $("pwdInfo").innerHTML="密码不能为空!";
            $("pwd").focus();
            return false;
        }
        
        return true;
    }
</script>

<style type="text/css">
    #userInfo{color:red;}
    #pwdInfo{color:red;}
</style>

<title>登录</title>

</head>

<body>
    <center>
        <form action="loginResult.jsp" method="post" onsubmit="return check()">
            <br><br><br><br><br><br>
            <table>
                <tr><td>用户名:</td><td><input type="text" name="userName" id="user"><span id="userInfo" ></span></td></tr>
                <tr><td>密码:</td><td><input type="password" name="userPwd" id="pwd"><span id="pwdInfo"></span></td></tr>
                <tr><td></td><td><input type="submit" value="登     录"></td>
                
            </table>
        </form>
    </center>
</body>

</html>