黑马程序员技术交流社区

标题: java基础之---注解:JDBCUtils [打印本页]

作者: 周发建    时间: 2016-3-31 13:22
标题: java基础之---注解:JDBCUtils
(一)案例1JdbcUtils
@Retention(RetentionPolicy.RUNTIME)
@interface DbInfo{
        String driver();
        String url();
        String username();
        String password();
       
}
class JdbcUtils{
        private static String driver;
        private static String url;
        private static String username;
        private static String password;
        static{
                try {
                        Method method = JdbcUtils.class.getMethod("getConnetion", null);
                        DbInfo info = method.getAnnotation(DbInfo.class);
                       
                        driver = info.driver();
                        url = info.url();
                        username = info.username();
                        password = info.password();
                        System.out.println(driver);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
        @DbInfo(driver="com.mysql.jdbc.Driver",url="",username="",password="")
        public static Connection getConnetion(){
               
                return null;
        }
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2