当前位置:网站首页>自定义一个注解来获取数据库的链接
自定义一个注解来获取数据库的链接
2022-06-30 11:06:00 【全栈程序员站长】
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{ //获取字节码文件 Class clazz = JdbcUtils.class; //获取getConnection() Method method = clazz.getMethod(“getConnection”, null); if(method.isAnnotationPresent(JdbcInfo.class)){//判断方法上是否有注解 //获取注解 JdbcInfo jdbcInfo = method.getAnnotation(JdbcInfo.class); //获取四大参数 String driverClass = jdbcInfo.driverClass(); String url = jdbcInfo.url(); String username = jdbcInfo.username(); String password = jdbcInfo.password(); //注册驱动 Class.forName(driverClass); Connection connection = DriverManager.getConnection(url, username, password); //获取连接 return connection; } return null; } public static void main(String[] args) throws Exception { System.out.println(getConnection()); } }
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/100805.html原文链接:https://javaforall.cn
边栏推荐
- 优惠券种类那么多,先区分清楚再薅羊毛!
- 如何通过GDB分析Native Crash
- What is erdma as illustrated by Coptic cartoon?
- 100 important knowledge points that SQL must master: using table aliases
- [understanding of opportunity -34]: fate is within the light cone
- 线代(高斯消元法、线性基)
- SQL必需掌握的100个重要知识点:使用表别名
- 数据库 事务
- 14岁懂社会-《关于“工作的幸福”这件事儿》读书笔记
- 8 lines of code to achieve quick sorting, easy to understand illustrations!
猜你喜欢

R language view version R package view version

记一次ViewPager + RecyclerView的内存泄漏

“新数科技”完成数千万元A+轮融资,造一体化智能数据库云管理平台

启明星辰集团运维安全网关(堡垒机)再次夺得榜首!

如何通过GDB分析Native Crash

dplyr 中的filter报错:Can‘t transform a data frame with duplicate names

Alibaba cloud database represented by polardb ranks first in the world

科普达人丨漫画图解什么是eRDMA?

中移OneOS开发板学习入门

A theoretical defect of relative position coding transformer and Its Countermeasures
随机推荐
Handler-源码分析
The reasoning delay on iphone12 is only 1.6 MS! Snap et al. Analyzed the transformer structure latency in detail, and used NAS to find out the efficient network structure of mobile devices
19年来最艰难的618,徐雷表达三个谢意
Shutter from zero 004 button assembly
100 important knowledge points that SQL must master: creating and manipulating tables
LED driver library based on Hal Library
The life, working principle and application of electrochemical oxygen sensor
The first batch in China! Alibaba cloud native data Lake products have passed the evaluation and certification of the ICT Institute
R语言查看版本 R包查看版本
Introduction to China Mobile oneos development board
The first China Digital Collection conference will be held soon
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
什么是微信小程序,带你推开小程序的大门
[leetcode 239] sliding window
中移OneOS开发板学习入门
A theoretical defect of relative position coding transformer and Its Countermeasures
ESP32-C3入门教程 基础篇⑫——量产烧写设备配置和序列号, NVS partition分区确认, NVS 分区生成程序, csv转bin
AMS source code analysis
It's time for the kotlin coroutine to schedule thread switching to solve the mystery
国内首批!阿里云云原生数据湖产品通过信通院评测认证