代码改变世界

软件工程概论课堂测试01

2017-11-22 20:27  默默不语  阅读(273)  评论(0编辑  收藏  举报

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

HTML语言可控制网页的内容。

⑵层叠样式表CSS控制网页的样式,比如不同的颜色,布局,排版。

⑶MySQL:一个网站会有很多页面,一个页面需要做一张html,当有很多张时就会需要维护在MySQL中。

⑷JDBC:一种用于执行SQL语句的Java API。当有很多张表时,如何操作这些数据呢?这是需要通过JDBC驱动与数据库建立连接、发送 操作数据库的语句并处理结果。

⑸JAVA语言基础:为了满足JDBC的运行,我们需要用到java语言。将Java语言和JDBC结合起来使程序员不必为不同的平台编写不同的应用程序,只须写一遍程序就可以让它在任何平台上运行。

⑹JSP:是一种动态页面技术。想要将通过JDBC操作的数据显示在HTML上,这时就需要用到JSP。

⑺Tomcat:Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,Tomcat服务器具有处理HTML页面的功能。另外它还是一个Servlet和JSP容器,可以保证JSP的运行。

⑻js(JavaScript):是目前所有主流浏览器上唯一支持的脚本语言,它能够让我们的网页具有动态效果。

⑼PS(PhotoShop):图片处理技术,网站制作过程中经常需要对图片进行处理,这时我们需要用到ps图片处理技术让我们想要的图片更适合我们的网站。

……

网站系统开发需要掌握的技术还有很多,以上只是比较基础的一些,还有PHP、.net、asp、Bootstrap等等,以后还需要更深入的学习。^_^

 

2.程序源代码

定义接口,用以声明对用户的增删改查功能

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();
    
}

实现对用户的增删改查功能

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 {
    
    
    //添加用户
    public void add(User user) {
        //获得链接对象
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "select count(*) from l_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 l_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);
        }
        
    }

    //删除用户
    public void delete(int id) {
        Connection connection = DBUtil.getConnection();
        String sql = "delete from l_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);
        }
        
    }

    //修改用户信息
    public void update(User user) {
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "update l_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);
        }
    }

    //以编号查找用户
    public User load(int id) {
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "select * from l_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;
    }
    
    
    //以用户姓名查找用户信息
    public User load(String username) {
        // TODO Auto-generated method stub
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "select * from l_user  where username = ?";
        //创建语句传输对象
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                user = new User();
                user.setUsername(username);
                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;
    }

    //显示所有用户信息
    public List<User> load() {
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "select * from l_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;
    }
}

定义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) {
        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;
    }
    
    
    
    
}

定义工具类:实现连接数据库与关闭

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 = "chen123";
        String url = "jdbc:mysql://localhost:3306/jaovo_Login";
        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();
            }
        }
        
}

定义异常类

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
    }
    
}

设计登录界面(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="login.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="登录" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

登录界面的输入(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="loginInput.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>

显示所有用户的信息表

<%@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>

操作数据库

-- mysal -u root -p root;
-- show databases;
-- drop database jaovo_shop;

create database jaovo_Login;
use jaovo_Login;
GRANT ALL ON jaovo_Login.* to "jaovo"@"localhost" IDENTIFIED BY "root";
create table l_user(
     id int(10) primary key auto_increment,
     username varchar(255),
     password varchar(255),
     type int(2),
     status int(2)
);

create table l_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 l_user(id)
);

create table l_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 l_user(id),
   CONSTRAINT FOREIGN KEY(msg_id) REFERENCES l_message(id)
);

 

3.运行结果截图

 

 

 

 4.难点

对于界面的操作与对数据库的操作(重点)。

5.目标

希望能讲的详细一点,以便打好基础。希望学完这门课,自己的编程能力有一个提升,以及提高团队之间的默契度。我预计一个星期大概会有一天半的空余时间去练习。