六、JDK1.8新特性&日期&Map
1.接口中的JDK1.8新特性(了解)
接口在1.7时,可以放置:抽象方法,常量
public interface Smokeable { //常量 public static final double PI = 3.14; void smoke(); }
接口在1.7时,还可以放置:default 默认方法,static静态方法
public interface Conformityable { default void drink(){} default void smoke(){} default void swim(){} static void common(){ System.out.println("公共的代码!"); } }
2.JDK1.7的时间类
JDK1.7的时间:new Date()
public class DateUtil { //时间转换为字符串 //template = yyyy-MM-dd public static String format(Date date,String template){ SimpleDateFormat sdf = new SimpleDateFormat(template); return sdf.format(date); } public static Date parse(String content,String template){ try{ SimpleDateFormat sdf = new SimpleDateFormat(template); return sdf.parse(content); }catch (Exception e){ e.printStackTrace(); } return null; } } public class MainEnter { public static void main(String[] args) { //获取时间的方式一 System.out.println(new Date()); //获取时间的方式二 long timeMillis = System.currentTimeMillis(); System.out.println(new Date(timeMillis));//获得当前时间的毫秒值 Date d1 = new Date(2000,11,11,12,12,12); Date d2 = new Date(2001,12,12,11,11,11); System.out.println(d1.before(d2));//d1的时间,是否早于d2 System.out.println(d2.after(d1));//d2的时间,是否晚于d1 //日期格式化成字符串: //2024-05-25 10:59:20 //yyyy-MM-dd HH:mm:ss 年月日时分秒 //yyyy-MM-dd //HH:mm:ss System.out.println(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss")); //字符串解析成日期 String dateStr = "2020-12-12";//yyyy-MM-dd System.out.println(DateUtil.parse(dateStr,"yyyy-MM-dd")); } }
3、JDK1.8的时间类
LocalDateTime
public class MainEnter { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); System.out.println(now); //向后或向前推测时间 now = now.plusYears(-50); now = now.plusMonths(120); //将JDK1.8的日期,格式化成字符串 String str = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); System.out.println(str); } }
LocalDate
public class MainEnter { public static void main(String[] args) { LocalDate now = LocalDate.now(); //格式化 System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"))); //前端的字符串 解析为 日期 birthday = 1999-11-12 String birthday = "1999/11/12"; LocalDate date = LocalDate.parse(birthday,DateTimeFormatter.ofPattern("yyyy/MM/dd")); System.out.println(date); //向前/向后推测时间 date = date.plusDays(30000); System.out.println(date); } } public class MainEnter { public static void main(String[] args) { LocalDate now = LocalDate.now(); LocalDate date = LocalDate.parse("1999-12-12"); System.out.println(now.isBefore(date));//当前时间是否早于1999的时间 } }
LocalTime
public class MainEnter { public static void main(String[] args) { LocalTime now = LocalTime.now(); System.out.println(now.format(DateTimeFormatter.ofPattern("HH:mm:ss"))); //将字符串解析为JDK1.8时间 String str = "08:30:00"; LocalTime localTime = LocalTime.parse(str, DateTimeFormatter.ofPattern("HH:mm:ss")); System.out.println(localTime.getHour()); System.out.println(localTime.getMinute()); System.out.println(localTime.getSecond()); } }
七、JDBC
JDBC 全称:Java Database Connectivity 一种Java程序链接数据库的技术!
Java程序链接数据库:存储数据,查询数据
JDBC只是一套API规范,并没有具体的实现!原因:数据库厂商太多了!
驱动下载地址:https://mvnrepository.com
lib 全称libary 库,专门用来放置jar包,jar包中包含了第3方提供的很多的类 & 接口!
于是lib 也被称为:类库
1、查询数据库
public static void main(String[] args) { //第1步骤:加载驱动 //第2步骤:获得链接 //第3步骤:编写SQL语句 //第4步骤:编译SQL语句 //第5步骤:执行&解析结果 //第6步骤:关闭链接 }
2、新增数据库
public class ConnectionUtil { public static Connection getConnection(){ try{ Class.forName("org.gjt.mm.mysql.Driver");//通过"反射"加载驱动 //第2步骤:获得链接 //jdbc:mysql://IP地址:3306/数据库名称?useSSL=false //IP地址,端口号,数据库名称 可以改 String url = "jdbc:mysql://127.0.0.1:3306/student_course?useSSL=false"; String userName = "root"; String password = "150316"; Connection connection = DriverManager.getConnection(url,userName,password); return connection; }catch (Exception e){ e.printStackTrace(); } return null; } }
新增代码:
public class TestAdd { public static void main(String[] args) { Student stu = new Student(null,"锦鲤","女","123456789@qq.com", LocalDate.parse("1990-10-10")); addStudent(stu); } private static void addStudent(Student stu) { try{ //第1步:获得链接 //第2步:编写SQL语句 //第3步:获得编译对象 //第4步:执行SQL语句 //第5步:关闭链接 }catch (Exception e){ e.printStackTrace(); } } }
3、修改数据库
public class TestUpdate { public static void main(String[] args) { //修改之前,先根据ID查询 Student student = getById(5); //再修改数据 student.setStudentName("锦鲤"); student.setGender("女"); updateStudent(student); } private static void updateStudent(Student student) { try{ //第1步:获得链接 //第2步:编写SQL语句 //第3步:获得编译对象 //第4步:执行并获得结果 //第5步:关闭链接 }catch (Exception e){ e.printStackTrace(); } } private static Student getById(long id) { try{ //第1步:获得链接 //第2步:编写SQL //第3步:获得编译对象 //第4步:执行并获得结果 //第5步:关闭链接 return null; }catch (Exception e){ e.printStackTrace(); } return null; } }
4、删除数据库
public class TestDelete { public static void main(String[] args) { deleteById(5); } public static void deleteById(long id){ try{ //第1步:获得链接 //第2步:编写SQL语句 //第3步:获得编译对象 //第4步:执行SQL语句,并处理结果 //第5步:关闭链接 }catch (Exception e){ e.printStackTrace(); } } }
八、SQL注入攻击
SQL注入攻击:
String sex = “‘女’ or 1=1”;
String sql = “select * from students where sex = “ + sex;
String sql = “delete from students where sex =” + sex;
Statement statement = connection.createStatement();
Statement 直接编译器的问题:
1、需要程序员手动的添加 ‘ ‘ 防止SQL注入攻击!
2、SQL 手动拼接数据,很麻烦,很难受
解决方案:PreparedStatement 预编译
九、JdbcTemplate封装
public class JdbcTemplate { public ResultSet query(String sql,Object... params){ Connection connection = null; try{ //第1步:获得链接 connection = ConnectionUtil.getConnection(); //第2步:获得编译对象 & 并且完成赋值 PreparedStatement statement = connection.prepareStatement(sql); int length = params.length; for (int i = 1; i <= length; i++) { statement.setObject(i,params[i-1]); } //第3步:使用编译对象执行sql return statement.executeQuery(); }catch (Exception e){ e.printStackTrace(); } return null; } //insert into 表(字段列表) values (?,?,?,?); //update 表 set 字段=?,字段=?…… where id = ? //delete from 表 where id = ? public int handler(String sql,Object... params){ Connection connection = null; try{ //第1步:获得链接 connection = ConnectionUtil.getConnection(); //第2步:获得编译对象 & 并且完成赋值 PreparedStatement statement = connection.prepareStatement(sql); int length = params.length; for (int i = 1; i <= length; i++) { statement.setObject(i,params[i-1]); } //第3步:使用编译对象执行sql return statement.executeUpdate(); }catch (Exception e){ e.printStackTrace(); }finally { try { connection.close(); } catch (SQLException e) { throw new RuntimeException(e); } } //执行失败就返回-1 return -1; } }