1.自定一个注解,注意参数
![这里写图片描述](https://img-blog.csdn.net/20151224234859739)
2.在工具类中调用注解,获取注解中的数据
public class JDBCUtils {
@MyDriver(driver="com.mysql.jdbc.Driver",url="jdbc:mysql:///estore",user="root",pwd="abc")
public static Connection getConnection() throws Exception{
Class clazz = JDBCUtils.class;
Method method = clazz.getMethod("getConnection", null);
MyDriver mydriver = method.getAnnotation(MyDriver.class);
String driver = mydriver.driver();
String url = mydriver.url();
String user = mydriver.user();
String pwd = mydriver.pwd();
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, pwd);
return conn;
}
}