当前位置:网站首页>Customize an annotation to get a link to the database
Customize an annotation to get a link to the database
2022-06-30 11:43:00 【Full stack programmer webmaster】
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface JdbcInfo { String driverClass() default “com.mysql.jdbc.Driver”; String url(); String username() default “root”; String password() default “1012”; }
import java.lang.reflect.Method; import java.sql.Connection; import java.sql.DriverManager;
public class JdbcUtils { @JdbcInfo(url = “jdbc:mysql://localhost:3306/day16”) public static Connection getConnection() throws Exception{ // Get bytecode file Class clazz = JdbcUtils.class; // obtain getConnection() Method method = clazz.getMethod(“getConnection”, null); if(method.isAnnotationPresent(JdbcInfo.class)){// Whether there are comments on the judgment method // Get comments JdbcInfo jdbcInfo = method.getAnnotation(JdbcInfo.class); // Get four parameters String driverClass = jdbcInfo.driverClass(); String url = jdbcInfo.url(); String username = jdbcInfo.username(); String password = jdbcInfo.password(); // Registration drive Class.forName(driverClass); Connection connection = DriverManager.getConnection(url, username, password); // Get the connection return connection; } return null; } public static void main(String[] args) throws Exception { System.out.println(getConnection()); } }
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/100805.html Link to the original text :https://javaforall.cn
边栏推荐
- Win10 R package installation error: not installed in arch=i386
- “\“id\“ contains an invalid value“
- koa - 洋葱模型浅析
- Discussion on the essence of "FPGA mining" from open source projects
- Multiparty cardinality testing for threshold private set-2021: Interpretation
- 【重温经典C语言】~c语言中%x、%c、%d、%x等等等、c语言取地址符&的作用、C语言中的 联合体
- Oceanbase installation Yum source configuration error and Solutions
- 【西安交通大学】考研初试复试资料分享
- 深入解析 Apache BookKeeper 系列:第四篇—背压
- 导致系统性能失败的10个原因
猜你喜欢

导致系统性能失败的10个原因

10 reasons for system performance failure

Uncover the whole link communication process of customer service im

R语言查看版本 R包查看版本

Methods and usage of promise async and await

19年来最艰难的618,徐雷表达三个谢意

揭秘得物客服IM全链路通信过程

Qualcomm released the "magic mirror" of the Internet of things case set, and digital agriculture has become a reality

It's time for the kotlin coroutine to schedule thread switching to solve the mystery

Boost研究:Boost Log
随机推荐
暑假学习记录
10 reasons for system performance failure
Typescript readonlyarray (read only array type) details
使用cookie技术实现历史浏览记录并控制显示的个数
CVPR 2022 | greatly reduce the manual annotation required for zero sample learning. Mapu and Beiyou proposed category semantic embedding rich in visual information
Go language defer
100 important knowledge points that SQL must master: insert data
60 个神级 VS Code 插件!!
暑假学习记录
数据库 级联操作
他是上海两大产业的第一功臣,却在遗憾中默默离世
林克庆到番禺区调研“发展要安全”工作 以“时时放心不下”责任感抓好安全发展各项工作
创建型-配置工厂
Alibaba cloud lifeifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
19年来最艰难的618,徐雷表达三个谢意
STM32F407ZGT6使用SDIO方式驱动SD卡
R语言查看版本 R包查看版本
Esp32-c3 introductory tutorial basic part ⑫ - mass production burning device configuration and serial number, NVS partition confirmation, NVS partition generation program, CSV to bin
Qt嵌入子Qt程序窗口到当前程序
HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