软件工程概论第一课
Posted 渔夫的梦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软件工程概论第一课相关的知识,希望对你有一定的参考价值。
一.网站系统开发需要掌握的技术
1.javaweb
2.html
3.CSS
二.课堂测试的源代码
1 package com.jaovo.msg.dao; 2 3 4 import com.jaovo.msg.model.User; 5 6 public interface IUserDao { 7 8 public void login(User user); 9 public void register(User user); 10 11 12 }
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 8 9 import com.jaovo.msg.Util.DBUtil; 10 import com.jaovo.msg.Util.UserException; 11 import com.jaovo.msg.model.User; 12 13 14 15 public class UserDaoImpl implements IUserDao { 16 17 public void login(User user) { 18 19 Connection connection = DBUtil.getConnection(); 20 String sql = "select count(*) from t_login where username=? And password = ?"; 21 PreparedStatement preparedStatement = null; 22 ResultSet resultSet = null; 23 24 try { 25 preparedStatement = connection.prepareStatement(sql); 26 preparedStatement.setString(1, user.getUsername()); 27 preparedStatement.setString(2, user.getPassword()); 28 System.out.println("当前的文本框中的姓名为:"+user.getUsername()); 29 System.out.println("当前的文本框中的密码为:"+user.getPassword()); 30 resultSet = preparedStatement.executeQuery(); 31 32 while(resultSet.next()) { 33 34 if (resultSet.getInt(1) > 0) 35 { 36 return; 37 } 38 else 39 { 40 throw new UserException("用户不存在!") ; 41 } 42 43 44 } 45 46 47 48 } catch (SQLException e) { 49 e.printStackTrace(); 50 }finally { 51 DBUtil.close(resultSet); 52 DBUtil.close(preparedStatement); 53 DBUtil.close(connection); 54 } 55 56 } 57 58 public void register(User user) { 59 Connection connection = DBUtil.getConnection(); 60 String sql = "select count(*) from t_login where username = ?"; 61 PreparedStatement preparedStatement = null; 62 ResultSet resultSet = null; 63 64 try { 65 preparedStatement = connection.prepareStatement(sql); 66 preparedStatement.setString(1, user.getUsername()); 67 resultSet = preparedStatement.executeQuery(); 68 69 70 while(resultSet.next()) { 71 72 if (resultSet.getInt(1) > 0) 73 { 74 throw new UserException("用户已存在!") ; 75 } 76 77 } 78 79 sql = "insert into t_login(username,password) values(?,?)"; 80 preparedStatement = connection.prepareStatement(sql); 81 82 preparedStatement.setString(1,user.getUsername()); 83 preparedStatement.setString(2,user.getPassword()); 84 85 preparedStatement.executeUpdate(); 86 87 } catch (SQLException e) { 88 e.printStackTrace(); 89 }finally { 90 DBUtil.close(resultSet); 91 DBUtil.close(preparedStatement); 92 DBUtil.close(connection); 93 } 94 95 } 96 97 98 99 }
1 package com.jaovo.msg.model; 2 3 public class User { 4 5 private int id; 6 private String username; 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 21 public String getPassword() { 22 return password; 23 } 24 public void setPassword(String password) { 25 this.password = password; 26 } 27 28 }
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 14 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); 15 16 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { 17 // TODO Auto-generated catch block 18 e.printStackTrace(); 19 } 20 String user = "sa"; 21 String password = "jisuan@10Q"; 22 String url = "jdbc:sqlserver://localhost:1433;DatabaseName=hoghouse"; 23 Connection connection = null; 24 try { 25 connection = DriverManager.getConnection(url,user,password); 26 27 } catch (SQLException e) { 28 29 e.printStackTrace(); 30 } 31 return connection; 32 } 33 34 35 public static void close(Connection connection ) { 36 try { 37 if (connection != null) { 38 connection.close(); 39 } 40 41 } catch (SQLException e) { 42 // TODO Auto-generated catch block 43 e.printStackTrace(); 44 } 45 } 46 public static void close(PreparedStatement preparedStatement ) { 47 try { 48 if (preparedStatement != null) { 49 preparedStatement.close(); 50 } 51 52 } catch (SQLException e) { 53 // TODO Auto-generated catch block 54 e.printStackTrace(); 55 } 56 } 57 public static void close(ResultSet resultSet ) { 58 try { 59 if (resultSet != null) { 60 resultSet.close(); 61 } 62 63 } catch (SQLException e) { 64 // TODO Auto-generated catch block 65 e.printStackTrace(); 66 } 67 } 68 69 }
1 package com.jaovo.msg.Util; 2 3 public class UserException extends RuntimeException{ 4 5 /** 6 * 7 */ 8 private static final long serialVersionUID = 1L; 9 10 public UserException() { 11 super(); 12 // TODO Auto-generated constructor stub 13 } 14 15 16 public UserException(String message) { 17 super(message); 18 // TODO Auto-generated constructor stub 19 } 20 21 22 }
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 </head> 8 <body> 9 <%=request.getAttribute("error") %> 10 <form action="back-login.jsp" method="get"> 11 <table align="center" border="1" width="500"> 12 <tr> 13 <td>用户名称 : </td> 14 <td> 15 <input type="text" name="username" /> 16 </td> 17 </tr> 18 <tr> 19 <td>用户密码:</td> 20 <td> 21 <input type="password" name="password" /> 22 </td> 23 </tr> 24 <tr align="center"> 25 <td colspan="2"> 26 <input type="submit" value="登录" /> 27 </td> 28 <td colspan="2"> 29 <a href="front-register.jsp">注册</a> 30 </td> 31 </tr> 32 </table> 33 </form> 34 </body> 35 </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 </head> 8 <body> 9 <%=request.getAttribute("error") %> 10 <form action="back-register.jsp" method="get"> 11 <table align="center" border="1" width="500"> 12 <tr> 13 <td>用户名称 : </td> 14 <td> 15 <input type="text" name="username" /> 16 </td> 17 </tr> 18 <tr> 19 <td>用户密码:</td> 20 <td> 21 <input type="password" name="password" /> 22 </td> 23 </tr> 24 <tr align="center"> 25 <td colspan="2"> 26 <input type="submit" value="注册" /> 27 <input type="reset" value="重置" /> 28 </td> 29 </tr> 30 </table> 31 </form> 32 </body> 33 </html>
1 <%@page import="com.jaovo.msg.Util.UserException"%> 2 <%@page import="com.jaovo.msg.dao.UserDaoImpl"%> 3 <%@page import="com.jaovo.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 if(username == null || "".equals(username.trim())){ 13 request.setAttribute("error", "用户名不能为空"); 14 %> 15 <jsp:forward page="front-login.jsp"></jsp:forward> 16 <% 17 } 18 User user = new User(); 19 user.setUsername(username); 20 user.setPassword(password); 21 22 UserDaoImpl userDao = new UserDaoImpl(); 23 try{ 24 userDao.login(user); 25 %> 26 用户登录成功!!<br构建之法 第一章 概论