课程作业1

Posted ╄冷丶夜♂

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了课程作业1相关的知识,希望对你有一定的参考价值。

 

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

对于JAVAWeb网站开发需要掌握的技术有JAVA、html、CSS、javascript、JQuery、Servlet等

2.本次课堂测试的源程序代码:

IUserDao.java

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

UserDaoImpl.java

  1 package com.jaovo.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.jaovo.msg.Util.DBUtil;
 11 import com.jaovo.msg.Util.UserException;
 12 import com.jaovo.msg.model.User;
 13 
 14 public class UserDaoImpl implements IUserDao{
 15 
 16     @Override
 17     public void add(User user) {
 18         Connection con = DBUtil.getConnection();
 19         String sql = "select count(*) from t_user where username = ?";
 20         PreparedStatement ps = null;
 21         ResultSet rs = null;
 22         try {
 23             ps = con.prepareStatement(sql);
 24             ps.setString(1, user.getUsername());
 25             rs = ps.executeQuery();
 26             while(rs.next()){
 27                 if(rs.getInt(1) > 0){
 28                     throw new UserException("用户已存在");
 29                 }
 30             }
 31         } catch (SQLException e) {
 32             // TODO Auto-generated catch block
 33             e.printStackTrace();
 34         }
 35         sql = "insert into t_user(username,password,nickname) value (?,?,?)";
 36         
 37         try {
 38             ps = con.prepareStatement(sql);
 39             ps.setString(1, user.getUsername());
 40             ps.setString(2, user.getPassword());
 41             ps.setString(3, user.getNickname());
 42             ps.executeUpdate();
 43         } catch (SQLException e) {
 44             // TODO Auto-generated catch block
 45             e.printStackTrace();
 46         }finally{
 47             DBUtil.close(rs);
 48             DBUtil.close(ps);
 49             DBUtil.close(con);
 50         }
 51     }
 52 
 53     @Override
 54     public void delete(int id) {
 55         // TODO Auto-generated method stub
 56         Connection con = DBUtil.getConnection();
 57         String sql = "delete from t_user where id = ?";
 58         PreparedStatement ps = null;
 59         try{
 60             ps = con.prepareStatement(sql);
 61             ps.setInt(1, id);
 62             ps.executeUpdate();
 63         }catch(SQLException e){
 64             e.printStackTrace();
 65         }finally{
 66             DBUtil.close(ps);
 67             DBUtil.close(con);
 68         }
 69     }
 70 
 71     @Override
 72     public void update(User user) {
 73         Connection con = DBUtil.getConnection();
 74         String sql = "update t_user set password = ?,nickname = ? where id = ?";
 75         PreparedStatement ps = null;
 76         try {
 77             ps = con.prepareStatement(sql);
 78             ps.setString(1, user.getPassword());
 79             ps.setString(2, user.getNickname());
 80             ps.setInt(3, user.getId());
 81             ps.executeUpdate();
 82         } catch (SQLException e) {
 83             // TODO Auto-generated catch block
 84             e.printStackTrace();
 85         }finally{
 86             DBUtil.close(ps);
 87             DBUtil.close(con);
 88         }
 89     }
 90 
 91     @Override
 92     public User load(int id) {
 93         Connection con = DBUtil.getConnection();
 94         String sql = "select * from t_user where id = ?";
 95         PreparedStatement ps = null;
 96         ResultSet rs = null;
 97         User user = null;
 98         try {
 99             ps = con.prepareStatement(sql);
100             ps.setInt(1, id);
101             rs = ps.executeQuery();
102             while(rs.next()){
103                 user = new User();
104                 user.setId(id);
105                 user.setUsername(rs.getString("username"));
106                 user.setPassword(rs.getString("password"));
107                 user.setNickname(rs.getString("nickname"));
108             }
109         } catch (SQLException e) {
110             // TODO Auto-generated catch block
111             e.printStackTrace();
112         }
113         
114         return user;
115     }
116 
117     @Override
118     public User load(String username) {
119         // TODO Auto-generated method stub
120         return null;
121     }
122 
123     public User load(String username,String password){
124         User user = null;
125         String sql = "select * from t_user where username = ? && password = ?";
126         Connection con = DBUtil.getConnection();
127         ResultSet rs = null;
128         try {
129             PreparedStatement ps = con.prepareStatement(sql);
130             ps.setString(1, username);
131             ps.setString(2, password);
132             rs = ps.executeQuery();
133             while(rs.next()){
134                 user = new User(rs.getInt("id"),rs.getString("username"),rs.getString("nickname"),rs.getString("password"));
135             }
136         } catch (SQLException e) {
137             // TODO Auto-generated catch block
138             e.printStackTrace();
139         }
140         return user;
141     }
142     
143     @Override
144     public List<User> load() {
145         Connection con = DBUtil.getConnection();
146         String sql = "select * from t_user";
147         ResultSet rs = null;
148         List<User> users = new ArrayList<User>();
149         try {
150             PreparedStatement ps = con.prepareStatement(sql);
151             rs = ps.executeQuery();
152             while(rs.next()){
153                 User user = new User(rs.getInt("id"),rs.getString("username"),rs.getString("nickname"),rs.getString("password"));
154                 users.add(user);
155             }
156         } catch (SQLException e) {
157             // TODO Auto-generated catch block
158             e.printStackTrace();
159         }
160         return users;
161     }
162 }

