标题Java日记
Day4
使用语句对SQLyog进行增删改查
查:SELECT * FROM student
增:INSERT INTO student(NAME,PASSWORD)VALUES(‘h’,‘8’);
改:UPDATE student SET NAME=‘jiangxinghao’ WHERE id=1;
删:DELETE FROM student WHERE id=3;
创建如图
进行模拟斗地主的编译
package Test;
import java.util.*;
public class doudizhu {
public static void main(String[] args){
List<String> pokers =new ArrayList<>();
// String[] colors=new String[4];
String[] colors=new String[]{"黑桃","红桃","方块","梅花"};
String[] numbers=new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for(String color:colors){
for(String number:numbers){
String poker=number+color;
pokers.add(poker);
}
}
pokers.add("大王");
pokers.add("小王");
System.out.println(pokers);
//洗牌
Collections.shuffle(pokers);
System.out.println(pokers);
List<String> dipai=new ArrayList<>();
Random random=new Random();
for(int i=0;i<3;i++){
int index=random.nextInt(pokers.size()-1);
String pai=pokers.remove(index);
dipai.add(pai);
}
System.out.println(pokers);
System.out.println(dipai);
List<String> wanghao=new ArrayList<>();
List<String> wangfeng=new ArrayList<>();
List<String> wanghong=new ArrayList<>();
for(int i=0;i<pokers.size();i++){
String pai=pokers.get(i);
int mod=i%3;
if(mod==0){
wanghao.add(pai);
}else if(mod==1){
wangfeng.add(pai);
}else{
wanghong.add(pai);
}
}
System.out.println("wanghao"+wanghao);
System.out.println("wangfeng"+wangfeng);
System.out.println("wanghong"+wanghong);
int dizhu=random.nextInt(3);
if(dizhu==0){
wanghao.addAll(dipai);
}else if(dizhu==1){
wangfeng.addAll(dipai);
}else{
wanghong.addAll(dipai);
}
System.out.println("wanghao"+wanghao);
System.out.println("wangfeng"+wangfeng);
System.out.println("wanghong"+wanghong);
}
}
明确数组集合区别:
1.数组是大小固定的,一旦创建无法扩容;集合大小不固定
2.数组的存放的类型只能是一种,集合的存放类型可以不是一种(不加泛型时添加的类型是:Object)
3.数组是java语言中内置的数据类型,是线性排列的,执行效率或者类型检查,都是最快的
ArrayList就是基于数组创建的容器类
jdbc
package Test.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class jdbc {
public static void main(String[] args) throws Exception{
//1.加载驱动
Class.forName(“com.mysql.jdbc.Driver”);
//2.创建连接
Connection connection = DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/nbcj?useSSL=true&characterEncoding=utf-8&user=root&password=123456”);
System.out.println(“创建连接成功”);
//3.写Sql语句
String sql=“select * from student”;
//4.得到statement对象
PreparedStatement preparedStatement=connection.prepareStatement(sql);
//5.执行sqL得到结果集
ResultSet resultSet=preparedStatement.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();
preparedStatement.close();
connection.close();
}
}
//1.加载驱动
//2.创建连接
//3.写sql语句
//4.得到statement对象
//5.执行sql得到结果集
//6.处理结果集
//7.关闭资源
静态 static
写工具类
调用类中方法的方式2种
1.new 对象调用
2.static 直接通过类名调用