首页 > 其他 > 详细

注解实例

时间:2014-05-07 02:11:49      阅读:381      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
package com.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
/**
 * 
 * @author cong
 * 数据库连接注解
 *
 */
@Target(value = {FIELD})//Target:此注解用到的地方有TYPE、FIELD、METHOD
@Retention(value = RUNTIME)//注解的运行环境
public @interface CustomConnection {
    String url() default "";
    String driverClass() default "";
    String username() default "";
    String password() default "";
}
bubuko.com,布布扣

 

bubuko.com,布布扣
注解处理
package
com.annotation.processor; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.DriverManager; import com.annotation.CustomConnection; @SuppressWarnings("unchecked")//不显示警告提示 public class CustomAnnotationProcessor { public static <T> void processor(Class<T> clazz) { try { //T t = clazz.newInstance(); T t = null; Field[] fields = clazz.getDeclaredFields();//获取T对象声明变量 Constructor<?>[] constructors = clazz.getDeclaredConstructors();//获取T对象的构造函数,因为构造函数的访问级别问题 for(Constructor<?> constructor : constructors) { constructor.setAccessible(true);//无论T对象的构造函数是什么访问级别都可以访问其方法 t = (T)constructor.newInstance(new Object[]{}); } for(Field field : fields) { if(field.isAnnotationPresent(CustomConnection.class)) { CustomConnection customConnection = field.getAnnotation(CustomConnection.class); String url = customConnection.url(); String driverClass = customConnection.driverClass(); String username = customConnection.username(); String password = customConnection.password(); Class.forName(driverClass); Connection connection = DriverManager.getConnection(url, username, password); field.setAccessible(true); field.set(t, connection); continue; } } } catch (Exception e) { e.printStackTrace(); } } }
bubuko.com,布布扣
bubuko.com,布布扣
package com.util;

import java.sql.Connection;

import com.annotation.CustomConnection;
/**
 * 
 * @author Administrator
 * 调用注解连接数据库
 *
 */
public class DBUtil {

    private DBUtil() {//构造方法
        
    }
    
    @CustomConnection(url = "jdbc:oracle:thin:@//192.168.40.29:1521/PDBMYORACLE",
            driverClass = "oracle.jdbc.driver.OracleDriver",
            username = "victop",
            password = "victop")
    private static Connection connection;
    
    public static Connection getConnection() {
        return connection;
    }
}
bubuko.com,布布扣

测试

bubuko.com,布布扣
package com.victop;

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

import com.annotation.processor.CustomAnnotationProcessor;
import com.util.DBUtil;

public class Test {
    
    public static void main(String[] args) {
        try {
            CustomAnnotationProcessor.processor(DBUtil.class);
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("select * from Users");
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                System.out.println(resultSet.getString("username"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
bubuko.com,布布扣

 

注解实例,布布扣,bubuko.com

注解实例

原文:http://www.cnblogs.com/lhconglhc/p/3712136.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!