User.java

 1 package com.jaovo.msg.model;
 2 
 3 public class User {
 4     
 5     private int id;
 6     private String  username;
 7     private String  nickname;
 8     private String  password;
 9     public User(int id, String username, String nickname, String password) {
10         this.id=id;
11         this.nickname=nickname;
12         this.password=password;
13         this.username=username;
14     }
15     public User() {
16         // TODO Auto-generated constructor stub
17     }
18     public int getId() {
19         return id;
20     }
21     public void setId(int id) {
22         this.id = id;
23     }
24     public String getUsername() {
25         return username;
26     }
27     public void setUsername(String username) {
28         this.username = username;
29     }
30     public String getNickname() {
31         return nickname;
32     }
33     public void setNickname(String nickname) {
34         this.nickname = nickname;
35     }
36     public String getPassword() {
37         return password;
38     }
39     public void setPassword(String password) {
40         this.password = password;
41     }
42     
43 }

DBUtil.java

 1 package com.jaovo.msg.Util;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 
 9 public class DBUtil {
10     
11     public  static  Connection getConnection() {
12         try {
13             //1 鍔犺浇椹卞姩
14             Class.forName("com.mysql.jdbc.Driver").newInstance();
15         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
16             // TODO Auto-generated catch block
17             e.printStackTrace();
18         }
19         String user = "root";
20         String password = "root";
21         String url = "jdbc:mysql://localhost:3306/jaovo_msg";
22         Connection connection = null;
23         try {
24             //2 鍒涘缓閾炬帴瀵硅薄connection
25              connection = DriverManager.getConnection(url,user,password);
26         } catch (SQLException e) {
27             // TODO Auto-generated catch block
28             e.printStackTrace();
29         }
30         return connection;
31     }
32     
33     //鍏抽棴璧勬簮鐨勬柟娉�
34     public static void close(Connection connection ) {
35         try {
36             if (connection != null) {
37                 connection.close();
38             }
39             
40         } catch (SQLException e) {
41             // TODO Auto-generated catch block
42             e.printStackTrace();
43         }
44     }
45     public static void close(PreparedStatement preparedStatement ) {
46         try {
47             if (preparedStatement != null) {
48                 preparedStatement.close();
49             }
50             
51         } catch (SQLException e) {
52             // TODO Auto-generated catch block
53             e.printStackTrace();
54         }
55     }
56     public static void close(ResultSet resultSet ) {
57         try {
58             if (resultSet != null) {
59                 resultSet.close();
60             }
61             
62         } catch (SQLException e) {
63             // TODO Auto-generated catch block
64             e.printStackTrace();
65         }
66     }
67     
68 }

UserException.java

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

login.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 3 <html>
 4 <head>
 5     <title>登录</title>
 6     <link href="css/login_css.css" rel="stylesheet" type="text/css">
 7 
 8     <style type="text/css">
 9     body {
10         background-image: url(img/login.jpg);
11         background-repeat:no-repeat;
12         background-size:cover;
13     }
14     </style>
15 </head>
16 <body>
17 
18 <!--登录框-->
19 <div id="login">
20 <!--表单-->
21   <form name="Login" method="get" action="loginjudge.jsp">
22     <p></p>
23     <table width="300" height="175" border="0" align="center">
24       <tr>
25         <td width="120" align="right">用户名: </td>
26         <td colspan="2" align="center"><input name="name" type="text" tabindex="1" placeholder="用户名" class="log_input"></td>
27       </tr>
28       <tr>
29         <td align="right">密码:</td>
30         <td colspan="2" align="center"><input onkeyup="value=value.replace(/[^\\u0020-\\u007e]/ig,\'\')" type="password" name="password" id="password" tabindex="2" placeholder="密码" class="log_input"></td>
31       </tr>
32     </table>
33     <p><label id="tip">&nbsp;</label></p>
34     <p><label id="tip2">&nbsp;</label></p>
35     <p><input type="submit" name="login_button" value="登录" id="login_button" tabindex="4"></p>
36     <p class="tip">还没有账号?点击<a href=\'javascript:;\' onclick=\'showRegist()\'>注册</a></p>
37   </form>
38 </div>
以上是关于课程作业1的主要内容,如果未能解决你的问题,请参考以下文章

Android课程---Android Studio使用小技巧:提取方法代码片段

02-方法 课程作业01:将课程作业010203的设计思想程序流程图源程序代码和结果截图整理成一篇博文

20155324 《信息安全系统设计基础》课程总结

# 20155310 2017-2018-1 《信息安全系统设计基础》课程总结

2017-2018-1 20155310 《信息安全系统设计基础》课程总结

课后作业02-1-将课程作业01的设计思想程序流程图源程序代码和结果截图整理。