package com.jxr;
/*
* @Author jingxurun
* @Date 2024/2/6 星期二 16:36
* @Version
**/
public class Person {
private String name;
private int age;
private String number;
public Person() {
}
private Person(String number) {
this.number = number;
}
public Person(String name, int age, String number) {
this.name = name;
this.age = age;
this.number = number;
}
public String name() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int age() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String number() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", number='" + number + '\'' +
'}';
}
private void show(){
System.out.println("你竟然可以调用我");
}
}
package com.jxr;
/*
* @Author jingxurun
* @Date 2024/2/6 星期二 16:33
* @Version
**/
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class GetClassTest {
@Test
public void getClassTest() {
//获取Class对象,方式一:通过全类名.class的方式获取 全类名即包名+类名
Class c1 = Person.class;
System.out.println(c1);//class com.jxr.Person
//方式二:引用.getClass()得到类的Class对象。
Person person = new Person();
Class c2 = person.getClass();
System.out.println(c2);//class com.jxr.Person 双亲委派模型 只会加载一次 不会重复加载
System.out.println(c1 == c2);//true
//方式三:Class.forName(“全类名”)[推荐使用]
try {
Class c3 = Class.forName("com.jxr.Person");//这个类可以没有编译阶段不会报错,运行阶段才会报错
System.out.println(c3);//class com.jxr.Person
System.out.println(c1 == c3 && c2 ==c3);//true 地址值
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
//在调用时,
//如果类在内存中不存在,则会加载到内存;
//如果类在内存中存在,不会重复加载,而是会重复利用
//一个class文件在内存中不会存在两个类对象
}
@Test
public void getConstructorTeest() throws Exception {
//获取类的class对象
Class c = Class.forName("com.jxr.Person");
//获取构造器 public
Constructor constructor = c.getConstructor();
Object o = constructor.newInstance();
System.out.println(o);
System.out.println("-------------------------------------");
//获取Person类中私有的构造器
Constructor declaredConstructor = c.getDeclaredConstructor(String.class);
declaredConstructor.setAccessible(true);//设置访问权限
Object o1 = declaredConstructor.newInstance("13843834438");
System.out.println(o1);
}
@Test
public void getMethodTest() throws Exception{
Class c = Class.forName("com.jxr.Person");
Constructor constructor = c.getConstructor();
Object o = constructor.newInstance();
Method setName = c.getMethod("setName", String.class);
setName.invoke(o, "jxr");
System.out.println(o);
System.out.println("------------------------");
Method declaredMethod = c.getDeclaredMethod("show");
declaredMethod.setAccessible(true);
declaredMethod.invoke(o);
System.out.println(o);
}
}
/*
* @Author jingxurun
* @Date 2024/1/31 星期三 11:07
* @Version
**/
import java.io.*;
public class Demo01 {
public static void main(String[] args) throws IOException {
/*
ClassLoader classLoader = Demo01.class.getClassLoader();//获取类加载器
System.out.println(classLoader);//jdk.internal.loader.ClassLoaders$e
// AppClassLoadr 应用类加载器
// @63947c6b
System.out.println(classLoader.getParent());//获取父类 jdk.internal.loader.ClassLoaders$PlatformClassLoader@4eec7777
System.out.println(classLoader.getParent().getParent());//获取父类 null
**/
InputStream inputStream = Demo01.class.getClassLoader().getResourceAsStream("haha.txt");//获得输入流
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));//缓冲字符读取流
try {
String text = br.readLine();//会优先加载resources资源下的haha.txt
System.out.println(text);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
package com.java;
/*
* @Author jingxurun
* @Date 2024/2/9 星期五 13:27
* @Version
**/
import java.sql.*;
public class JDBCDemo {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//写在里面释放时会够不着
//1.在项目上新建一个文件夹 lib ,导入MySQL驱动,并将jar包导入到项目中
try {
//2.注册驱动,使用到反射的第三种方式
Class.forName("com.mysql.cj.jdbc.Driver");
//3.获取连接
/*String url = "jdbc:mysql://localhost:3306/userdb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";//本地*/
String url = "jdbc:mysql://39.107.92.169:3306/userdb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";//云端
String username = "userdb";
String password = "root";
/*Connection*/ connection = DriverManager.getConnection(url, username, password);
//System.out.println(connection);//com.mysql.cj.jdbc.ConnectionImpl@1ea9f6af
//4.获取预编译sql,查询emp表中的数据 效率高,安全,防止SQL灌入
String sql = "select * from emp";
/*PreparedStatement*/ preparedStatement = connection.prepareStatement(sql);
//5.查询,获取结果集(增删改,添加数据即可)并遍历,查询使用executeQuery
// 增删改使用execute()[返回布尔类型]或者executeUpdate()[返回int类型只要大于0就代表插入成功]
/*ResultSet*/ resultSet = preparedStatement.executeQuery();
while (resultSet.next()/*看还有没有下一条*/){
String ename = resultSet.getString("ename");
String job = resultSet.getString("job");
System.out.println(ename + "======" + job);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//6.释放资源 Connection,PrepareStatement,ResultSet,释放时需从后往前释放
//如果 Connection,PrepareStatement,ResultSet对象为null JVM GC(垃圾回收装置)回收不是立即会过一会儿
if (resultSet != null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement != null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
package com.util;
/*
* @Author jingxurun
* @Date 2024/2/9 星期五 18:30
* @Version
* @title 封装连接与关闭的工具类
**/
import com.mysql.cj.protocol.Resultset;
import com.mysql.cj.xdevapi.Result;
import java.sql.*;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;
public class DButils {
private static String url = "";
private static String username = "";
private static String password = "";
//静态代码块 只执行一次
static {
InputStream inputStream = DButils.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
try {
properties.load(inputStream);
/*String*/ url = properties.getProperty("url");
/*String*/ username = properties.getProperty("username");
/*String*/ password = properties.getProperty("password");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//相当于注册驱动
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() {
try {
return DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void close(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
//6.释放资源 Connection,PrepareStatement,ResultSet,释放时需从后往前释放
//如果 Connection,PrepareStatement,ResultSet对象为null JVM GC(垃圾回收装置)回收不是立即会过一会儿
if (resultSet != null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement != null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package com.util;
import com.mysql.cj.jdbc.PreparedStatementWrapper;
import com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import org.junit.Test;
import java.sql.*;
import java.sql.Connection;
import static org.junit.Assert.*;
/*
* @Author jingxurun
* @Date 2024/2/17 星期六 18:35
* @Version
**/
public class DButilsTest {
private static final String INSERT_SQL;
private static final String UPDATE_SQL;
private static final String DELETE_SQL;
static {
INSERT_SQL = "INSERT INTO emp(empno,ename,job) VALUES(?,?,?)";
UPDATE_SQL = "UPDATE emp SET ename = ?,job = ? WHERE empno = ?";
DELETE_SQL = "DELETE FROM emp WHERE empno = ?";
}
@Test
public void test01() {
Connection connection = DButils.getConnection();
System.out.println(connection);//com.mysql.cj.jdbc.ConnectionImpl@6e2c9341
}
@Test//增加
public void insertTest(){
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = DButils.getConnection();//获得连接
preparedStatement = connection.prepareStatement(INSERT_SQL);
preparedStatement.setString(1,"2024");//第一个位置的问号
preparedStatement.setString(2,"lxp");//第二个位置的问号
preparedStatement.setString(3,"worker");//第三个位置的问号
int i = preparedStatement.executeUpdate();
if (i > 0){
System.out.println("插入成功了!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DButils.close(connection,preparedStatement,null);//增删改没有结果系
}
}
@Test//修改
public void updateTest(){
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = DButils.getConnection();//获得连接
preparedStatement = connection.prepareStatement(UPDATE_SQL);
preparedStatement.setString(1,"jxr");
preparedStatement.setString(2,"CEO");
preparedStatement.setString(3,"2024");
int i = preparedStatement.executeUpdate();
if (i > 0){
System.out.println("修改成功!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DButils.close(connection,preparedStatement,null);//增删改没有结果系
}
}
@Test//删除
public void deleteTest(){
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = DButils.getConnection();//获得连接
preparedStatement = connection.prepareStatement(DELETE_SQL);
preparedStatement.setString(1,"2024");
boolean b = preparedStatement.execute();
if (b == false){
System.out.println("删除成功!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DButils.close(connection,preparedStatement,null);//增删改没有结果系
}
}
}