JAVA实训航班管理系统加连接数据库(详细代码)

经过了几天的实训做出了比较粗鄙的实训项目,也是供大家参考,互相学习,也希望能够能指出代码不足之处

接下来话不多说直接代码走起,如下:

FlightDaoImpl类
对数据库的增删改查的功能方法

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 po.Flight;
import util.DBConnection;
public class FlightDaoImpl implements IFlightDao{
    public boolean findUser(String name,String passWord) {
        try {
            Connection conn = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            conn = DBConnection.getConnection();//获取与数据库连接对象
            String sql = "select * from user_info where user_name=? and user_pwd=?";//表示占位符
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, passWord);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                return true;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
    @Override
    public List<Flight> findAll() {
        // TODO Auto-generated method stub
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from Flight";
        ArrayList<Flight> list = new ArrayList<Flight>();
        try {
            conn = DBConnection.getConnection();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while(rs.next()){//查询得到的是多条数据,所以用while
                Flight flight = new Flight();//如果查找到,创建对象
                flight.setPnm(rs.getString(1));
                flight.setPnumber(rs.getString(2));
                flight.setDestin(rs.getString(3));
                flight.setStime(rs.getString(4));
                list.add(flight);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            DBConnection.closeAll(conn, pstmt, rs);
        }
        return list;
    }
    @Override
    public List<Flight> findByKey(String date) {
        // TODO Auto-generated method stub
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from Flight where stime=?";
        ArrayList<Flight> list = new ArrayList<Flight>();
        try {
            conn = DBConnection.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, date);
            rs = pstmt.executeQuery();
            while(rs.next()){//查询得到的是多条数据,所以用while
                Flight flight = new Flight();//如果查找到,创建对象
                flight.setPnm(rs.getString(1));
                flight.setPnumber(rs.getString(2));
                flight.setDestin(rs.getString(3));
                flight.setStime(rs.getString(4));
                list.add(flight);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            DBConnection.closeAll(conn, pstmt, rs);
        }
        return list;
    }
    public List<Flight> findByKeyl(String aq) {
        // TODO Auto-generated method stub
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from Flight where destin=?";
        ArrayList<Flight> list = new ArrayList<Flight>();
        try {
            conn = DBConnection.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, aq);
            rs = pstmt.executeQuery();
            while(rs.next()){//查询得到的是多条数据,所以用while
                Flight flight = new Flight();//如果查找到,创建对象
                flight.setPnm(rs.getString(1));
                flight.setPnumber(rs.getString(2));
                flight.setDestin(rs.getString(3));
                flight.setStime(rs.getString(4));
                list.add(flight);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            DBConnection.closeAll(conn, pstmt, rs);
        }
        return list;
    }
    @Override
    public int delete(String id) {
        // TODO Auto-generated method stub
        Connection conn = null;
        PreparedStatement pstmt = null;
        String sql = "delete from Flight where Pnm = ?";
        int result = 0;
        try {
            conn = DBConnection.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            result = pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            DBConnection.closeAll(conn, pstmt, null);
        }
        return result;
    }
    @Override
    public int update(Flight flight) {
        // TODO Auto-generated method stub
        Connection conn = null;
        PreparedStatement pstmt = null;
        String sql = "update Flight set Pnumber = ?  , destin = ? , stime = ? where Pnm=?";
        int result = 0;
        try {
            conn = DBConnection.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, flight.getPnumber());
            pstmt.setString(2, flight.getDestin());
            pstmt.setString(3, flight.getStime());
            pstmt.setString(4, flight.getStime());
            result = pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            DBConnection.closeAll(conn, pstmt, null);
        }
        return result;
    }
}

IFlightDao类
方法接口的封装

import java.util.List;
import po.Flight;
public interface IFlightDao {
    //登录
    public boolean findUser(String name,String passWord);
    //查询所有信息
    public List<Flight> findAll();
    //根据起飞时间查询
    public List<Flight> findByKey(String date);
    //根据起飞时间查询
    public List<Flight> findByKeyl(String aq);
    //删除航班,通常是根据主键删除
    public int delete(String id);
    //更新航班 ,主键做条件修改其他项
    public int update(Flight flight);

}

Flight类

对数据库要建立的数据进行封装,得到GET和SET方法

public class Flight {
    private String Pnm ;//编号
    private String  Pnumber;//航班号
    private String destin;//目的地
    private String stime;//起飞日期
    public Flight() {
        super();
    }
    public Flight(String Pnm, String Pnumber, String destin, String stime) {
        super();
        this.Pnm = Pnm;
        this.Pnumber = Pnumber;
        this.destin = destin;
        this.stime = stime;
    }
    public String getPnm() {
        return Pnm;
    }
    public String getPnumber() {
        return Pnumber;
    }
    public String getDestin() {
        return destin;
    }
    public String getStime() {
        return stime;
    }
    public void setPnm(String pnm) {
        Pnm = pnm;
    }
    public void setPnumber(String pnumber) {
        Pnumber = pnumber;
    }
    public void setDestin(String destin) {
        this.destin = destin;
    }
    public void setStime(String stime) {
        this.stime = stime;
    }
    @Override
    public String toString() {
        return "Flight [Pnm=" + Pnm + ", Pnumber=" + Pnumber + ", destin=" + destin
                + ", stime=" + stime + "]";
    }
}

DBConnection类
用于连接数据库,如果在这里出现了问题,请看连接数据库的博客文章,就在我的主页里面,就会得到解决,并且有新的体会。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConnection {
    public static Connection getConnection() {
        Connection connection = null;
        //链接sqlserver2014r2注册驱动程序字符串
        String jdbc_driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        //与数据库建立链接的字符串
        String jdbc_url = "jdbc:sqlserver://localhost:1433;DatabaseName=Flight";
        try {
            //将JDBC驱动类装载入Java虚拟机
            Class.forName(jdbc_driver);
            //使用户名、密码与数据库建立链接,sa用户名,sa密码
            connection = DriverManager.getConnection(jdbc_url,"sa","sa");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (SQLException ex) {
            // TODO: handle exception
            ex.printStackTrace();
        }
        return connection;
    }
    public static void closeAll(Connection conn,Statement stmt,ResultSet rs){
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

接下来就是GUI设计页面,是采用的Netbeans的GUI设计器进行设计的,最终在IDEA上运行

Login类
登陆界面

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Login extends JFrame {
    private JLabel labelName;
    private JLabel labelPwd;
    private JTextField textName;
    private JPasswordField passWord;
    private JButton buttonConfirm;
    private JButton buttonCancle;
    private JPanel jplPane;
    Login() {
        initGUI();
        showGUI();
    }
    void initGUI() {
        jplPane = new JPanel();
        jplPane.setBackground(Color.white);
        this.getContentPane().add(jplPane);
        jplPane.setLayout(null);  //设置JPanel面板为空布局
        JLabel labelName1 = new JLabel();
        labelName1.setFont(new Font("宋体", Font.PLAIN, 14));
        labelName1.setText("---欢迎使用航班管理系统---");
        labelName1.setBounds(70, 30, 500, 20);
        jplPane.add(labelName1);
        labelName = new JLabel();
        labelName.setFont(new Font("新宋体", Font.PLAIN, 14));
        labelName.setText("用户名");
        labelName.setBounds(65, 70, 50, 20);
        jplPane.add(labelName);
        textName = new JTextField();
        textName.setBounds(115, 70, 120, 20);
        jplPane.add(textName);
        labelPwd = new JLabel();
        labelPwd.setFont(new Font("新宋体", Font.PLAIN, 14));
        labelPwd.setText("密码");
        labelPwd.setBounds(65, 110, 50, 20);
        jplPane.add(labelPwd);
        passWord = new JPasswordField();
        passWord.setBounds(115, 110, 120, 20);
        jplPane.add(passWord);
        buttonConfirm = new JButton("登录");
        buttonConfirm.setBounds(85, 160, 60, 20);
        buttonConfirm.setBackground(Color.green);
        buttonCancle = new JButton("取消");
        buttonCancle.setBounds(165, 160, 60, 20);
        jplPane.add(buttonConfirm);
        jplPane.add(buttonCancle);
        //注册监听器
        buttonConfirm.addActionListener(new ConfirmActionClass()); //new ConfirmActionClass()是监听器对象
    }
//定义内部监听类
    class ConfirmActionClass implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String name = textName.getText(); //获取姓名
            String pass = new String(passWord.getPassword());//获取密码,与下行代码功能相同
            // String pass = String.copyValueOf(passWord.getPassword());
            if (name.equals("admin") && pass.equals("123456")) {//判断姓名和密码是否正确
                FlightJFrame2 flightJFrame2=new FlightJFrame2();
                flightJFrame2.setExtendedState(JFrame.MAXIMIZED_HORIZ);
                flightJFrame2.setVisible(true);
            } else {
                JOptionPane.showMessageDialog(null, " 您输入的用户名或密码错误,请重新输入", "错误提示  ", JOptionPane.QUESTION_MESSAGE);
            }
        }
    }
    void showGUI() {
        this.setVisible(true);
        this.setSize(400, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        Login login = new Login();
        login.setSize(350, 250);
    }
}

接下来的代码有点多,估计看的时候也会不耐烦,想要的可以私聊我,全部免费,只为互相学习。就给大家上几个图片吧!看一下效果

主页面也是参照了 别人(灵魂漫步)的界面【现在好像没有了】 只不过它使用JAVAWEB 写出来的
这个便是纯JAVA编写,供大家参考,但其他界面均原创。

登录界面
在这里插入图片描述
主页面
在这里插入图片描述
想想还是不要再右边显示,虽然好看,但是对于初学最好是多页面,有助于理解事件处理功能,更加利于操作。

按日期查询
在这里插入图片描述
按目的地查询
在这里插入图片描述
删除航班
在这里插入图片描述
更新航班内容
在这里插入图片描述
希望大家通过交流更加精进自己的能力水平,谢谢大家观看,想要的私聊,直接发给大家源码

  • 15
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 20
    评论
OOP(机试)[具体要求在压缩文档中给出] 项目名称: Air Infomation System 基于控制台的航班信息系统,简称AIS 具体要求如下: (1)显示航班信息系统主菜单,如图-1所示,包括: 1)列出所有航班 2)按起飞时间查询 3)按目的地查询 4)删除航班 5)更新航班 6)退出系统 (2)列出所有航班:查出所有航班的信息,以列表形式显示,包括:编号,航班号,目的地,起飞日期。 (3)按起飞时间查询:输入起飞时间(格式如2011-2-25),查出所有这一天的航班。 (4)按目的地查询:输入目的地,查出所有飞往此地的航班。 (5)删除航班:删除指定编号的航班。 (6)更新航班:更新指定编号的航班。 (7)退出系统。 三、类的设计 需要定义如下类 航班信息实体类(AirInfo) 航班编号(id) 航班号(flight_number) 目的地(destination) 起飞日期(flight_date) 航班信息管理类AirInfoManager类 程序入口类TestAirInfo类 四、具体要求及推荐实现步骤 6.创建实体类AirInfo,属性私有化,根据业务提供需要的构造方法和setter/getter方法。 7.创建航班管理AirInfoManager类,在类中提供列出所有航班的方法,按起飞时间查询 的方法、按目的地查询的方法、删除航班的方法、更新航班的方法、退出系统的方法。 8.创建TestAirInfo类,启动和运行系统。 9.航班的信息用ArrayList(或数组)保存。 10.要求代码规范,命名正确。
# OOP(机试) 本程序总结文章:http://blog.qiji.tech/?p=10344 - - - ## 程序基本要求 一、项目名称: Air Infomation Programming 基于控制台的航班信息程序,简称AIP 二、具体要求如下: (1)显示航班信息程序主菜单,如图-1所示,包括: * 1)列出所有航班 * 2)按起飞时间查询 * 3)按目的地查询 * 4)删除航班 * 5)更新航班 * 6)退出系统 (2)列出所有航班:查出所有航班的信息,以列表形式显示,包括:编号,航班号,目的地,起飞日期。 (3)按起飞时间查询:输入起飞时间(格式如2011-2-25),查出所有这一天的航班。 (4)按目的地查询:输入目的地,查出所有飞往此地的航班。 (5)删除航班:删除指定编号的航班。 (6)更新航班:更新指定编号的航班。 (7)退出系统。 三、类的设计 需要定义如下类 * 航班信息实体类(AirInfo) * 航班编号(id) * 航班号(flight_number) * 目的地(destination) * 起飞日期(flight_date) * 航班信息管理类AirInfoManager类 * 程序入口类TestAirInfo类 四、具体要求及推荐实现步骤 1. 创建实体类AirInfo,属性私有化,根据业务提供需要的构造方法和setter/getter方法。 1. 创建航班管理AirInfoManager类,在类中提供列出所有航班的方法,按起飞时间查询的方法、按目的地查询的方法、删除航班的方法、更新航班的方法、退出程序的方法。 2. 创建TestAirInfo类,启动和运行程序。 3. 航班的信息用ArrayList(或数组)保存。 4. 要求代码规范,命名正确。 - - -
package com.hexiang.utils.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import org.apache.log4j.Logger; public class DBConnection { /** * 获得与数据库的连接 * * @param path * @return Connection */ public static Connection getConn(String classDriver, String url, String user, String pwd) { try { Class.forName(classDriver); return DriverManager.getConnection(url, user, pwd); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(DataSource dataSource) { try { return dataSource.getConnection(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(String jndiName) { try { Context ctx; ctx = new InitialContext(); DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/" + jndiName); return dataSource.getConnection(); } catch (NamingException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(Properties properties) { try { String driver = properties.getProperty("jdbc.driverClassName"); String url = properties.getProperty("jdbc.url"); String user = properties.getProperty("jdbc.username"); String password = properties.getProperty("jdbc.password"); Class.forName(driver); return DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } /** * oracle连接 * * @param path * @return Connection */ public static Connection getOracleConn(String

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值