java大作业回顾

来源:互联网 发布:网络惊魂 电影 2016 编辑:程序博客网 时间:2024/05/29 06:27

Java大作业回顾——公交线路管理系统

学了半学期的java。(因为之前学过c++所以感觉java比c++好上手多了。)写作业期间,也是查了不少资料,为了方便像我这样的新手理解,分享一下当时自己在写时的错误,希望大家少走弯路。也希望有大神给出指点。使用工具:eclipse,sql2012

概要设计

刚拿到这个题目时,既然是线路管理。便分了两种模式:管理员游客

管理员拥有所有权限,而游客只拥有访问和查询的权限

数据库设计

因为只学过sql所以一切就按照最基本的方式。

用户信息表(adm_user) 属性有: id,password,status

车辆表(BUS) 属性有: WNO(线路名),BNO,BSIZE,BMONEY,OTHER

车站表(Station) 属性有: SNO,SNAME,SOTHER

线路表(WBS) 属性有: WNO,BNO,SNO,no

备注:斜体加粗为主键

E-R 图:

Created with Raphaël 2.1.0车站车站多对多线路表(WNO,BNO,SNO,no)多对多

界面设计

将所以界面语句统一放在了一个包内,我就拿管理的主界面举个例子

Bus_Management_View_admin

两个JPanel,一个放在西边做功能栏,一个放右边做展示。
按钮的监听和鼠标事件的产生(鼠标事件本来想做成一个可以凭靠点击完成线路的功能,但时间有限,没能完成)

public class Bus_Management_View_admin extends JFrame implements ActionListener,MouseListener    {        //控件定义        JPanel MV_jp1,MV_jp2;        JButton MV_jb1,MV_jb2,MV_jb3,MV_jb4,MV_jb5,MV_jb6,MV_jb7,MV_jb8,MV_jb9,MV_jb10;        JLabel MV_jbl1,MV_jbl2,MV_jbl3,MV_jbl4,MV_jbl5,MV_jbl6,MV_jbl7,MV_jbl8;        JTextArea jta ;        Bus_sql_operating bb = new Bus_sql_operating();        //主方法        public static void main(String[] args)         {            Bus_Management_View_admin sda = new Bus_Management_View_admin();        }        //构造方法        public Bus_Management_View_admin()         {            MV_jp1 = new JPanel();            MV_jb1 = new JButton("新建站点");            MV_jb1.addActionListener(this);            MV_jb1.setFont(new Font("仿宋",Font.PLAIN,30));            MV_jb2 = new JButton("新建车辆");            MV_jb2.addActionListener(this);            MV_jb2.setFont(new Font("仿宋",Font.PLAIN,30));            MV_jb3 = new JButton("新建线路");            MV_jb3.addActionListener(this);            MV_jb3.setFont(new Font("仿宋",Font.PLAIN,30));            MV_jb4 = new JButton("查询站点");            MV_jb4.addActionListener(this);            MV_jb4.setFont(new Font("仿宋",Font.PLAIN,30));            MV_jb5 = new JButton("查询车辆");            MV_jb5.addActionListener(this);            MV_jb5.setFont(new Font("仿宋",Font.PLAIN,30));            MV_jb6 = new JButton("查询路线");            MV_jb6.addActionListener(this);            MV_jb6.setFont(new Font("仿宋",Font.PLAIN,30));            MV_jb7 = new JButton("删除路线");            MV_jb7.addActionListener(this);            MV_jb7.setFont(new Font("仿宋",Font.PLAIN,30));            MV_jb8 = new JButton("删除站点");            MV_jb8.addActionListener(this);            MV_jb8.setFont(new Font("仿宋",Font.PLAIN,30));            MV_jb9 = new JButton("删除车辆");            MV_jb9.addActionListener(this);            MV_jb9.setFont(new Font("仿宋",Font.PLAIN,30));            //功能栏依次添加按钮            //布局10行1列            MV_jp1.add(MV_jb1);            MV_jp1.add(MV_jb2);            MV_jp1.add(MV_jb3);            MV_jp1.add(MV_jb4);            MV_jp1.add(MV_jb5);            MV_jp1.add(MV_jb6);            MV_jp1.add(MV_jb8);            MV_jp1.add(MV_jb9);            MV_jp1.add(MV_jb7);            MV_jbl3 = new JLabel(new ImageIcon("image/way.PNG"));            //照片的添加            MV_jbl3.addMouseListener(this);            this.add(MV_jp1,"West");            this.add(MV_jbl3,"Center");            this.setTitle("公交线路管理系统(管理员)");            this.setSize(884,720);            this.setLocation(550,170);            this.setVisible(true);            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        }        @Override        public void actionPerformed(ActionEvent e)         {            if(e.getSource() == MV_jb1)            {                try {                    Bus_new_station  a = new Bus_new_station();                } catch (ClassNotFoundException | SQLException e1) {                    // TODO Auto-generated catch block                    e1.printStackTrace();                }            }            if(e.getSource() == MV_jb2)            {                Bus_new_bus b = new Bus_new_bus();            }            if(e.getSource() == MV_jb3)            {                try {                    Bus_new_way c = new Bus_new_way();                } catch (ClassNotFoundException|SQLException e1) {                    // TODO Auto-generated catch block                    JOptionPane.showMessageDialog(this,"系统出现BUG");                    e1.printStackTrace();                }             }            if(e.getSource() == MV_jb4)            {                String db = "";                try {                    bb.selecet(1,db);                    JOptionPane.showMessageDialog(this,"操作成功");                } catch (ClassNotFoundException | SQLException e1) {                    JOptionPane.showMessageDialog(this,"系统出现BUG");                    e1.printStackTrace();                }            }            if(e.getSource() == MV_jb5)            {                //String db = jta.getText().trim();                String db = "";                try {                    bb.selecet(2,db);                    JOptionPane.showMessageDialog(this,"操作成功");                } catch (ClassNotFoundException | SQLException e1) {                    JOptionPane.showMessageDialog(this,"系统出现BUG");                     e1.printStackTrace();                }            }            if(e.getSource() == MV_jb6)            {                way_kinds_select dsd = new way_kinds_select();            }            if(e.getSource() == MV_jb7)            {                Bus_delete_way sd= new Bus_delete_way();            }            if(e.getSource() == MV_jb8)            {                Bus_delete_station ddd = new Bus_delete_station();            }            if(e.getSource() == MV_jb9)            {                Bus_delete_bus  sads = new Bus_delete_bus();            }        }        @Override        public void mouseClicked(MouseEvent e) {            // TODO Auto-generated method stub             if(e.getButton()==e.BUTTON1)                {                 int x =e.getX()/22;                  int y =e.getY()/22;                   String str = "当前位置为 x = "+x+",y = "+y;                 Bus_sql_operating dss = new Bus_sql_operating();                 try {                    String asd = dss.getstation(x,y);                    JOptionPane.showMessageDialog(this,"此为"+asd);                } catch (ClassNotFoundException | SQLException e1) {                    // TODO Auto-generated catch block                    e1.printStackTrace();                }               //  System.out.println(str);           }        }

我写的很简单,全都是默认控件,没有自己写控件出来。还是容易看懂的。

Java中sql语句的基本用法

连接数据库的基本语句

