程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> Java Annotation(Java 注解)的實現代碼

Java Annotation(Java 注解)的實現代碼

編輯:JAVA編程入門知識

如果你想知道java annotation是什麼?你可以先看看:“http://www.infoq.com/articles/Annotation-Hammer”

下面是我做的一個demo:

項目結構:

運行效果:

====================================================

代碼部分:

注:很多人會考慮這個問題,“這樣做的目的是什麼?我們可以做一個配置文件(xml,properties等),不是比這個跟方便...或者說

直接把我們的配置信息寫入程序...這樣也不會去解析我們寫的注釋..”

但是annotation和xml,properties等配置文件的優缺點是什麼呢..

個人觀點:寫注釋的時候,比較方便...可以提高開發的效率.有用到注釋的框架,如:Hibernate,Struts,Spring等

回到原話題,“這樣做的目的是什麼?“---這裡只是做一個demo,讓大家知道annotation是怎麼一回事兒....在很多我們開發的

過程中,很少用到我們自己定義的注釋(Annotation),如果真的用到了,那麼這篇blog也許就有幫助了..^_^

====================================================

/java_annotation/src/com/b510/hongten/annotation/JDBCAnnotation.java
代碼如下:

/**
  *
  */
 package com.b510.hongten.annotation;

 import java.lang.annotation.Documented;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;

 /**
  * JDBC annotation
  *
  * @author Hongten
  * @date 2013-4-10
  */
 @Documented
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.TYPE)
 public @interface JDBCAnnotation {

     String driver() default "com.mysql.jdbc.Driver";

     String dbName() default "";

     String encoding() default "UTF-8";

     String port() default "3306";

     String host() default "localhost";

     String userName() default "root";

     String password() default "";

 }

/java_annotation/src/com/b510/hongten/jdbc/JDBCUtil.java
代碼如下:

/**
  *
  */
 package com.b510.hongten.jdbc;

 import com.b510.hongten.annotation.JDBCAnnotation;

 /**
  * @author Hongten
  * @date 2013-4-12
  */
 @JDBCAnnotation(dbName = "db_lucene", port = "3306", host = "192.168.0.119", userName = "root", password = "root")
 public class JDBCUtil {

     private static String driver;
     private static String dbName;
     private static String encoding;
     private static String port;
     private static String host;
     private static String passwrod;
     private static String userName;
     private static String url;

     public void checkInterceptor(Class<?> cl) throws Exception {
         boolean flag = cl.isAnnotationPresent(JDBCAnnotation.class);
         if (flag) {
             JDBCAnnotation jdbcAnnotation = cl.getAnnotation(JDBCAnnotation.class);
             driver = jdbcAnnotation.driver();
             dbName = jdbcAnnotation.dbName();
             encoding = jdbcAnnotation.encoding();
             port = jdbcAnnotation.port();
             host = jdbcAnnotation.host();
             userName = jdbcAnnotation.userName();
             passwrod = jdbcAnnotation.password();
             url = "jdbc:mysql://" + host + ":" + port + "/" + dbName + "?characterEncoding=" + encoding;
             System.out.println("JDBCUtil加載注釋完成...");
         }
     }

     public JDBCUtil() {
         try {
             checkInterceptor(JDBCUtil.class);
         } catch (Exception e) {
             e.printStackTrace();
         }
     }

     public static String getDriver() {
         return driver;
     }

     public static void setDriver(String driver) {
         JDBCUtil.driver = driver;
     }

     public static String getDbName() {
         return dbName;
     }

     public static void setDbName(String dbName) {
         JDBCUtil.dbName = dbName;
     }

     public static String getEncoding() {
         return encoding;
     }

     public static void setEncoding(String encoding) {
         JDBCUtil.encoding = encoding;
     }

     public static String getPort() {
         return port;
     }

     public static void setPort(String port) {
         JDBCUtil.port = port;
     }

     public static String getHost() {
         return host;
     }

     public static void setHost(String host) {
         JDBCUtil.host = host;
     }

     public static String getPasswrod() {
         return passwrod;
     }

     public static void setPasswrod(String passwrod) {
         JDBCUtil.passwrod = passwrod;
     }

     public static String getUserName() {
         return userName;
     }

     public static void setUserName(String userName) {
         JDBCUtil.userName = userName;
     }

     public static String getUrl() {
         return url;
     }

     public static void setUrl(String url) {
         JDBCUtil.url = url;
     }

    
 }

/java_annotation/src/com/b510/hongten/jdbc/JDBCTest.java
代碼如下:

/**
  *
  */
 package com.b510.hongten.jdbc;

 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;

 /**
  *
  * @author Hongten</br>
  * @date 2012-7-16
  *
  */
 public class JDBCTest {
     @SuppressWarnings("static-access")
     public static void main(String[] args) {
         JDBCUtil jdbcUtil = new JDBCUtil();
         String sql = "select * from mymails";
         try {
             Class.forName(jdbcUtil.getDriver());
             Connection conn = DriverManager.getConnection(jdbcUtil.getUrl(), jdbcUtil.getUserName(), jdbcUtil.getPasswrod());
             PreparedStatement ps = conn.prepareStatement(sql);
             ResultSet rs = ps.executeQuery();
             while (rs.next()) {
                 System.out.println("id : " + rs.getInt(1) + " name : " + rs.getString(2) + " mail : " + rs.getString(3));
             }
             // 關閉記錄集
             if (rs != null) {
                 try {
                     rs.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
             }

             // 關閉聲明
             if (ps != null) {
                 try {
                     ps.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
             }

             // 關閉鏈接對象
             if (conn != null) {
                 try {
                     conn.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
     }

 }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved