当前位置:网站首页>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
边栏推荐
- Line generation (Gauss elimination method, linear basis)
- EMC surge
- 8 lines of code to achieve quick sorting, easy to understand illustrations!
- 以PolarDB为代表的阿里云数据库以跻身全球第一阵营
- LiveData源码赏析三 —— 常见问题
- Boost研究:Boost Log
- Introduction to China Mobile oneos development board
- Oceanbase installation Yum source configuration error and Solutions
- 【西安交通大学】考研初试复试资料分享
- Lucene全文检索工具包学习笔记总结
猜你喜欢
随机推荐
Database transactions
"New digital technology" completed tens of millions of yuan of a + round financing and built an integrated intelligent database cloud management platform
孔松(信通院)-数字化时代云安全能力建设及趋势
国内首批!阿里云云原生数据湖产品通过信通院评测认证
谁还记得「张同学」?
微信表情符号被写入判决书,你发的每个 emoji 都可能成为呈堂证供
wallys/600VX – 2×2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz QCA 9880
Xu Lei expressed three thanks for the most difficult 618 in 19 years
Pointdistiller: structured knowledge distillation for efficient and compact 3D detection
Win10 R package installation error: not installed in arch=i386
【重温经典C语言】~c语言中%x、%c、%d、%x等等等、c语言取地址符&的作用、C语言中的 联合体
Oracle NetSuite 助力 TCM Bio,洞悉数据变化,让业务发展更灵活
“新数科技”完成数千万元A+轮融资,造一体化智能数据库云管理平台
缓存雪崩和缓存穿透解决方案
ESP32-C3入门教程 基础篇⑫——量产烧写设备配置和序列号, NVS partition分区确认, NVS 分区生成程序, csv转bin
相对位置编码Transformer的一个理论缺陷与对策
Summer vacation study record
Mathematics (fast power)
wallys/3×3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
Esp32-c3 introductory tutorial basic part ⑪ - reading and writing non-volatile storage (NVS) parameters