     // 定义数据库访问参数      String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Text_JAVA_BUS";      String user = "cytus";  //数据库用户名(自填项)    String password = "123";  //数据库密码(自填项)    String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";     //1.添加驱动。    Class.forName(driverName);     //2.获得连接对象    ConnectiondbConn=DriverManager.getConnection(url,user,password);    //3.获得Statement(通过其来完成将sql语句传入并执行)    Statement st = ConnectiondbConn.createStatement();    //4.得到结果集(sql_text为你的sql语句)    ResultSet rs = st.executeQuery(sql_text);    //4.如果不想得到结果集(sql_text为你的sql语句)    st.executeUpdate(sql_text);    //5.得到的结果集进行操作:不断读取rs的下一条数据,直至为空。    while(rs.next())    {        //分别获得rs中SNO,SNAME,SOTHER列的内容        String no=rs.getString("SNO");          String size=rs.getString("SNAME");        String money=rs.getString("SOTHER");    }    //6.关闭各种通道    st.close();    getConnection().close();

对sql的操作就是如上的内容。

关于各种信息的处理

在使用我们的系统时,我们会将要处理的各种数据信息进行统一的类包装。这样做:方便与我们管理和实现。像用户信息的处理:

public class User {    private String User_Name;    private String User_Password;    private String User_Status;    public String getUser_Name() {        return User_Name;    }    public void setUser_Name(String user_Name) {        User_Name = user_Name;    }    public String getUser_Password() {        return User_Password;    }    public void setUser_Password(String user_Password) {        User_Password = user_Password;    }    public String getUser_Status() {        return User_Status;    }    public void setUser_Status(String user_status)    {        User_Status = user_status;    }}

这样一来,在视图页面上只需实例化各个类,使用类方法即可。

以上,便是我此次设计这个系统的基本方式。

第一次写博客,写的不好,请见谅。

原创粉丝点击