JDBC基本操作、斗地主的发牌(集合的用法)

JDBC基本操作、斗地主的发牌(集合的用法)

因为之前学过jdbc,所以本次课程上手比较快,算是回忆下之前的忘了的东西。

一、jdbc的基本操作

jdbc的基本操作有以下几步:

1、加载驱动

2、创建连接

3、sql语句

4、得到statement对象(之前一直用的是statement,现在老师教的用的是PreparedStatement)

5、执行sql

6、处理结果集

7、关闭资源

以上是数据库查询操作的步骤,如果是增删改的话因为没有结果集,所以少了处理结果集的步骤

以下是完成的代码,注释部分是基础操作,其他完善了点增删查改

顺便:用Scanner在控制台读入数据时第二次读取会读到上次的回车,所以用了String t = sc.nextLine();来把空格读掉

import java.io.IOException;
import java.sql.*;
import java.util.Scanner;

public class Test {
    private static Connection conn;
    private static PreparedStatement pstmt = null;
    private static ResultSet rs = null;
    public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {



        /*try {
            //1、加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2、创建链接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=123");
            //3、sql语句
            String sql = "select * from user";
            //4、得到statement对象
            preparedStatement = connection.prepareStatement(sql);
            //5、执行sql
            resultSet = preparedStatement.executeQuery();
            //6、处理结果集
            while(resultSet.next()){
                System.out.println(resultSet.getInt("id")+ "   " + resultSet.getString("username")+"   "+resultSet.getString("password"));

            }

        }catch (Exception e){

        }finally {
            //7、关闭资源
            connection.close();
            preparedStatement.close();
            resultSet.close();
        }*/
        //search();
        int i = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("输入1添加,输入2删除,输入3修改");
        i = sc.nextInt();
        String sql = "";
        switch (i){
            case 1:
                System.out.println("请输入用户名");
                String t = sc.nextLine();
                String username = sc.nextLine();
                System.out.println("用户名为"+username+",请输入密码");
                String password = sc.nextLine();
                sql ="insert into user(username,password) values('"+username+"','"+password+"') ";
                break;
            case 2:
                search();
                System.out.println("输入要删除的账号id");
                t = sc.nextLine();
                int id = sc.nextInt();
                sql = "delete from user where id="+id;
            break;
            case 3:
                search();
                System.out.println("输入要修改的账号id");
                t = sc.nextLine();
                id = sc.nextInt();
                System.out.println("请输入修改后的用户名");
                t = sc.nextLine();
                username = sc.nextLine();
                System.out.println("请输入修改后的密码");
                password = sc.nextLine();
                sql = "update user set username='"+username+"',password='"+password+"' where id="+id;
                break;
            default:
                System.out.println("输入错误请重新输入"); break;
        }

        adu(sql);

    }
    //查
    public static void search() throws SQLException {

        try {
            conn = Util.getConnection();
            String sql = "select * from user";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while(rs.next()){
                System.out.println(rs.getInt("id")+ "   " + rs.getString("username")+"   "+rs.getString("password"));

            }
        }catch (Exception e){

        }finally{
            Util.close(conn,pstmt,rs);
        }
    }
    //增、删、改
    public static void adu(String sql2) throws SQLException {
        try {
            conn = Util.getConnection();
            pstmt = conn.prepareStatement(sql2);
            pstmt.executeUpdate();
            search();
        }catch (Exception e){

        }finally {
            Util.close(conn,pstmt,rs);
        }
    }

}

还做了一个工具类Util,里面完成getConnection和close的操作:

import java.sql.*;

public class Util {
    public static Connection getConnection(){
        Connection conn = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=123");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    //关闭
    public static void close(Connection conn, PreparedStatement pstmt, ResultSet rs) throws SQLException {
        if(rs!=null){
            rs.close();
        }
        if (pstmt!= null){
            pstmt.close();
        }
        if (conn!=null){
            conn.close();
        }
    }
}

二、斗地主发牌

斗地主的发牌主要就是集合的操作,我对集合的操作不怎么熟悉,边查边完成的代码,如下:

import java.util.ArrayList;
import java.util.Collections;

public class Pocker {

    public static void main(String[] args) {
        ArrayList<String> pocker = new ArrayList<>();
        String[] huas = {"♣","♦","♠","♥"};//梅花,方块,黑桃,红心
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        //添加牌组
        pocker.add("大王");
        pocker.add("小王");
        //pocker.add(huas[0]+numbers[0]);
        for(int i=0;i<=3;i++){
            for (int j=0;j<13;j++){
                pocker.add(huas[i]+numbers[j]);
            }
        }
        //洗牌
        Collections.shuffle(pocker);
        //发牌
        ArrayList<String> player1 = new ArrayList<>();
        ArrayList<String> player2 = new ArrayList<>();
        ArrayList<String> player3 = new ArrayList<>();
        ArrayList<String> other = new ArrayList<>();
        for(int i = 0;i<pocker.size();i++){
            String p = pocker.get(i);
            if(i>=51) other.add(p);
            else if (i%3==1) player2.add(p);
            else if (i%3==2) player3.add(p);
            else player1.add(p);
        }

        //查看牌组
        System.out.println(pocker);
        System.out.println(player1);
        System.out.println(player2);
        System.out.println(player3);
        System.out.println(other);
    }
}

以上是今天的课程,完成了jdbc 的操作和斗地主的发牌

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值