8.8
1.jdbc的学习
//1.加载驱动
//2.创建连接
//3.写sql语句
//4.得到statement对象
//5.执行sql得到结果集
//6.处理结果集
//7.关闭资源
代码:
package com.zhongruan.test;
import java.sql.*;
public class Testjdb {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName(“com.mysql.jdbc.Driver”);
//2.创建连接
Connection connection = DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/ningda?useSSL=true&characterEncoding=utf-8&user=root&password=root\n”);
System.out.println(“创建连接成功”);
//3.写sql语句
String sql=“select * from student”;
//4.得到statement对象
PreparedStatement prepareStatement = connection.prepareStatement(sql);
//5.执行sql得到结果表
ResultSet resultSet = prepareStatement.executeQuery();
//6.处理结果集
while (resultSet.next()){
System.out.println(resultSet.getInt(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getString(3));
}
//7.关闭资源
resultSet.close();
prepareStatement.close();
connection.close();
}
}
2.实现斗地主的两个功能
package com.zhongruan.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Doudizhu {
public static void main(String[] args) {
/**
* 功能:
* 1.准备一幅牌(54张)
* 2.洗牌(打乱牌序)
* 3.发牌(三个人)
* 4.留三张底牌
* 5.每个人拿到牌后,对牌进行排序
* 6.出牌…
*
* 二.发牌(每个人的牌是排序)
*/
//1.准备一幅牌(54张)
List<String> pokers=new ArrayList<>();
//花色
/**
String[] colors=new String[4];
colors[0]="黑桃";
colors[1]="红桃";
colors[2]="方块";
colors[3]="梅花";
*/
String[] colors = {"黑桃", "红桃", "方块", "梅花"};
String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2",};
for (String color : colors) {
for (String number : numbers) {
String poker = number+color;
pokers.add(poker);
}
}
pokers.add("大王");
pokers.add("小王");
System.out.println(pokers);
//2.洗牌(打乱牌序)
Collections.shuffle(pokers);
System.out.println(pokers);
List<String> dipai = new ArrayList<>();
Random random=new Random();
for (int x=0; x<3; x++){
int index=random.nextInt(pokers.size()-1);//0-53的随机数
String pai=pokers.remove(index);
dipai.add(pai);
}
System.out.println(pokers);
System.out.println(dipai);
List<String> lin=new ArrayList<>();
List<String> hua=new ArrayList<>();
List<String> zheng=new ArrayList<>();
for (int i=0;i<pokers.size();i++) {
String pai = pokers.get(i);
int mod = i % 3;
if (mod == 0) {
lin.add(pai);
} else if (mod == 1) {
hua.add(pai);
} else {
zheng.add(pai);
}
}
System.out.println("lin: "+lin );
System.out.println("hau: "+hua );
System.out.println("zheng: "+zheng );
int dizhu =random.nextInt(3);
if (dizhu == 0) {
lin.addAll(dipai);
}else if (dizhu == 1){
hua.addAll(dipai);
}else {
zheng.addAll(dipai);
}
System.out.println("lin: "+lin );
System.out.println("hau: "+hua );
System.out.println("zheng: "+zheng );
}
}
3.mysql实现增删改查的功能
SELECT * FROM student;
INSERT INTO student(id,NAME,PASSWORD) VALUES(4,‘123’,‘hualili’);
UPDATE student SET NAME=‘zheng’ WHERE id=1;
SELECT FROM student WHERE id=3;
静态 static
写工具类
调用类中方法的方式2种
1.new 对象调用
2.static 直接通过类名调用