当前位置:网站首页>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
边栏推荐
- 国产数据库的黄金周期要来了吗?
- 记一次ViewPager + RecyclerView的内存泄漏
- 创建型-配置工厂
- ESP32-C3入门教程 问题篇⑨——Core 0 panic‘ed (Load access fault). Exception was unhandled. vfprintf.c:1528
- Multiparty cardinality testing for threshold private set-2021: Interpretation
- Wechat Emoji is written into the judgment, and every Emoji you send may become evidence in court
- 线代(高斯消元法、线性基)
- Flutter 从零开始 008 表单
- ARouter 最新问题合集
- Summer vacation study record
猜你喜欢

Esp32-c3 introductory tutorial question ⑨ - core 0 panic 'ed (load access fault) Exception was unhandled. vfprintf. c:1528

HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴

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

HMS core audio editing service 3D audio technology helps create an immersive auditory feast

以PolarDB为代表的阿里云数据库以跻身全球第一阵营

谁还记得「张同学」?

wallys/3×3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise

如何通过GDB分析Native Crash

Methods and usage of promise async and await

一个悄然崛起的国产软件,低调又强大!
随机推荐
60 个神级 VS Code 插件!!
EMC-浪涌
How to analyze native crash through GDB
Record the memory leak of viewpager + recyclerview once
数据库 事务
ESP32-C3入门教程 基础篇⑫——量产烧写设备配置和序列号, NVS partition分区确认, NVS 分区生成程序, csv转bin
Introduction to China Mobile oneos development board
AMS源码解析
暑假学习记录
1175. prime permutation
相对位置编码Transformer的一个理论缺陷与对策
科普达人丨漫画图解什么是eRDMA?
10 days to learn how to flutter Day10 flutter play animation and packaging
koa - 洋葱模型浅析
以PolarDB为代表的阿里云数据库以跻身全球第一阵营
100 important knowledge points that SQL must master: using table aliases
Summer vacation study record
Oceanbase installation Yum source configuration error and Solutions
100 important knowledge points that SQL must master: insert data
Introduction to game theory