wordpress做社交网站吗,ins做甜品网站,怎么做国际网站,好的做彩平图的网站反射
反射API 获取Class对象的方式#xff1a;1. Class.forName(全类名)#xff1a;将字节码文件加载进内存#xff0c;返回Class对象2. 类名.class#xff1a;通过类名的属性class获取3. 对象.getClass()#xff1a;getClass()方法在Object类中定义着。pack…反射
反射API 获取Class对象的方式1. Class.forName(全类名)将字节码文件加载进内存返回Class对象2. 类名.class通过类名的属性class获取3. 对象.getClass()getClass()方法在Object类中定义着。package cn.itcast.reflect;import cn.itcast.domain.Person;import java.lang.reflect.Constructor;
import java.lang.reflect.Method;public class ReflectDemo4 {/**Class对象功能* 获取功能1. 获取成员变量们* Field[] getFields()* Field getField(String name)* Field[] getDeclaredFields()* Field getDeclaredField(String name)2. 获取构造方法们* Constructor?[] getConstructors()* ConstructorT getConstructor(类?... parameterTypes)* ConstructorT getDeclaredConstructor(类?... parameterTypes)* Constructor?[] getDeclaredConstructors()3. 获取成员方法们* Method[] getMethods()* Method getMethod(String name, 类?... parameterTypes)* Method[] getDeclaredMethods()* Method getDeclaredMethod(String name, 类?... parameterTypes)4. 获取类名* String getName()*/public static void main(String[] args) throws Exception {//0.获取Person的Class对象Class personClass Person.class;/*3. 获取成员方法们* Method[] getMethods()* Method getMethod(String name, 类?... parameterTypes)* Method[] getDeclaredMethods()* Method getDeclaredMethod(String name, 类?... parameterTypes)*///获取指定名称的方法Method eat_method personClass.getMethod(eat);Person p new Person();//执行方法eat_method.invoke(p);Method eat_method2 personClass.getMethod(eat, String.class);//执行方法eat_method2.invoke(p,饭);System.out.println(-----------------);//获取所有public修饰的方法Method[] methods personClass.getMethods();for (Method method : methods) {System.out.println(method);String name method.getName();System.out.println(name);//method.setAccessible(true);}//获取类名String className personClass.getName();System.out.println(className);//cn.itcast.domain.Person}}
package fx.data;import org.junit.Test;import java.lang.reflect.*;
import java.util.Arrays;public class OtherTest {// 获得泛型父类Testpublic void test1() throws ClassNotFoundException {Class? clazz Class.forName(fx.data.Person);
// 获得父类Type type clazz.getGenericSuperclass();System.out.println(type);}// 获得父类org.junit.Testpublic void test2() throws ClassNotFoundException {
// 可以获取包、修饰符、类型名、父类包括泛型父类、父接口包括泛型父接口、成员属性、构造器、方法、注解类上的、方法上的、属性上的。// ClassPerson clazz Person.class;Class? clazz Class.forName(fx.data.Person);
// 获得父类Class? superclass clazz.getSuperclass();System.out.println(superclass);}// 获得接口Testpublic void test3() throws ClassNotFoundException {Class? clazz Class.forName(fx.data.Person);Class?[] inter clazz.getInterfaces();System.out.println(Arrays.toString(inter));for (Class o : inter) {System.out.println(o);}}// 获得接口Testpublic void test4() throws ClassNotFoundException {Class? aClass Class.forName(fx.data.Person);Package aPackage aClass.getPackage();
// System.out.println(aPackage);Type genericSuperclass aClass.getGenericSuperclass();System.out.println(genericSuperclass);ParameterizedType genericSuperclass1 (ParameterizedType) genericSuperclass;
// System.out.println(genericSuperclass1);Type[] actualTypeArguments genericSuperclass1.getActualTypeArguments();System.out.println(((Class) actualTypeArguments[0]).getName());}// public int age 1;Testpublic void test5() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {Class? aClass Class.forName(fx.data.Person);Object o aClass.newInstance();Field age aClass.getField(age);age.set(o, 2);Object o1 age.get(o);System.out.println(o1);}// private String name;Testpublic void test6() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {Class? aClass Class.forName(fx.data.Person);Object o aClass.newInstance();Field name aClass.getDeclaredField(name);
// 获取此属性是可以访问name.setAccessible(true);
// 获得权限后可以使用set()修改值和get()获取值name.set(o, lqc);System.out.println(name.get(o));//PS:私有属性:替换为getDeclaredField方法,增加setAccessible方法}// private static String info;Testpublic void test7() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {Class? aClass Class.forName(fx.data.Person);
// Object o aClass.newInstance();Field infoFile aClass.getDeclaredField(info);infoFile.setAccessible(true);// 类变量可以使用nullinfoFile.set(null, 13);
// infoFile.set(aClass, 13);
// 获得权限后可以使用set()修改值和get()获取值System.out.println(infoFile.get(null));
// System.out.println(infoFile.get(aClass));//PS:私有属性:替换为getDeclaredField方法,增加setAccessible方法}// 调用制定的方法
//private String showNation(String nation, int age)
//Testpublic void test8() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {Class clazz Class.forName(fx.data.Person);Object o clazz.newInstance();
// o.getClass(clazz,String.class,int.class);//方法先找到方法.属性也是一样的. Field name aClass.getDeclaredField(name);Method showNation clazz.getDeclaredMethod(showNation, String.class, int.class);
// 设置可以访问showNation.setAccessible(true);//之后就可以调用了.infoFile.set(null, 13);System.out.println(showNation.invoke(o, lqc, 22));}// public static void showInfo()Testpublic void test9() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {Class clazz Class.forName(fx.data.Person);Method showNation clazz.getDeclaredMethod(showInfo);System.out.println(showNation.invoke(clazz));}// private Person(String name, int age)Testpublic void test10() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {Class clazz Class.forName(fx.data.Person);
// 找到Constructor declaredConstructor clazz.getDeclaredConstructor(String.class, int.class);declaredConstructor.setAccessible(true);
// 调用Object o declaredConstructor.newInstance(lqc,22 );System.out.println(o);}
}反射调用全部方法属性构造器
指定属性和方法和构造器.
JDBC代码
package com.atguigu.api.Statement;import java.sql.*;
import java.util.Scanner;public class StatementLogin {public static void main(String[] args) throws Exception {Scanner name new Scanner(System.in);System.out.println(输入账号);String account name.nextLine();System.out.println(输入密码);String password name.nextLine();// System.out.println(n p);String driver com.mysql.cj.jdbc.Driver;Class.forName(driver);
// Connection connection DriverManager.getConnection(jdbc:mysql://127.0.0.1:3306/atguigu?userrootpassword);Connection connection DriverManager.getConnection(jdbc:mysql://127.0.0.1:3306/atguigu?userrootpassword);Statement statement connection.createStatement();
// String mysql select * from t_user where accountnameand passwordpassword;;
// String sql select * from t_user where account account and PASSWORD password;;String sql select * from t_user where account account and password password ;;
// 如果* 是查询有别名就用别名
/*
* todo:密码填入 or 11 出现了第三个逻辑判断,影响了判断导致结果是true.导致所有数据泄漏出来 是注入攻击
* 这就是静态SQL,没有动态值语句
*
* */// DQL(查询方法) DML(增加删除修改)// DML使用这个返回影响了多少行,例如修改了多少行等等
// int i statement.executeUpdate(sql);
// DQL使用这个ResultSet resultSet statement.executeQuery(sql);
// 将查询的结果返回一个resultSet对象,内部也是有行和列的.System.out.println(resultSet);//resultSet里面查询出来的数据也是有行有列,有游标.next()可以判断下一行是否存在之后到下一行.默认游标在第一行前面.
// next()方法在判断有无下一行之后游标可以往后一行.while (resultSet.next()) {
// resultSet.get列名类型(列名)int id resultSet.getInt(id);String account1 resultSet.getString(account);String password1 resultSet.getString(password);String nickname resultSet.getString(nickname);System.out.println(id - account1 - password1 - nickname);}/** TODO:单行数据就用if,多行就用while*** */if(resultSet.next()){
// System.out.println(success);
// }else{
// System.out.println(fail);
// }}
}package com.atguigu.api.Statement;import com.mysql.cj.jdbc.Driver;import java.sql.*;
import java.util.Properties;public class StatementQuery {/** TODO:*** */public static void main(String[] args) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
// 依赖导入只需要一次.// 1.注册驱动还有反射好处// 方案一:DriverManager.registerDriver(new Driver());
// 这个会注册两次驱动,所以只需要new Driver().里面会触发代码块.DriverManager.registerDriver(new Driver());
// 方案二: new Driver();代码写死了
// 方案三:String driver com.mysql.cj.jdbc.Driver;Class.forName(driver);
// 如果我们使用orlce数据库,new OracleDriver()就要修改我们的代码,重新更新我们的服务器.
// 需要我们的修改代码.使用放射的话,我们把类名字放到我们的数据文件里面.需要的使用进行更改文件就不需要更改代码类名了
// 可以看出放射的好处了.修改名字就能加载类/** Todo:类加载机制:* 加载[class文件--class对象]* 链接[验证(检查文件类型)--准备(静态代码块默认赋值)--解析(触发静态代码块)]* 初始化[(静态属性值赋真实值)]* 类加载机制使用 1.反射 2.new 关键字 3.调用静态属性值 4.调用静态方法 5.子类触发父类 6.程序main方法.* */
//
// 2.链接数据库/*TODO:就像navcat链接数据库链接服务器一样,java程序也是这样的,也是需要ip端口,账号密码链接数据库名字String url:* 数据库驱动及其软件* url:咯擦了红石头|127.0.0.1* 端口号* 数据库表名字* 账号* 密码* *///这个是三个参数的写法
// Connection root DriverManager.getConnection(jdbc:mysql://127.0.0.1:3306/atguigu, root, );// 还有两个参数的写法链接数据库
// Properties infonew Properties();
// info.put(user,root);
// info.put(password,);
// Connection connection DriverManager.getConnection(jdbc:mysql://127.0.0.1:3306/atguigu, info);// 还有一种的写法,加上jdbc:mysql://127.0.0.1:3306/atguigu?keyvaluekeyvalue; 多了?keyvaluekeyvalueConnection connection DriverManager.getConnection(jdbc:mysql://127.0.0.1:3306/atguigu?userrootpassword);//
// 3.创建statement,statement可以发送sql语句到数据库Statement statement connection.createStatement();
// 4.写sql,运输开车过去数据库,返回结果,String mysql select * from t_user;;ResultSet resultSet statement.executeQuery(mysql);System.out.println(resultSet);
// 看看下一行还有没有数据
// 5.解析结果while (resultSet.next()) {int id resultSet.getInt(id);String account resultSet.getString(account);String password resultSet.getString(PASSWORD);String nickname resultSet.getString(nickname);System.out.println(id account password nickname);}
// 6.关闭资源statement.close();connection.close();resultSet.close();}
}
prepareStatement
package com.atguigu.prepadred;import com.mysql.cj.jdbc.Driver;import java.sql.*;
import java.util.Scanner;/** 使用预编译statement完成登陆,解决了注入攻击和String拼装问题** */
public class PSUserLoginpart {public static void main(String[] args) throws ClassNotFoundException, Exception {Scanner name new Scanner(System.in);System.out.println(输入账号);String account name.nextLine();System.out.println(输入密码);String password name.nextLine();
//new Driver();Class.forName(com.mysql.cj.jdbc.Driver);
// 会调用类的静态方法Connection connection DriverManager.getConnection(jdbc:mysql://127.0.0.1:3306/atguigu?userrootpassword);
//预编译prepareStatement,用于创建可以运行参数化SQL查询的预编译的声明对象。String mysql select * from t_user where account? and password?;;PreparedStatement preparedStatement connection.prepareStatement(mysql);// 单独的占位符进行赋值,index从1开始 ,preparedStatement.setObject(1, account);preparedStatement.setObject(2, password);ResultSet resultSet preparedStatement.executeQuery();if (resultSet.next()) {System.out.println(登录成功);} else {System.out.println(登录失败);}connection.close();resultSet.close();preparedStatement.close();}
}package com.atguigu.prepadred;import org.junit.Test;import java.sql.*;
import java.util.*;public class PSCURDpart {Testpublic void testInsert() throws Exception {
// 写一个javaClass.forName(com.mysql.cj.jdbc.Driver);Connection connection DriverManager.getConnection(jdbc:mysql://127.0.0.1:3306/atguigu?userrootpassword);String sql INSERT INTO t_user(account,password,nickname) value(?,?,?);;PreparedStatement preparedStatement connection.prepareStatement(sql);preparedStatement.setObject(1, lqc);preparedStatement.setObject(2, 123456);preparedStatement.setObject(3, BOSS);
// DML语言int row preparedStatement.executeUpdate();System.out.println(row);if (row 1) {System.out.println(插入成功);} else {System.out.println(插入失败);}}Testpublic void testSelect() throws Exception {//new Driver();Class.forName(com.mysql.cj.jdbc.Driver);
// 会调用类的静态方法Connection connection DriverManager.getConnection(jdbc:mysql://127.0.0.1:3306/atguigu?userrootpassword);
//预编译prepareStatement,用于创建可以运行参数化SQL查询的预编译的声明对象。String mysql select * from t_user;;PreparedStatement preparedStatement connection.prepareStatement(mysql);ResultSet resultSet preparedStatement.executeQuery();ListMap list new ArrayList();// 2.装的是列的结果集合ResultSetMetaData metaData resultSet.getMetaData();System.out.println(metaData);int columnCount metaData.getColumnCount();//列的数量while (resultSet.next()) {
//Map map new HashMap();for (int i 1; i columnCount; i) {Object value resultSet.getObject(i);String key metaData.getColumnLabel(i);map.put(key,value);}list.add(map);}System.out.println(list);connection.close();resultSet.close();preparedStatement.close();}
}package com.atguigu.prepadred;import java.sql.*;public class PSother {public static void main(String[] args) throws Exception {
// new PSother().returnPrimaryKey();new PSother().Batch();}//主键回显public void returnPrimaryKey() throws Exception {Class.forName(com.mysql.cj.jdbc.Driver);
// 会调用类的静态方法Connection connection DriverManager.getConnection(jdbc:mysql://127.0.0.1:3306/atguigu?userrootpassword);
//预编译prepareStatement,用于创建可以运行参数化SQL查询的预编译的声明对象。String mysql INSERT INTO t_user (account, password,nickname) values (?,?,?);;PreparedStatement preparedStatement connection.prepareStatement(mysql, Statement.RETURN_GENERATED_KEYS);for (int i 10; i 10010; i) {preparedStatement.setObject(1, test i);preparedStatement.setObject(2, 123546 i);preparedStatement.setObject(3, 管理员 i);}int rows preparedStatement.executeUpdate();if (rows 0) {System.out.println(插入成功);
// 一行一列的结果集对象, id 值ResultSet generatedKeys preparedStatement.getGeneratedKeys();generatedKeys.next();int anInt generatedKeys.getInt(1);System.out.println(anInt);}
//获取列的话是:statement.getObject(column);}//批量插入/*todo:* 批量细节* 1.url?rewriteBatchedStatementstrue* 2.insert 语句必须使用 values* 3.语句后面不能添加分号; String mysql INSERT INTO t_user (account, password,nickname) values (?,?,?) sql没有分号结束* 4.语句不能直接执行每次需要装货 addBatch() 最后 executeBatch();*/public void Batch() throws Exception {Class.forName(com.mysql.cj.jdbc.Driver);
//加入rewriteBatchedStatementstrueConnection connection DriverManager.getConnection(jdbc:mysql://127.0.0.1:3306/atguigu?rewriteBatchedStatementstrue, root, );
// jdbc可以这样INSERT INTO t_user (account, password,nickname) values (?,?,?),(),(),(),();String mysql INSERT INTO t_user (account, password,nickname) values (?,?,?);PreparedStatement preparedStatement connection.prepareStatement(mysql, Statement.RETURN_GENERATED_KEYS);long star System.currentTimeMillis();for (int i 1; i 10000; i) {preparedStatement.setObject(1, testt i);preparedStatement.setObject(2, op i);preparedStatement.setObject(3, 管理员 i);preparedStatement.addBatch();//把()添加到sql后面}preparedStatement.executeBatch();//执行添加后的语句long end System.currentTimeMillis();System.out.println(end - star ms);preparedStatement.close();connection.close();}}
Druid的软链接和硬链接
package com.atguigu.Druid;import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.Test;import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;public class DruidUserpart {// 使用代码来设置连接池public void testHard() throws SQLException {DruidDataSource druidDataSource new DruidDataSource();
// 设置参数/** todo:必须设置的参数* 1.把驱动交给连接池* 2.url|user|password** */druidDataSource.setDriverClassName(com.mysql.cj.jdbc.Driver);druidDataSource.setUrl(jdbc:mysql://127.0.0.1:3306/atguigu);druidDataSource.setUsername(root);druidDataSource.setPassword();/** todo:非必须设置的参数* 1.连接池初始化数量* 2.连接池最大数量* */druidDataSource.setInitialSize(5);druidDataSource.setMaxActive(10);
// 把连接池的连接拿出来DruidPooledConnection connection druidDataSource.getConnection();connection.close();//连接池提供的,close就是回收连接.}Testpublic void testSoft() throws Exception {Properties properties new Properties();InputStream resourceAsStream DruidUserpart.class.getClassLoader().getResourceAsStream(druid.properties);properties.load(resourceAsStream);// 创建连接池之后 DruidDataSource druidDataSource new DruidDataSource(); 之后配置.DataSource dataSource DruidDataSourceFactory.createDataSource(properties);Connection connection dataSource.getConnection();
// curd操作//connection.close();}}druid.properties是配置文件
测试用例
0
Deprecated表示已经弃用
1
元注解就是:注解的注解,相当于是注解的说明
元注解 还有下面代码组成的
public class MyannoTest {}注解的本质就是一个接口.继承了接口类在注解中,返回类型很多.也可以设置default
package cn.itcast.day1;public interface MyAno {// 属性是全局静态属性// 方法是抽象方法// 但是抽象方法在注解是属性int age();String show2() default mm;Person per();
// Myanno no();String[] str();
}下面是测试类
package cn.itcast.day1;Myanno(12)
//数组只有一个的时候可以不写大括号
MyAno(age 1, show2 lqc, per Person.p2, str {abc})
public class MyannoTest {}
2.
Myanno(12)这种情况绝对注解的属性值只有一个而且有属性为value.
3.元注解
Target 注解可以作用的位置 Retention 注解的存在时间 Inherited 注解在父类的时候注解可以被子类继承
package cn.itcast.day1;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD})
//ElementType.TYPE表示作用与类接口枚举,不能用在方法和属性
//ElementType.FIELD,作用在属性
//ElementType.METHOD作用在方法Retention(RetentionPolicy.RUNTIME)
//表示当前描述的注解可以再class之中,并且被jvm运行Retention(RetentionPolicy.CLASS)
//表示当前描述的注解可以再class之中,不会被jvm运行Retention(RetentionPolicy.SOURCE)
//不会在class和jvm中
Inherited
public interface Myanno3 {}注解的作用
package cn.itcast.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;Target({ElementType.TYPE})
Retention(RetentionPolicy.RUNTIME)
public interface Pro {String className();String methodName();}package cn.itcast.annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//通过注解
Pro(className cn.itcast.annotation.Demo1, methodName show)
public class ReflectTest {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
// 1.获取ReflectTest的class文件ClassReflectTest demo1Class ReflectTest.class;Pro annotation demo1Class.getAnnotation(Pro.class);
// 这里的className()是一个方法,虽然名字叫属性,其实是方法String className annotation.className();String methodName annotation.methodName();System.out.println(className methodName);Class? aClass Class.forName(className);Object o aClass.newInstance();Method show aClass.getMethod(show);show.invoke(o);}
}反射应用,写一个简单的测试框架
package cn.itcast.annotation;import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.Buffer;
import java.util.Arrays;public class CalculatorTest {public static void main(String[] args) throws IOException, InvocationTargetException, IllegalAccessException {CalculatorDemo calculatorDemo new CalculatorDemo();Class aClass calculatorDemo.getClass();Method[] declaredMethods aClass.getMethods();BufferedWriter bufferedWriter new BufferedWriter(new FileWriter(bug.txt,true));// System.out.println(bufferedWriternull);for (Method method : declaredMethods) {/** todo:心得:这个能否获取注解要看注解的存在时间的设置.!!!** */if (method.isAnnotationPresent(Check.class)) {try {method.invoke(calculatorDemo);} catch (Exception e) {System.out.println(error);bufferedWriter.write(123);// 新增以下两行代码bufferedWriter.newLine();bufferedWriter.flush();}}}}
}
package cn.itcast.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;Target({ElementType.METHOD})
Retention(RetentionPolicy.RUNTIME)
public interface Check {
}