软件工程概论第一次作业
1.网站系统开发需要哪些技术
【1】Java语言
【2】面向对象分析设计思想
【3】xml语言
【4】网页脚本语言
【5】数据库
【6】应用服务器
【7】集成开发环境
2源代码(2017、11、21)
1 package com.jaovo.msg.dao; 2 3 4 5 import java.util.List; 6 7 import com.jaovo.msg.model.User; 8 9 public interface IUserDao { 10 public void add(User user); 11 public void delete(int id); 12 public void update(User user); 13 public User load(int id); 14 public User load(String username); 15 public List<User> load(); 16 17 } 18 19 ************************ 20 package com.jaovo.msg.model; 21 22 public class User { 23 private int id; 24 private String username; 25 private String nickname; 26 private String password; 27 public int getId() { 28 return id; 29 } 30 public void setId(int id) { 31 this.id = id; 32 } 33 public String getUsername() { 34 return username; 35 } 36 public void setUsername(String username) { 37 this.username = username; 38 } 39 public String getNickname() { 40 return nickname; 41 } 42 public void setNickname(String nickname) { 43 this.nickname = nickname; 44 } 45 public String getPassword() { 46 return password; 47 } 48 public void setPassword(String password) { 49 this.password = password; 50 } 51 52 53 } 54 ******************** 55 package com.jaovo.msg.Util; 56 57 import java.sql.Connection; 58 import java.sql.DriverManager; 59 import java.sql.ResultSet; 60 import java.sql.SQLException; 61 62 import java.sql.PreparedStatement; 63 64 public class DBUtil { 65 public static Connection getConnection() { 66 67 try { 68 69 Class.forName("com.mysql.jdbc.Driver").newInstance(); 70 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { 71 // TODO Auto-generated catch block 72 e.printStackTrace(); 73 } 74 String user = "root"; 75 String password = "root"; 76 String url = "jdbc:mysql://localhost:3306/mysql"; 77 Connection connection = null; 78 try { 79 80 connection = DriverManager.getConnection(url,user,password); 81 } catch (SQLException e) { 82 // TODO Auto-generated catch block 83 e.printStackTrace(); 84 } 85 return connection; 86 } 87 88 89 public static void close(Connection connection ) { 90 try { 91 if (connection != null) { 92 connection.close(); 93 } 94 95 } catch (SQLException e) { 96 // TODO Auto-generated catch block 97 e.printStackTrace(); 98 } 99 } 100 public static void close(PreparedStatement preparedStatement ) { 101 try { 102 if (preparedStatement != null) { 103 preparedStatement.close(); 104 } 105 106 } catch (SQLException e) { 107 // TODO Auto-generated catch block 108 e.printStackTrace(); 109 } 110 } 111 public static void close(ResultSet resultSet ) { 112 try { 113 if (resultSet != null) { 114 resultSet.close(); 115 } 116 117 } catch (SQLException e) { 118 // TODO Auto-generated catch block 119 e.printStackTrace(); 120 } 121 } 122 123 124 125 126 } 127 ************************ 128 package com.jaovo.msg.Util; 129 130 public class UserException extends RuntimeException{ 131 132 public UserException() { 133 super(); 134 // TODO Auto-generated constructor stub 135 } 136 137 public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 138 super(message, cause, enableSuppression, writableStackTrace); 139 // TODO Auto-generated constructor stub 140 } 141 142 public UserException(String message, Throwable cause) { 143 super(message, cause); 144 // TODO Auto-generated constructor stub 145 } 146 147 public UserException(String message) { 148 super(message); 149 // TODO Auto-generated constructor stub 150 } 151 152 public UserException(Throwable cause) { 153 super(cause); 154 // TODO Auto-generated constructor stub 155 } 156 157 } 158 159 ************************ 160 <%@page import="com.jaovo.msg.dao.UserDaoImpl"%> 161 <%@page import="com.jaovo.msg.Util.UserException"%> 162 <%@page import="com.jaovo.msg.model.User"%> 163 <%@ page language="java" contentType="text/html; charset=UTF-8" 164 pageEncoding="UTF-8"%> 165 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 166 <html> 167 <% 168 //接收客户端传递过来的参数 169 String username = request.getParameter("username"); 170 String password = request.getParameter("password"); 171 String nickname = request.getParameter("nickname"); 172 if(username == null || "".equals(username.trim())){ 173 request.setAttribute("error", "用户名不能为空"); 174 175 %> 176 <jsp:forward page="addInput.jsp"></jsp:forward> 177 <% 178 } 179 User user = new User(); 180 user.setUsername(username); 181 user.setPassword(password); 182 user.setNickname(nickname); 183 184 UserDaoImpl userDao = new UserDaoImpl(); 185 try{ 186 userDao.add(user); 187 %> 188 189 190 用户保存成功!!<br> 191 <a href="addInput.jsp">继续添加</a><br> 192 <a href="#">用户列表</a> 193 <% 194 }catch(UserException e){ 195 %> 196 <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2> 197 <% 198 } 199 %> 200 </html> 201 ************************** 202 <%@ page language="java" contentType="text/html; charset=UTF-8" 203 pageEncoding="UTF-8"%> 204 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 205 <html> 206 <head> 207 <title>用户添加页面</title> 208 </head> 209 <body> 210 211 <form action="add.jsp" method="get"> 212 <table align="center" border="1" width="500"> 213 <tr> 214 <td>用户名称 : </td> 215 <td> 216 <input type="text" name="username" /> 217 </td> 218 </tr> 219 <tr> 220 <td>用户密码:</td> 221 <td> 222 <input type="password" name="password" /> 223 </td> 224 </tr> 225 <tr> 226 <td>用户昵称:</td> 227 <td> 228 <input type="text" name="nickname" /> 229 </td> 230 </tr> 231 <tr align="center"> 232 <td colspan="2"> 233 <input type="submit" value="提交" /> 234 <input type="reset" value="重置" /> 235 </td> 236 </tr> 237 </table> 238 </form> 239 </body> 240 </html>
3运行截图