jdbc封装,斗地主和音乐管理系统

一.用一种新的方式来写斗地主小程序

代码如下

public class DouDiZhu2 {
    public static void main(String[] args){
        //1.拿到一张牌
        Map<Integer,String> poker=new HashMap<>();
        String[] nums={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        String[] colors={"黑桃","红桃","梅花","方块"};
        int index=0;
        for(String num:nums){
            for(String color:colors){
                String pai=color+num;
                index=index+1;
                poker.put(index,pai);
            }
        }
        System.out.println(index);
        index++;

        poker.put(index+1,"大王");
        index++;
        poker.put(index+1,"小王");
        System.out.println(poker);
        //2.洗牌
        List<Integer> pokerIndexs=new ArrayList<>();
        Set<Integer> integers=poker.keySet();
        for (Integer i:poker.keySet()){
            pokerIndexs.add(i);
        }
        Collections.shuffle(pokerIndexs);
        System.out.println(pokerIndexs);
        //3.留三张底牌
        Set<Integer> dipaiInds=new TreeSet<>();
        dipaiInds.add(pokerIndexs.remove(0));
        dipaiInds.add(pokerIndexs.remove(0));
        dipaiInds.add(pokerIndexs.remove(0));
        System.out.println(dipaiInds);
        System.out.println(pokerIndexs);
        //4.发牌
        Set<Integer> yzj=new TreeSet<>();
        Set<Integer> wdx=new TreeSet<>();
        Set<Integer> xxx=new TreeSet<>();
        for (int i=0;i<pokerIndexs.size();i++){
            int pi=pokerIndexs.get(i);
            int mod=i%3;
            if(mod==0){
                yzj.add(pi);
            }else if(mod==1){
                wdx.add(pi);
            }else {
                xxx.add(pi);
            }
        }
        System.out.println(yzj);
        System.out.println(wdx);
        System.out.println(xxx);
        //5.看牌
        Look(poker,yzj);
        Look(poker,wdx);
        Look(poker,xxx);
    }

    public static void Look(Map<Integer, String> poker, Set<Integer> indexs) {
        List<String> p=new ArrayList<>();
        for(Integer i:indexs){
            String pai=poker.get(i);
            p.add(pai);
        }
        System.out.println(p);
    }
}

二.音乐管理系统

1.新建一个表
在这里插入图片描述使用数据库,打开表查看表,以下图表示创建表成功
在这里插入图片描述2.进入Idea创建包和JAVA文件
在这里插入图片描述
Test中代码

public class Test {
    public List<Music> findMusic() throws SQLException {
        ResultSet resultSet=null;
        PreparedStatement statement=null;
        Connection connection=null;
        List<Music> musics=new ArrayList<>();
        try {
            connection= DBUtil.getConnection();
            String sql="select * from music";
            statement=connection.prepareStatement(sql);
            resultSet=statement.executeQuery();
            while(resultSet.next()){
                Music music=new Music();
                music.setId(resultSet.getInt(1));
                music.setName(resultSet.getString(2));
                music.setAuthor(resultSet.getString(3));
                musics.add(music);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(resultSet,statement,connection);
        }
        return musics;

    }
    public static void main(String[] args) throws SQLException {
        Test test=new Test();
        List<Music> musics=test.findMusic();
        System.out.println(musics);
    }}


DButil中代码

public class DBUtil {
    public static Connection getConnection() throws SQLException{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/zjgm?characterEncoding=utf-8&user=root&password=123456");
        System.out.println("操作成功!");
        return connection;
    }
    public static void closeAll(ResultSet resultSet,Statement statement, Connection connection){
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

Music中代码

public class Music {
    private int id;
    private String name;
    private String author;

    public int getId(int anInt) {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName(String string) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Music{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值