如果你想知道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
1 /** 2 * 3 */ 4 package com.b510.hongten.annotation; 5 6 import java.lang.annotation.Documented; 7 import java.lang.annotation.ElementType; 8 import java.lang.annotation.Retention; 9 import java.lang.annotation.RetentionPolicy; 10 import java.lang.annotation.Target; 11 12 /** 13 * JDBC annotation 14 * 15 * @author Hongten 16 * @date 2013-4-10 17 */ 18 @Documented 19 @Retention(RetentionPolicy.RUNTIME) 20 @Target(ElementType.TYPE) 21 public @interface JDBCAnnotation { 22 23 String driver() default "com.mysql.jdbc.Driver"; 24 25 String dbName() default ""; 26 27 String encoding() default "UTF-8"; 28 29 String port() default "3306"; 30 31 String host() default "localhost"; 32 33 String userName() default "root"; 34 35 String password() default ""; 36 37 }
/java_annotation/src/com/b510/hongten/jdbc/JDBCUtil.java
1 /** 2 * 3 */ 4 package com.b510.hongten.jdbc; 5 6 import com.b510.hongten.annotation.JDBCAnnotation; 7 8 /** 9 * @author Hongten 10 * @date 2013-4-12 11 */ 12 @JDBCAnnotation(dbName = "db_lucene", port = "3306", host = "192.168.0.119", userName = "root", password = "root") 13 public class JDBCUtil { 14 15 private static String driver; 16 private static String dbName; 17 private static String encoding; 18 private static String port; 19 private static String host; 20 private static String passwrod; 21 private static String userName; 22 private static String url; 23 24 public void checkInterceptor(Class<?> cl) throws Exception { 25 boolean flag = cl.isAnnotationPresent(JDBCAnnotation.class); 26 if (flag) { 27 JDBCAnnotation jdbcAnnotation = cl.getAnnotation(JDBCAnnotation.class); 28 driver = jdbcAnnotation.driver(); 29 dbName = jdbcAnnotation.dbName(); 30 encoding = jdbcAnnotation.encoding(); 31 port = jdbcAnnotation.port(); 32 host = jdbcAnnotation.host(); 33 userName = jdbcAnnotation.userName(); 34 passwrod = jdbcAnnotation.password(); 35 url = "jdbc:mysql://" + host + ":" + port + "/" + dbName + "?characterEncoding=" + encoding; 36 System.out.println("JDBCUtil加载注释完成..."); 37 } 38 } 39 40 public JDBCUtil() { 41 try { 42 checkInterceptor(JDBCUtil.class); 43 } catch (Exception e) { 44 e.printStackTrace(); 45 } 46 } 47 48 public static String getDriver() { 49 return driver; 50 } 51 52 public static void setDriver(String driver) { 53 JDBCUtil.driver = driver; 54 } 55 56 public static String getDbName() { 57 return dbName; 58 } 59 60 public static void setDbName(String dbName) { 61 JDBCUtil.dbName = dbName; 62 } 63 64 public static String getEncoding() { 65 return encoding; 66 } 67 68 public static void setEncoding(String encoding) { 69 JDBCUtil.encoding = encoding; 70 } 71 72 public static String getPort() { 73 return port; 74 } 75 76 public static void setPort(String port) { 77 JDBCUtil.port = port; 78 } 79 80 public static String getHost() { 81 return host; 82 } 83 84 public static void setHost(String host) { 85 JDBCUtil.host = host; 86 } 87 88 public static String getPasswrod() { 89 return passwrod; 90 } 91 92 public static void setPasswrod(String passwrod) { 93 JDBCUtil.passwrod = passwrod; 94 } 95 96 public static String getUserName() { 97 return userName; 98 } 99 100 public static void setUserName(String userName) { 101 JDBCUtil.userName = userName; 102 } 103 104 public static String getUrl() { 105 return url; 106 } 107 108 public static void setUrl(String url) { 109 JDBCUtil.url = url; 110 } 111 112 113 }
/java_annotation/src/com/b510/hongten/jdbc/JDBCTest.java
1 /** 2 * 3 */ 4 package com.b510.hongten.jdbc; 5 6 import java.sql.Connection; 7 import java.sql.DriverManager; 8 import java.sql.PreparedStatement; 9 import java.sql.ResultSet; 10 import java.sql.SQLException; 11 12 /** 13 * 14 * @author Hongten</br> 15 * @date 2012-7-16 16 * 17 */ 18 public class JDBCTest { 19 @SuppressWarnings("static-access") 20 public static void main(String[] args) { 21 JDBCUtil jdbcUtil = new JDBCUtil(); 22 String sql = "select * from mymails"; 23 try { 24 Class.forName(jdbcUtil.getDriver()); 25 Connection conn = DriverManager.getConnection(jdbcUtil.getUrl(), jdbcUtil.getUserName(), jdbcUtil.getPasswrod()); 26 PreparedStatement ps = conn.prepareStatement(sql); 27 ResultSet rs = ps.executeQuery(); 28 while (rs.next()) { 29 System.out.println("id : " + rs.getInt(1) + " name : " + rs.getString(2) + " mail : " + rs.getString(3)); 30 } 31 // 关闭记录集 32 if (rs != null) { 33 try { 34 rs.close(); 35 } catch (SQLException e) { 36 e.printStackTrace(); 37 } 38 } 39 40 // 关闭声明 41 if (ps != null) { 42 try { 43 ps.close(); 44 } catch (SQLException e) { 45 e.printStackTrace(); 46 } 47 } 48 49 // 关闭链接对象 50 if (conn != null) { 51 try { 52 conn.close(); 53 } catch (SQLException e) { 54 e.printStackTrace(); 55 } 56 } 57 } catch (Exception e) { 58 e.printStackTrace(); 59 } 60 } 61 62 }