Implementation code for Java Annotation (Java annotations) _java

Source: Internet
Author: User

If you want to know what the Java annotation is? You can look at it first: "Http://www.infoq.com/articles/Annotation-Hammer"

Here is a demo I made:

Project structure:

Operation Effect:

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

Code section:

Note: Many people will consider the question, "What is the purpose of this?" We can make a configuration file (Xml,properties etc), not more convenient than this ... Or that

Write our configuration information directly to the program ... It's not going to parse the notes we wrote ...

But what are the pros and cons of annotation and xml,properties profiles?

Personal point of view: When writing notes, more convenient ... Can improve the efficiency of development. A framework useful to annotations, such as: hibernate,struts,spring, etc.

Back to the original topic,"What is the purpose of doing this?" "---here just to do a demo, let everyone know how annotation is a thing ...." In a lot of our developed

In the process, we rarely use our own defined annotations (Annotation), and if so, this blog may be helpful. ^_^

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

/java_annotation/src/com/b510/hongten/annotation/jdbcannotation.java

Copy Code code as follows:

/**
*
*/
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
Copy Code code as follows:

/**
*
*/
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 load annotation complete ...");
}
}

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
Copy Code code as follows:

/**
*
*/
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));
}
Turn off recordsets
if (Rs!= null) {
try {
Rs.close ();
catch (SQLException e) {
E.printstacktrace ();
}
}

Close declaration
if (PS!= null) {
try {
Ps.close ();
catch (SQLException e) {
E.printstacktrace ();
}
}

Close a linked object
IF (conn!= null) {
try {
Conn.close ();
catch (SQLException e) {
E.printstacktrace ();
}
}
catch (Exception e) {
E.printstacktrace ();
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.