当前位置:网站首页>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
边栏推荐
- Esp32-c3 introductory tutorial basic part ⑫ - mass production burning device configuration and serial number, NVS partition confirmation, NVS partition generation program, CSV to bin
- 深入解析 Apache BookKeeper 系列:第四篇—背压
- There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
- Flutter 从零开始 004 按钮组件
- Oracle NetSuite 助力 TCM Bio,洞悉数据变化,让业务发展更灵活
- [IC5000 tutorial] - 01- use daqdea graphical debug to debug C code
- The operation and maintenance security gateway (Fortress machine) of Qiming star group once again won the first place!
- 1175. 质数排列
- Automatic database growth
- 100 important knowledge points that SQL must master: insert data
猜你喜欢

对象映射 - Mapping.Mapster

暑假学习记录

建立自己的网站(13)

1175. 质数排列

Alibaba cloud lifeifei: China's cloud database has taken the lead in many mainstream technological innovations abroad

What is erdma as illustrated by Coptic cartoon?

10 reasons for system performance failure

Win10 R package installation error: not installed in arch=i386

Multiparty Cardinality Testing for Threshold Private Set-2021:解读

如何通过GDB分析Native Crash
随机推荐
Flutter 从零开始 004 按钮组件
A man is a Book
10 reasons for system performance failure
14岁懂社会-《关于“工作的幸福”这件事儿》读书笔记
wallys/IPQ8074a/2x(4×4 or 8×8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD
10天学会flutter DAY10 flutter 玩转 动画与打包
Set up your own website (13)
100 important knowledge points that SQL must master: summary data
Handler source code analysis
Speech signal processing - Fundamentals (V): Fourier transform
Esp32-c3 introductory tutorial question ⑨ - core 0 panic 'ed (load access fault) Exception was unhandled. vfprintf. c:1528
A theoretical defect of relative position coding transformer and Its Countermeasures
Livedata source code appreciation III - frequently asked questions
Is the golden cycle of domestic databases coming?
微信表情符号被写入判决书,你发的每个 emoji 都可能成为呈堂证供
Pointdistiller: structured knowledge distillation for efficient and compact 3D detection
Shutter from zero 004 button assembly
Mathematics (fast power)
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
[applet practice series] Introduction to the registration life cycle of the applet framework page