软件工程概论第一堂课,网站系统的开发

1.网站系统开发的技术以及方法

实现java web的项目的开发,需要如下六个技术:Java 面向对象分析设计思想 设计模式以及框架结构 XML语言 网页脚本语言 数据库 应用服务器 集成开发环境

在Java中具体需要熟练掌握:JSP、Servlet、JDBC、JavaBean(Application)

(1)JDBC(Java Database Connectivity) 是一种用于执行 SQL 语句的 Java API使用户能够用Java API 来进行数据库的编写程序

(2)Servlet是运行在服务端的技术,可以认为时服务器端的applent

(3)(JSP) 技术:JSP是从Servlet上分离出来的一小部分,简化了开发,加强了界面设计。JSP定位在交互网页的开发。运用Java语法,但功能较Servlet弱了很多,并且高级开发中只充当用户界面部分。JSP容器收到客户端发出的请求时,首先执行其中的程序片段,然后将执行结果以HTML格式响应给客户端。其中程序片段可以是:操作数据库、重新定向网页以及发送 E-Mail 等等,这些都是建立动态网站所需要的功能。所有程序操作都在服务器端执行,网络上传送给客户端的仅是得到的结果,与客户端的浏览器无关,因此,JSP 称为Server-Side Language

(4)Application是Java应用程序,在WEB项目和一些开发中主要应用JavaBean。它就是Application的一部分,逻辑运算能力很强,能极大的发挥Java语言的优点。JavaBean 被称为是Java 组件技术的核心。JavaBean 的结构必须满足一定的命名约定。JavaBean能提供常用功能并且可以重复使用,这使得开发人员可以把某些关键功能和核心算法提取出来封装成为一个组件对象,这样就增加了代码的重用率和系统的安全性

面向对象分析设计思想

  在进行项目分析之时,利用UML例图,进行设计会对项目有很大的帮助,有助于快速地找到项目服务的对像

设计模式以及框架结构在普通的WEB项目中很多采用两层的开发结构。JSP+Servlet或JSP+JavaBean。当对开发要求高的项目中使用很多的还是MVC的三层开发结构,也就是JSP+Servlet+JavaBean。它能分有效的分离逻辑开发,使开发人员能专注于各自的开发。同时也能时整个开发结构流程更清晰,但是需要比较高的开发配合度。 
在项目中,我们经常使用著名的Model-View-Controller(MVC)架构。MVC架构是随着smalltalk language语言的发展提出的,它是一个著名的用户界面设计架构。经典的MVC架构把一个组件(可认为是整个应用程序的一个模块)划分成三部分组 Model管理这个模块中所用到的数据和业务逻辑。而View 管理模块如何显示给用户,Controller 决定如何处理用户和该模块交互式时候产生的事件 如用户点击一个按钮等

XML语言

(1)、简单数据的表示和交换(针对XML的简单API(SAX)和文档对象模型(DOM)语法解析,不同的文档类型定义(DTDs)和概要(schemas))
(2)、用户界面相关、表示相关的上下文(可扩展样式表语言(XSL),可扩展样式表语言转换(XSLT))
(3)、面向消息的计算(XML-RPC(远程过程调用),基于SOAP协议的Web 服务(Web Services),电子化业务XML(ebXML))
网页脚本语言

  网页脚本语言的执行都是在客户端执行的,速度很很快,并且大多的操作与服务器没有交互运算,所以在一些应用中非常理想

开发工具有

数据服务器,web服务器,集成开发环境

2课堂测试源代码:

所有的类包:

IUserDao.java

package com.jaovo.msg.dao;

import java.util.List;

import com.jaovo.msg.mobeI.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();
}

UserDaoImpI

package com.jaovo.msg.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import java.sql.Connection;
import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.Util.UserException;
import com.jaovo.msg.mobeI.User;


public class UserdaoImpI implements IUserDao{

    @Override
    public void add(User user) {
        // TODO Auto-generated method stub
        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,nickname) values (?,?,?)";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, user.getUsername());
        preparedStatement.setString(2, user.getPasswork());
        preparedStatement.setString(3, user.getNickname());
        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) {
        // TODO Auto-generated method stub
        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) {
        // TODO Auto-generated method stub
        Connection connection=DBUtil.getConnection();
        String sql = "update t_user set password = ?, nickname = ? where id = ?";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getPasswork());
            preparedStatement.setString(2, user.getNickname());
            preparedStatement.setInt(3, user.getId());
            preparedStatement.executeQuery();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
    }

    @Override
    public User load(int id) {
        // TODO Auto-generated method stub
        Connection connection=DBUtil.getConnection();
        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.setPasswork(resultSet.getString("passwork"));
                user.setNickname(resultSet.getString("nickname"));
            }
        } 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
        Connection connection=DBUtil.getConnection();
        String sql = "select * from t_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.setId(resultSet.getInt("id"));
                user.setPasswork(resultSet.getString("passwork"));
                user.setNickname(resultSet.getString("nickname"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        return  user;
    }

    @Override
    public List<User> load() {
        // TODO Auto-generated method stub
        Connection connection=DBUtil.getConnection();
        String sql = "select * from t_user";    
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        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.setPasswork(resultSet.getString("passwork"));
                user.setNickname(resultSet.getString("nickname"));
                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.java

package com.jaovo.msg.mobeI;

public class User {
    private int id;
    private String username;
    private String nickname;
    private String passwork;
    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 getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public String getPasswork() {
        return passwork;
    }
    public void setPasswork(String passwork) {
        this.passwork = passwork;
    }
    
}

DBU til.java

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;

import org.apache.tomcat.dbcp.dbcp2.DriverManagerConnectionFactory;

public class DBUtil {
    public static Connection getConnection() {
        //1.加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String user = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/jaovo_msg";
        Connection connection = null;
        try {
            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.java

package com.jaovo.msg.Util;

public class UserException extends RuntimeException{

    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文件有:
add.jsp

package com.jaovo.msg.Util;

public class UserException extends RuntimeException{

    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
    }

}

addIput.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>
    <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>
                    <td>用户昵称:</td>
                    <td>
                        <input type = "text" name = "nickname"     />
                    </td>
                </tr>
                <tr align "center">
                    <td colspan = "2">
                        <input type = "submit" value = "提交"/>
                        <input type = "reset"  value = "重置"/>
                        </td>
        </table>
    </form>
</body>
</html>

实验执行结果截图:

自己的目标:

  再经过前九周的Java的课程的学习过程中,自己有所松懈,并没有尽到自己的全力,没有付出便没有收获

  自己希望在软件工程概论的接下来的学习中,自己付出的更多一点,争取在自己专业学习上更进一步,在期末的时候,能够与自己团队的成员做出一款自己所满意的成果 

posted @ 2017-11-22 17:11  tomhaha  阅读(209)  评论(0编辑  收藏  举报