首先对数据库进行设置
需要配置文件
(1)pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.mingzi</groupId>
<artifactId>jdbcdemo</artifactId>
<version>1.0</version>
</parent>
<artifactId>db-Demo</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- mysql-connector-java -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.1.0</version>
</dependency>
<!-- com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Demo.java直接使用(还没加密)
public class DbDemo {
public static void main(String[] args){
try(DruidDataSource ds = new DruidDataSource()) {
ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
ds.setUrl("jdbc:mysql:/dbok");
ds.setUsername("admin");
ds.setFilters("stat,config");
ds.setConnectionProperties("config.decrypt=true;config.decrypt.key=");
ds.setPassword("");
var c =ds.getConnection();
System.out.println(c.getMetaData().getDatabaseMajorVersion());
}catch (Exception e){
e.printStackTrace();
}
}
找到对应jar包的路径,输入cmd进入控制命令台
进入命令控制台输入命令:
java -cp druid-1.2.18.jar com.alibaba.druid.filter.config.ConfigTools admin
#开启解密需要公要字符串如下:publicKey字符串
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMOz+sKtDhQBaDParZAylpFsPmHWDnFg3lLEqsXDNA9KEJL7rHTJG25duRw6XfRfYbfb1vQtB/O/UdRO7tcHq68CAwEAAQ==
publicKey,password加入代码中:
public class DbDemo {
public static void main(String[] args){
try(DruidDataSource ds = new DruidDataSource()) {
ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
ds.setUrl("jdbc:mysql:/dbok");
ds.setUsername("admin");
ds.setFilters("stat,config");
ds.setConnectionProperties("config.decrypt=true;config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMOz+sKtDhQBaDParZAylpFsPmHWDnFg3lLEqsXDNA9KEJL7rHTJG25duRw6XfRfYbfb1vQtB/O/UdRO7tcHq68CAwEAAQ==");
ds.setPassword("rN18MbLELYlEsx7pxVcKpmFpcgXKJtpZUitOpOEAXJkXxs/M1gOHY0VniYljo2IaYY/M3hcOR8qnxFBXQAE74w==");
var c =ds.getConnection();
System.out.println(c.getMetaData().getDatabaseMajorVersion());
}catch (Exception e){
e.printStackTrace();
}
}
执行结果:显示的是数据库8.1.0的版本号
如何解密:创建一个Ok.java类,编写代码
public class Ok {
public static void main(String[] args) throws Exception {
String pk = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMOz+sKtDhQBaDParZAylpFsPmHWDnFg3lLEqsXDNA9KEJL7rHTJG25duRw6XfRfYbfb1vQtB/O/UdRO7tcHq68CAwEAAQ==";
String pw = "rN18MbLELYlEsx7pxVcKpmFpcgXKJtpZUitOpOEAXJkXxs/M1gOHY0VniYljo2IaYY/M3hcOR8qnxFBXQAE74w==";
String ps = ConfigTools.decrypt(pk,pw);
System.out.println(ps);
}
}