JDBC实际应用案例

JDBC实际应用案例,第1张

 

目录

任务:

代码部分:

主体块:

Airinfo类:

AirinfoDao类:

Druid连接类(固定代码):

 db.properties(Druid连接参数):

AirinfoDaoImpl实现类:


任务:

做一个航班信息系统,用户 可以根据需求去对航班信息进行 *** 作,通过 java 代码 *** 作数据库,并且用户是可以在控制台做对 应的 *** 作

  • 1、创建数据库表 airinfo,添加测试数据不少于 4 条。要求主键自增
  • 2、创建实体类 AirInfo,根据业务提供需要的构造方法和 setter/getter
  • 方法。
  • 3、创建 BaseDao 类,实现数据库连接和关闭功能。
  • 4、创建 DAO 接口 AirInfoDao,定义查询所有航班,按日期和目的地查
  • 询航班,删除航班,更新航班的方法。
  • 5、创建 DAO 实现类 AirInfoDaoImpl,继承 BaseDao 类,实现 AirInfoDao
  • 接口,使用 JDBC 完成相应数据库 *** 作。
  • 6、创建 Main 类,完成在控制台显示留言信息和用户添加留言 *** 作,启动
代码部分: 主体块:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Scanner;

public class Demo1{
    public static void main(String[] args) throws ParseException {
        int num = 0;
        AirinfoDaoImpl infoDao = new AirinfoDaoImpl();
        do{
            //1.输出菜单
            System.out.println("-------航班管理系统-------");
            System.out.println("1. 查询所有航班信息 2. 根据航班时间进行查询 3. 根据目的地进行查询 4. 删除航班(根据航班编号) 5. 更新航班(根据航班编号) 6. 查询航班是否存在(根据航班编号) 7. 退出系统");
            System.out.println("请选择:");
            Scanner scanner = new Scanner(System.in);
            num = scanner.nextInt();
            switch(num){
                case 1:
                    List infos = infoDao.findAll();
                    System.out.println("编号\t航班号\t目的地\t起飞时间");
                    for(int i=0;i0?"更新成功,已更新"+update+"条数据":"输入的航班编号有误!");
                    }
                    break;
                case 6:
                    System.out.println("请输入航班号:");
                    String number = scanner.next();
                    List airnumber = infoDao.findByNumber(number);
                    System.out.println("编号\t航班号\t目的地\t起飞时间");
                    for(int i=0;i
Airinfo类:
import java.util.Date;

public class Airinfo {
    private int airid;
    private String number;
    private String address;
    private Date begintime;

    public int getAirid() {
        return airid;
    }

    public void setAirid(int airid) {
        this.airid = airid;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getBegintime() {
        return begintime;
    }

    public void setBegintime(Date begintime) {
        this.begintime = begintime;
    }
}
AirinfoDao类:
import java.util.List;

public interface AirinfoDao {
    //1. 查询所有航班信息
    public List findAll();
    //2. 根据航班时间进行查询
    public List findByTime(String time);
    //3. 根据目的地进行查询
    public List findByAddress(String address);
    //4. 删除航班(根据航班编号)
    public int delete(String number);
    //5. 更新航班(根据航班编号)
    public int update(Airinfo airinfo,String time);
    //6. 查询航班是否存在(根据航班编号)
    public List findByNumber(String number);
}
Druid连接类(固定代码):
import com.alibaba.druid.pool.DruidDataSource;

import java.sql.*;
import java.sql.ResultSet;
import java.util.List;
import java.util.ResourceBundle;

public class DbUtils {
    //1.定义需要的工具类对象
    private Connection connection=null;
    private PreparedStatement pps=null;
    protected ResultSet resultSet=null;
    private int count=0;//存储受影响的行数
    private static String url;
    private static String username;
    private static String password;
    private static String driverName;
    private static DruidDataSource datasource = new DruidDataSource();
    //2.工具类中写入属性文件
    static{
        //参数只写属性文件名即可,不需要写后缀
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        driverName = bundle.getString("driver");
        url = bundle.getString("url");
        username = bundle.getString("user");
        password = bundle.getString("password");
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setUrl(url);
        datasource.setDriverClassName(driverName);
    }
    //获取连接对象
    public Connection getConnection() {
        try {
            connection = datasource.getConnection();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
    }
    //4.创建通道
    protected PreparedStatement getPps(String sql){
        try {
            getConnection();
            //insert into users values(?,?,?,?,)
            pps=connection.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pps;
    }
//5.给占位符赋值 list中保存的是给占位符所赋的值
    private void setParams(List list){
     try {
         if(list!=null&&list.size()>0){
             for (int i=0;i
 db.properties(Druid连接参数):
#连接设置 key不能改
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/JavaTest?serverTimezone=UTC
user=root
password=12345678
AirinfoDaoImpl实现类:
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AirinfoDaoImpl extends DbUtils implements AirinfoDao {
    @Override
    public List findAll(){
        ArrayList arrayList = new ArrayList();
        try {
            String sql = "select * from airinfo";
            resultSet = query(sql, null);
            while(resultSet.next()){
                Airinfo airinfo = new Airinfo();
                airinfo.setNumber(resultSet.getString("number"));
                airinfo.setAddress(resultSet.getString("address"));
                airinfo.setBegintime(resultSet.getDate("begintime"));
                arrayList.add(airinfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return arrayList;
    }

    @Override
    public List findByTime(String time) {
        ArrayList arrayList = new ArrayList();
        try {
            String sql = "select * from airinfo where begintime = ?";
            ArrayList params = new ArrayList();
            params.add(time);
            resultSet = query(sql, params);
            while(resultSet.next()){
                Airinfo airinfo = new Airinfo();
                airinfo.setNumber(resultSet.getString("number"));
                airinfo.setAddress(resultSet.getString("address"));
                airinfo.setBegintime(resultSet.getDate("begintime"));
                arrayList.add(airinfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return arrayList;
    }

    @Override
    public List findByAddress(String address) {
        ArrayList arrayList = new ArrayList();
        try {
            String sql = "select * from airinfo where address like ?";
            ArrayList params = new ArrayList();
            //添加%%模糊查询地址
            params.add("%"+address+"%");
            resultSet = query(sql, params);
            while(resultSet.next()){
                Airinfo airinfo = new Airinfo();
                airinfo.setNumber(resultSet.getString("number"));
                airinfo.setAddress(resultSet.getString("address"));
                airinfo.setBegintime(resultSet.getDate("begintime"));
                arrayList.add(airinfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return arrayList;
    }

    @Override
    public int delete(String number) {
        int delete = 0;
        try {
            String sql = "delete from airinfo where number = ?";
            ArrayList params = new ArrayList();
            //添加%%模糊查询地址
            params.add(number);
            delete = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return delete;
    }

    @Override
    public int update(Airinfo airinfo,String time) {
        int update = 0;
        try {
            String sql = "update airinfo set address = ?, begintime = ? where number = ?";
            ArrayList params = new ArrayList();
            //添加%%模糊查询地址
            params.add(airinfo.getAddress());
            params.add(time);
            params.add(airinfo.getNumber());
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return update;
    }

    @Override
    public List findByNumber(String number) {
        ArrayList arrayList = new ArrayList();
        try {
            String sql = "select * from airinfo where number = ?";
            ArrayList params = new ArrayList();
            params.add(number);
            resultSet = query(sql, params);
            while(resultSet.next()){
                Airinfo airinfo = new Airinfo();
                airinfo.setNumber(resultSet.getString("number"));
                airinfo.setAddress(resultSet.getString("address"));
                airinfo.setBegintime(resultSet.getDate("begintime"));
                arrayList.add(airinfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return arrayList;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/726093.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)