动手动脑——登录界面
1. 关于网站系统开发的技术:
(1)技术内容:
HTML语法、CSS语法、JavaScript语法
(2)后台编程:
数据库:SQLServer设计、MySQL设计、Access设计、Oracle设计
另外,Java语言(Java database Connectivity(JjavawebDbc)技术、Servlet技术、jsp(Java Server Pages)技术,JavaBean(Application)应用组件技术)、面向对象分析设计思想、设计模式和框架结构、XML语言、网页脚本语言、开发工具(数据库、web服务器、集成开发环境(IDE))。
2. 源程序代码:
(1)创建一个接口
package com.jaovo.msg.dao; import java.util.List; import com.jaovo.msg.model.User; public interface IUserDao { public void add(User user); public void delete(int id); public void update(User user); public User load(int id); public User load(String username); public List<User> load(); }
(2)创建一个类实现IUserDao接口
package com.jaovo.msg.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.jaovo.msg.Util.DBUtil; import com.jaovo.msg.Util.UserException; import com.jaovo.msg.model.User; import sun.net.www.content.text.plain; public class UserDaoImpl implements IUserDao { @Override public void add(User user) { //获得链接对象 Connection connection = DBUtil.getConnection(); //准备sql语句 String sql = "select count(*) from t_user where username = ?"; //创建语句传输对象 PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getUsername()); //接收结果集 resultSet = preparedStatement.executeQuery(); //遍历结果集 while(resultSet.next()) { if (resultSet.getInt(1) > 0) { throw new UserException("用户已存在") ; } } sql = "insert into t_user(username,password) value (?,?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getUsername()); preparedStatement.setString(2, user.getPassword()); preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //关闭资源 DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } } @Override public void delete(int id) { Connection connection = DBUtil.getConnection(); String sql = "delete from t_user where id = ?"; PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1, id); preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { DBUtil.close(preparedStatement); DBUtil.close(connection); } } @Override public void update(User user) { Connection connection = DBUtil.getConnection(); //准备sql语句 String sql = "update t_user set password = ? , where id = ?"; //创建语句传输对象 PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getPassword()); preparedStatement.setInt(2, user.getId()); preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { DBUtil.close(preparedStatement); DBUtil.close(connection); } } @Override public User load(int id) { Connection connection = DBUtil.getConnection(); //准备sql语句 String sql = "select * from t_user where id = ?"; //创建语句传输对象 PreparedStatement preparedStatement = null; ResultSet resultSet = null; User user = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1, id); resultSet = preparedStatement.executeQuery(); while(resultSet.next()) { user = new User(); user.setId(id); user.setUsername(resultSet.getString("username")); user.setPassword(resultSet.getString("password")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } return user; } @Override public User load(String username) { // TODO Auto-generated method stub return null; } @Override public List<User> load() { Connection connection = DBUtil.getConnection(); //准备sql语句 String sql = "select * from t_user "; //创建语句传输对象 PreparedStatement preparedStatement = null; ResultSet resultSet = null; //集合中只能放入user对象 List<User> users = new ArrayList<User>(); User user = null; try { preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while(resultSet.next()) { user = new User(); user.setId(resultSet.getInt("id")); user.setUsername(resultSet.getString("username")); user.setPassword(resultSet.getString("password")); users.add(user); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } return users; } }
(3)创建一个com.jaovo.msg.model包,写一个User类
package com.jaovo.msg.model; public class User{ private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
(4)创建一个com.jaovo.msg.Util包:
添加DBUtil类:
package com.jaovo.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 { //1 加载驱动 Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String user = "root"; String password = "123456"; String url = "jdbc:mysql://localhost:3306/jaovo_msg"; Connection connection = null; try { //2 创建链接对象connection connection = DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return connection; } //关闭资源的方法 public static void close(Connection connection ) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 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(); } } }
添加UserException类
package com.jaovo.msg.Util; public class UserException extends RuntimeException{ public UserException() { super(); // TODO Auto-generated constructor stub } public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); // TODO Auto-generated constructor stub } public UserException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } public UserException(String message) { super(message); // TODO Auto-generated constructor stub } public UserException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } }
(5)创建一个SQL文件夹,放入init.sql
-- mysal -u root -p root; -- show databases; -- drop database jaovo_shop; create database jaovo_msg; use jaovo_msg; GRANT ALL ON jaovo_msg.* to "jaovo"@"localhost" IDENTIFIED BY "root"; create table t_user( id int(10) primary key auto_increment, username varchar(255), password varchar(255), nickname varchar(255), type int(2), status int(2) ); create table t_message( id int(10) primary key auto_increment, title varchar(254), content text, post_date datetime, user_id int(10), CONSTRAINT FOREIGN KEY(user_id) REFERENCES t_user(id) ); create table t_comment( id int(10) primary key auto_increment, content text, post_date datetime, user_id int(10), msg_id int(10), CONSTRAINT FOREIGN KEY(user_id) REFERENCES t_user(id), CONSTRAINT FOREIGN KEY(msg_id) REFERENCES t_message(id) );
(6)在WebContent---->META-INF---->创建user文件夹----->分别创建三个jsp文件
add.jsp:
<%@page import="com.jaovo.msg.Util.UserException"%> <%@page import="com.jaovo.msg.dao.UserDaoImpl"%> <%@page import="com.jaovo.msg.model.User"%> <%@ 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> <% //接收客户端传递过来的参数 String username = request.getParameter("username"); String password = request.getParameter("password"); if(username == null || "".equals(username.trim())){ request.setAttribute("error", "用户名不能为空"); %> <jsp:forward page="addInput.jsp"></jsp:forward> <% } User user = new User(); user.setUsername(username); user.setPassword(password); UserDaoImpl userDao = new UserDaoImpl(); try{ userDao.add(user); //重定向 response.sendRedirect("list.jsp"); %> <% }catch(UserException e){ %> <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2> <% } %> </html>
addInput.jsp:
<%@ 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>
<title>用户添加页面</title>
</head>
<body>
<%=request.getAttribute("error") %>
<form action="add.jsp" method="get">
<table align="center" border="1" width="500">
<tr>
<td>用户名称 : </td>
<td>
<input type="text" name="username" />
</td>
</tr>
<tr>
<td>用户密码:</td>
<td>
<input type="password" name="password" />
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</body>
</html>
list.jsp:
<%@page import="com.jaovo.msg.model.User"%> <%@page import="java.util.List"%> <%@page import="com.jaovo.msg.dao.UserDaoImpl"%> <%@ 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"> <title>用户展示界面</title> </head> <% UserDaoImpl userDao = new UserDaoImpl(); List<User> users = userDao.load(); %> <body> <table align="center" border="1" width="500"> <tr> <td>用户编号</td> <td>用户名称</td> <td>用户密码</td> </tr> <% for( User user : users ){ %> <tr> <td> <%=user.getId() %></td> <td> <%=user.getUsername() %></td> <td> <%=user.getPassword() %></td> <td> <a href="delete.jsp ? id="<%=user.getId() %>" >删除</a></td> </tr> <% } %> </table> </body> </html>
(7)其中,在WebContent---->WEB-INF---->lib文件夹中加载两个包:
jstl-1.2.jar
mysql-connector-java-5.1.38-bin.jar
3.运行结果截图:
创建后显示当前用户列表
若输入同名用户,则会显示已存在。不可创建
4.关于软件工程概论课程的希望、目标以及未来规划:
(1)希望及目标:
通过本课程了解软件工程的历史背景、现目前发展以及未来职业方向,培养学习软件工程专业的兴趣。
(2)未来规划:
通过专业学习,提升专业技能,立志成为一个不拖后腿的程序媛。