今天敲代码了吗?   

课程00作业

网站系统开发需要掌握的技术:

Java语言:JSP、Servlet、JDBC、JavaBean等

面向对象分析设计思想:UML统一建模语言的UML图
设计模式和框架结构
XML、html、css语言
网页脚本语言:JavaScript
数据库
应用服务器
集成开发环境(IDE)
 
本次课堂测试:
界面要求:绘制一个登陆界面:包括两个标签控件、两个文本框和一个登陆按钮;(完成界面绘制功能1分);
功能要求:
1、数据库要求:要求在数据库中建立用户表,表中包括用户名和密码(1分),实现数据库连接功能(1 分);
2、输入正确的用户名和密码,点击登录按钮,提示登录成功(0.5分)。输入错误的用户名或者密码,登录会失败,并提示错误信息(0.5分)
3什么都不输入,点击登陆按钮,提示“请输入用户名”信息(0.5分)
4、输入密码时加密显示(以星号“*”显示个数信息)。(0.5分)
源代码:
构造声明
 1 ackage com.Student.msg.model;
 2 
 3 public class User {
 4     private int id;
 5     private String username;
 6     private String nickname;
 7     private String password;
 8     public int getId() {
 9         return id;
10     }
11     public void setId(int id) {
12         this.id = id;
13     }
14     public String getUsername() {
15         return username;
16     }
17     public void setUsername(String username) {
18         this.username = username;
19     }
20     public String getNickname() {
21         return nickname;
22     }
23     public void setNickname(String nickname) {
24         this.nickname = nickname;
25     }
26     public String getPassword() {
27         return password;
28     }
29     public void setPassword(String password) {
30         this.password = password;
31     }
32     
33 }

 

接口

 1 package com.Student.msg.dao;
 2 
 3 import java.util.List;
 4 import com.Student.msg.model.User;
 5 public interface IUserDao {
 6     public void add(User user);
 7     public void delete(int id);
 8     public void update(User user);
 9     public User Login(String username);
10     public User load(int id);
11     public User load(String username);
12     public List<User> load();
13 }

数据库连接

