JAVA日报

软件设计                  石家庄铁道大学信息学院

 

实验1:UML与面向对象程序设计原则

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、掌握面向对象程序设计中类与类之间的关系以及对应的UML类图;

2、理解面向对象程序设计原则

 

[实验任务一]:UML复习

阅读教材第一章复习UML,回答下述问题:

面向对象程序设计中类与类的关系都有哪几种?分别用类图实例说明。

继承

 

实现

 

依赖

 

关联

 

聚合

 

组合

 

[实验任务二]:单一职责原则

登录模块在实际项目开发中很常见,请按照教材28页利用单一职责原则重构后的类图实现这一模块。

 

实验要求:

1. 提交源代码和对应的数据库文件(注意将此模块保存,以备以后使用);

package src.DBUtil;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

 

public class DBUtil {

    private static String url = "jdbc:mysql://localhost:3306/text3?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";

    private static String user = "root";

    private static String password = "123";

    private static String jdbcName="com.mysql.cj.jdbc.Driver";

    private Connection con=null;

    public static  Connection getConnection() {

        Connection con=null;

        try {

            Class.forName(jdbcName);

            con=DriverManager.getConnection(url, user, password);

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        try {

            con = DriverManager.getConnection(url,user,password);

 

        } catch (SQLException e) {

            // TODO: handle exception

            e.printStackTrace();

        }

        return con;

    }

    

    public static void close(Connection con) {

        if(con!=null)

            try {

                con.close();

            } catch (SQLException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        

    }

    public static void close(Statement state, Connection conn) {

        if(state!=null) {

            try {

                state.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        if(conn!=null) {

            try {

                conn.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    

    public static void close(ResultSet rs, Statement state, Connection conn) {

        if(rs!=null) {

            try {

                rs.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        if(state!=null) {

            try {

                state.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        if(conn!=null) {

            try {

                conn.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

 

}

 

 

package src.dao;

import src.DBUtil.*;

import java.sql.*;

import java.util.ArrayList;

import src.bean.*;

public class UserDao {

final  String URL = "jdbc:mysql://localhost:3306/text3?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";

    final  String USER = "root";

   final  String PASSWORD = "123";

   final  String DRIVER="com.mysql.cj.jdbc.Drive";

public boolean addC(User t) throws ClassNotFoundException{

String sql = "insert into user(name,id,password,phonenumber) values('"+t.getName()+"','"+t.getId()+"','"+t.getPassword()+"','"+t.getPhonenumber()+"')";

Class.forName("com.mysql.cj.jdbc.Driver");

Statement stat=null;

Connection db=null;

int act=0;

try {

db = DriverManager.getConnection(URL,USER,PASSWORD);

} catch (SQLException e1) {

e1.printStackTrace();

}

try {

stat=db.createStatement();

act=stat.executeUpdate(sql);

}catch(Exception e) {

e.printStackTrace();

}finally {

DBUtil.close(stat,db);

}

if(act>0) {

return true;

}else {

return false;

}

}

public boolean delC(String t) throws ClassNotFoundException{

String sql="delete from user where id"+" ="+t;

Class.forName("com.mysql.cj.jdbc.Driver");

Statement stat=null;

Connection db=null;

int act=0;

try {

db = DriverManager.getConnection(URL,USER,PASSWORD);

} catch (SQLException e1) {

e1.printStackTrace();

}

try {

stat=db.createStatement();

act=stat.executeUpdate(sql);

}catch(Exception e) {

e.printStackTrace();

}finally {

DBUtil.close(stat,db);

}

if(act>0) {

return true;

}else {

return false;

}

}

public boolean gaiC(String id,String id1,String id3) throws ClassNotFoundException{

String sql="update user set "+id+"='"+id1+"' where id='"+id3+"'";

Class.forName("com.mysql.cj.jdbc.Driver");

Statement stat=null;

Connection db=null;

int act=0;

try {

db = DriverManager.getConnection(URL,USER,PASSWORD);

} catch (SQLException e1) {

e1.printStackTrace();

}

try {

stat=db.createStatement();

act=stat.executeUpdate(sql);

}catch(Exception e) {

e.printStackTrace();

}finally {

DBUtil.close(stat,db);

}

if(act>0) {

return true;

}else {

return false;

}

}

@SuppressWarnings("null")

public ArrayList<User> getAlluserinfos() throws ClassNotFoundException, SQLException{

ArrayList<User> users=new ArrayList<>();

PreparedStatement stat=null;

Connection db=null;

String sql2 = "select * from user";

Class.forName("com.mysql.cj.jdbc.Driver");

try {

db = DriverManager.getConnection(URL,USER,PASSWORD);

} catch (SQLException e1) {

e1.printStackTrace();

}

stat=db.prepareStatement(sql2);

ResultSet rs = stat.executeQuery();

 

while(rs.next()) {

User user=new User();

user.setId(rs.getString("id"));

user.setPassword(rs.getString("password"));

user.setName(rs.getString("name"));

user.setPhonenumber(rs.getString("phonenumber"));

users.add(user);

     }

return users;

 

}

}

 

2.注意编程规范。

 

 

posted @ 2021-10-20 19:33  我的未来姓栗山  阅读(33)  评论(0)    收藏  举报