1.自定一個註解,注意參數
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;
}
}