package com.Student.msg.Util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
    public static Connection getConnection() {
        // 加载驱动
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            
        } catch ( ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String user="数据库名字";
        String password="密码";
        String url="jdbc:sqlserver://localhost:1433; DatabaseName=Student";
        Connection connection =null;
        
        // 创建链接对象
        try {
            connection=DriverManager.getConnection(url, user, password);
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
        
    }
    //关闭资源
    public static void close(PreparedStatement preparedStatement) {
        
             try {
                 if( preparedStatement !=null) {
                preparedStatement.close();
                 }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    
    public static void close(ResultSet resultSet)  {
        
            try {
                if(resultSet!= null) {
                resultSet.close();
            }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    
    public static void close(Connection connection) {
        // TODO Auto-generated method stub
        
            try {
                if(connection!=null) {
                connection.close();
                }
                } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

接口连接类

  1 package com.Student.msg.dao;
  2 
  3 import java.sql.Connection;
  4 import java.sql.PreparedStatement;
  5 import java.sql.ResultSet;
  6 import java.sql.SQLException;
  7 import java.util.ArrayList;
  8 import java.util.List;
  9 
 10 import com.Student.msg.Util.DBUtil;
 11 import com.Student.msg.Util.UserException;
 12 import com.Student.msg.model.User;
 13 
 14 public class UserDaoImpl implements IUserDao{
 15 
 16     @SuppressWarnings("resource")
 17     public User Login(String username) {
 18         // TODO Auto-generated method stub
 19         Connection connection=DBUtil.getConnection();
 20         //准备Sql
 21         String sql = "select * from Studen where username =?";
 22         //创建语句传输对象
 23         
 24         PreparedStatement preparedStatement=null;
 25         ResultSet resultSet =null;
 26         User user =null;
 27         try {
 28         preparedStatement =connection.prepareStatement(sql);
 29         preparedStatement.setString(1, username);
 30         resultSet =preparedStatement.executeQuery();
 31         while(resultSet.next()) {
 32             user =new User();
 33             user.setUsername(username);
 34             user.setId(resultSet.getInt("id"));
 35             user.setPassword(resultSet.getString("password"));
 36             user.setNickname(resultSet.getString("nickname"));
 37             //users.add(user);
 38         }
 39         }catch (SQLException e) {
 40             // TODO Auto-generated catch block
 41             e.printStackTrace();
 42         }
 43         finally {
 44             DBUtil.close(resultSet);
 45             DBUtil.close(preparedStatement);
 46             DBUtil.close(connection);
 47         }
 48         return user;
 49     }
 50                 
 51     
 52     @SuppressWarnings("resource")
 53     @Override
 54     public void add(User user) {
 55         // TODO Auto-generated method stub
 56         //获得连接对象
 57         Connection connection=DBUtil.getConnection();
 58         //准备sql语句
 59         String sql="select * from Studen where username = ? ";
 60         //创建语句传输对象
 61         PreparedStatement preparedStatement= null;
 62         ResultSet resultSet =null;
 63         try {
 64             preparedStatement = connection.prepareStatement(sql);
 65             preparedStatement.setString(1, user.getUsername());    
 66         //接收结果集
 67             resultSet =preparedStatement.executeQuery();
 68         
 69             //遍历结果集
 70             while(resultSet.next()) {
 71                 if(resultSet.getInt(1)>0) {
 72                     throw new UserException("用户存在 ");
 73                 }
 74         }    
 75             
 76             sql ="insert into Studen(username,password,nickname) values(?,?,?)";
 77             preparedStatement =connection.prepareStatement(sql);
 78             preparedStatement.setString(1, user.getUsername());
 79             preparedStatement.setString(2, user.getPassword());
 80             preparedStatement.setString(3, user.getNickname());
 81             preparedStatement.executeUpdate();
 82         } catch (SQLException e) {
 83             // TODO Auto-generated catch block
 84             e.printStackTrace();
 85         }finally {
 86             DBUtil.close(resultSet);
 87             DBUtil.close(preparedStatement);
 88             DBUtil.close(connection);
 89 
 90         }
 91         
 92         
 93     }
 94 
 95     @Override
 96     public void delete(int id) {
 97         // TODO Auto-generated method stub
 98         Connection connection =DBUtil.getConnection();
 99         String sql ="delete  from Studen where id= ?";
100         PreparedStatement preparedStatement=null;
101         try {
102             preparedStatement=connection.prepareStatement(sql);
103             preparedStatement.setInt(1, id);
104             preparedStatement.executeUpdate();
105         } catch (SQLException e) {
106             // TODO Auto-generated catch block
107             e.printStackTrace();
108         }finally {
109             DBUtil.close(preparedStatement);
110             DBUtil.close(connection);
111         }
112     }
113 
114     @Override
115     public void update(User user) {
116         // TODO Auto-generated method stub
117         Connection connection=DBUtil.getConnection();
118         //准备sql
119         String sql="update * Studen set password =? ,nickname =? where id=?";
120         //创建语句传输对象
121         PreparedStatement preparedStatement= null;
122         try {
123             preparedStatement =connection.prepareStatement(sql);
124             preparedStatement.setString(1, user.getPassword());
125             preparedStatement.setString(2, user.getNickname());
126             preparedStatement.setInt(3, user.getId());
127             preparedStatement.executeUpdate();
128         } catch (SQLException e) {
129             // TODO Auto-generated catch block
130             e.printStackTrace();
131         }
132         finally {
133             DBUtil.close(preparedStatement);
134             DBUtil.close(connection);
135         }
136     }
137 
138     @Override
139     public User load(int id) {
140         // TODO Auto-generated method stub
141         Connection connection =DBUtil.getConnection();
142         String sql ="selsct * from Studen where id= ?";
143         PreparedStatement preparedStatement=null;
144         ResultSet resultSet =null;
145         User user =null;
146         try {
147             preparedStatement =connection.prepareStatement(sql);
148             preparedStatement.setInt(1, id);
149             resultSet=preparedStatement.executeQuery();
150             while(resultSet.next()) {
151                 user =new User();
152                 user.setId(id);
153                 user.setUsername(resultSet.getString("username"));
154                 user.setPassword(resultSet.getString("password"));
155                 user.setNickname(resultSet.getString("nickname"));
156             }
157         } catch (SQLException e) {
158             // TODO Auto-generated catch block
159             e.printStackTrace();
160         }
161         finally {
162             DBUtil.close(resultSet);
163             DBUtil.close(preparedStatement);
164             DBUtil.close(connection);
165         }
166         return user;
167     }
168 
169     @Override
170     public User load(String username) {
171         // TODO Auto-generated method stub
172         Connection connection =DBUtil.getConnection();
173         String sql ="select * from Studen where username = ?";
174         PreparedStatement preparedStatement=null;
175         ResultSet resultSet =null;
176         User user =null;
177         try {
178             preparedStatement =connection.prepareStatement(sql);
179             preparedStatement.setString(1, username);
180             resultSet=preparedStatement.executeQuery();
181             while(resultSet.next()) {
182                 user =new User();
183                 user.setUsername(username);
184                 user.setId(resultSet.getInt("id"));
185                 user.setPassword(resultSet.getString("password"));
186                 user.setNickname(resultSet.getString("nickname"));
187             }
188         } catch (SQLException e) {
189             // TODO Auto-generated catch block
190             e.printStackTrace();
191         }
192         finally {
193             DBUtil.close(resultSet);
194             DBUtil.close(preparedStatement);
195             DBUtil.close(connection);
196         }
197         return user;
198     }
199 
200     @Override
201     public List<User> load() {
202         // TODO Auto-generated method stub
203         Connection connection=DBUtil.getConnection();
204         //准备Sql
205         String sql = "select * from Studen";
206         //创建语句传输对象
207         PreparedStatement preparedStatement=null;
208         ResultSet resultSet =null;
209         //集合中智能放入User对象
210         List<User> users=new ArrayList<User>();
211         User user =null;
212         try {
213         preparedStatement =connection.prepareStatement(sql);
214         resultSet =preparedStatement.executeQuery();
215         while(resultSet.next()) {
216             user =new User();
217             user.setUsername(resultSet.getString("username"));
218             user.setId(resultSet.getInt("id"));
219             user.setPassword(resultSet.getString("password"));
220             user.setNickname(resultSet.getString("nickname"));
221             users.add(user);
222         }
223         }catch (SQLException e) {
224             // TODO Auto-generated catch block
225             e.printStackTrace();
226         }
227         finally {
228             DBUtil.close(resultSet);
229             DBUtil.close(preparedStatement);
230             DBUtil.close(connection);
231         }
232         return users;
233     }
234 
235 }

异常处理类

 1 package com.Student.msg.Util;
 2 
 3 public class UserException extends RuntimeException {
 4     public UserException() {
 5         super();
 6         // TODO Auto-generated constructor stub
 7     }
 8 
 9     public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
10         super(message, cause, enableSuppression, writableStackTrace);
11         // TODO Auto-generated constructor stub
12     }
13 
14     public UserException(String message, Throwable cause) {
15         super(message, cause);
16         // TODO Auto-generated constructor stub
17     }
18 
19     public UserException(String message) {
20         super(message);
21         // TODO Auto-generated constructor stub
22     }
23 
24     public UserException(Throwable cause) {
25         super(cause);
26         // TODO Auto-generated constructor stub
27     }
28 
29 }

jsp登录界面

 1 <%@page import="com.Student.msg.Util.UserException"%>
 2 <%@page import="com.Student.msg.dao.UserDaoImpl"%>
 3 <%@page import="com.Student.msg.model.User"%>
 4 <%@ page language="java" contentType="text/html; charset=UTF-8"
 5     pageEncoding="UTF-8"%>
 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <%
 9     //接收客户端传递过来的参数
10     String username = request.getParameter("username");
11     String password = request.getParameter("password");
12     User user = new User();
13     if(username == null || "".equals(username.trim())){
14         request.setAttribute("error1", "用户名不能为空");
15 %>
16     <jsp:forward page="LoginInput.jsp"></jsp:forward>
17 <%
18     }
19     
20         if(password == null || "".equals(password.trim())){
21             request.setAttribute("error2", "密码不能为空");
22 %>
23     <jsp:forward page="LoginInput.jsp"></jsp:forward>
24 <%
25     }
26     
27     
28     //user.setUsername(username);
29     //user.setPassword(password);
30     user=null;
31     UserDaoImpl userDao = new UserDaoImpl();
32     try{
33     
34     user=userDao.Login(username);
35     if(user==null){
36         
37         request.setAttribute("error3", "用户不存在");
38 %>
39     <jsp:forward page="LoginInput.jsp"></jsp:forward>
40 <%
41             }
42     
43     if(user.getUsername().equals(username)&&user.getPassword().equals(password))
44     {
45     %>
46 
47     <br>用户登录成功!!<br>
48     <a href="Login.jsp">返回登录界面</a><br>
49     <a href="addInput.jsp">注册</a><br>
50     <a href="list.jsp">用户列表</a><br>
51     <a href="deleteInput.jsp">删除</a><br>
52 
53 <% 
54     }
55     
56     else{
57         %>
58         密码错误!!<br>
59         <a href="Login.jsp">返回登录界面</a><br>
60         <%
61         
62     }
63 
64     }catch(UserException e){
65 %>
66     <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
67     <%
68     }
69     %>
70 </html>

登录输入

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <title>用户登录页面</title>
 7 <style>
 8 #header{
 9 background-color:#80FFFF;
10 color:white;
11 text-align:left;
12 padding:3px;
13 }
14 #section1{
15 background-color:whirt;
16 width:200px;
17 text-align:center;
18 margin: 0 auto;
19 padding:10px;
20 
21 }
22 </style>
23 </head>
24 <body bgcolor="pink" >
25 <div id="header">
26 <h1>登录</h1>
27 <a href="addInput.jsp">注册新用户</a><br>
28 </div>
29 
30 <br><br><br><br><br><br>
31 <body>
32     <%=request.getAttribute("error") %>
33     <form action="Login.jsp" method="get">
34         <table align="center" border="1" width="500">
35             <tr>
36                 <td>用户名称 : </td>
37                 <td>
38                     <input type="text" name="username" />
39                 </td>
40             </tr>
41                 <tr>
42                 <td>用户密码:</td>
43                 <td>
44                     <input type="password" name="password" />
45                 </td>
46             </tr>
47             <tr align="center">
48                 <td colspan="2">
49                     <input type="submit"  style="background:#4DFFFF" value="登录" />
50                 </td>
51             </tr>
52         </table>
53     </form>
54 </body>
55 </html>

运行结果:

你对这门课的希望和自己的目标:希望在接下来几周内学习到更多的知识,对自己的目标是在结束这门课时有能力单独完成一个java web项目

具体列出你计划每周花多少时间在这门课上:

每周除去上课时间 ,每天保持至少1个半小时的时间

 

posted on 2017-11-23 10:29  今天学算法了吗?  阅读(216)  评论(0编辑  收藏  举报

导航