当前位置:网站首页>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
边栏推荐
- dplyr 中的filter报错:Can‘t transform a data frame with duplicate names
- HMS core audio editing service 3D audio technology helps create an immersive auditory feast
- 一瓶水引发的“战争”
- How to analyze native crash through GDB
- Database cascading operation
- 他是上海两大产业的第一功臣,却在遗憾中默默离世
- [applet practice series] Introduction to the registration life cycle of the applet framework page
- The operation and maintenance security gateway (Fortress machine) of Qiming star group once again won the first place!
- Livedata source code appreciation III - frequently asked questions
- 100 important knowledge points that SQL must master: using table aliases
猜你喜欢
Kongsong (ICT Institute) - cloud security capacity building and trend in the digital age
建立自己的网站(13)
1175. prime permutation
Mathematics (fast power)
Alibaba cloud database represented by polardb ranks first in the world
Summer vacation study record
PointDistiller:面向高效紧凑3D检测的结构化知识蒸馏
10 reasons for system performance failure
基于视觉的机器人抓取:从物体定位、物体姿态估计到平行抓取器抓取估计
EMC surge
随机推荐
A theoretical defect of relative position coding transformer and Its Countermeasures
8 lines of code to achieve quick sorting, easy to understand illustrations!
压缩状态DP位运算
揭秘得物客服IM全链路通信过程
重新理解oauth2.0协议进行联合登录
Introduction to game theory
Set up your own website (13)
一个人就是一本书
数据库 级联操作
什么是微信小程序,带你推开小程序的大门
[leetcode 239] sliding window
微信表情符号被写入判决书,你发的每个 emoji 都可能成为呈堂证供
他是上海两大产业的第一功臣,却在遗憾中默默离世
数学(快速幂)
100 important knowledge points that SQL must master: insert data
1175. prime permutation
科普达人丨漫画图解什么是eRDMA?
缓存雪崩和缓存穿透解决方案
Methods and usage of promise async and await
科普達人丨漫畫圖解什麼是eRDMA?