当前位置:网站首页>自定义一个注解来获取数据库的链接
自定义一个注解来获取数据库的链接
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
边栏推荐
- Handler-源码分析
- ESP32-C3入门教程 基础篇⑪——Non-Volatile Storage (NVS) 非易失性存储参数的读写
- 创建型-配置工厂
- Key library function based on Hal Library
- 200000 bonus pool! [Alibaba security × ICDM 2022] the risk commodity inspection competition on the large-scale e-commerce map is in hot registration
- What is erdma as illustrated by Coptic cartoon?
- Kotlin 协程调度切换线程是时候解开谜团了
- [xi'anjiaotonguniversity] information sharing of the first and second postgraduate entrance examinations
- 【leetcode 16】三数之和
- 启明星辰集团运维安全网关(堡垒机)再次夺得榜首!
猜你喜欢

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

What is erdma as illustrated by Coptic cartoon?

Database connection pool Druid

国内首批!阿里云云原生数据湖产品通过信通院评测认证

10 reasons for system performance failure

R language de duplication operation unique duplicate filter

win10 R包安装报错:没有安装在arch=i386

R语言去重操作unique duplicate filter

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

HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴
随机推荐
Go language defer
A man is a Book
The first batch in China! Alibaba cloud native data Lake products have passed the evaluation and certification of the ICT Institute
HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴
Automatic database growth
Filter error in dplyr: can't transform a data frame with duplicate names
林克庆到番禺区调研“发展要安全”工作 以“时时放心不下”责任感抓好安全发展各项工作
中移OneOS开发板学习入门
揭秘得物客服IM全链路通信过程
PointDistiller:面向高效紧凑3D检测的结构化知识蒸馏
"New digital technology" completed tens of millions of yuan of a + round financing and built an integrated intelligent database cloud management platform
100 important knowledge points that SQL must master: join table
“新数科技”完成数千万元A+轮融资,造一体化智能数据库云管理平台
SQL必需掌握的100个重要知识点:汇总数据
Win10 R package installation error: not installed in arch=i386
Esp32-c3 introductory tutorial basic part ⑫ - mass production burning device configuration and serial number, NVS partition confirmation, NVS partition generation program, CSV to bin
以PolarDB为代表的阿里云数据库以跻身全球第一阵营
一个人就是一本书
Summer vacation study record
How to analyze native crash through GDB