当前位置:网站首页>自定义一个注解来获取数据库的链接
自定义一个注解来获取数据库的链接
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
边栏推荐
猜你喜欢
Oracle NetSuite 助力 TCM Bio,洞悉数据变化,让业务发展更灵活
数字化不是试出来,而是蹚出来的|行知数字中国 × 富士康史喆
【西安交通大学】考研初试复试资料分享
“新数科技”完成数千万元A+轮融资,造一体化智能数据库云管理平台
达梦数据冲刺科创板,或成A股市场“国产数据库第一股”
Oceanbase installation Yum source configuration error and Solutions
Methods and usage of promise async and await
孔松(信通院)-数字化时代云安全能力建设及趋势
Qualcomm released the "magic mirror" of the Internet of things case set, and digital agriculture has become a reality
How to analyze native crash through GDB
随机推荐
Train an image classifier demo in pytorch [learning notes]
Livedata source code appreciation III - frequently asked questions
200000 bonus pool! [Alibaba security × ICDM 2022] the risk commodity inspection competition on the large-scale e-commerce map is in hot registration
Shutter from zero 004 button assembly
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
Automatic database growth
TypeScript ReadonlyArray(只读数组类型) 详细介绍
以PolarDB为代表的阿里云数据库以跻身全球第一阵营
从开源项目探讨“FPGA挖矿”的本质
10 reasons for system performance failure
Esp32-c3 introductory tutorial IOT part ⑤ - Alibaba cloud Internet of things platform espaliyun RGB LED practical mass production solution
考研这些“不靠谱”的经验有多害人?
Wechat Emoji is written into the judgment, and every Emoji you send may become evidence in court
Alibaba cloud database represented by polardb ranks first in the world
H3C switch emptying configuration
林克庆到番禺区调研“发展要安全”工作 以“时时放心不下”责任感抓好安全发展各项工作
Flutter start from scratch 008 form
100 important knowledge points that SQL must master: creating and manipulating tables
什么是微信小程序,带你推开小程序的大门
It's time for the kotlin coroutine to schedule thread switching to solve the mystery