Java Annotation (Java Annotation) implementation code

Source: Internet
Author: User

If you want to know what java annotation is? For more information, see http://www.infoq.com/articles/annotation-hammer"

Below is a demo I made:

Project Structure:

Running effect:

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

Code Section:

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

Directly write our configuration information into the program... this will not parse the comment we wrote .."

But what are the advantages and disadvantages of configuration files such as annotation, xml, and properties ..

Personal Opinion: it is more convenient to write comments... it can improve development efficiency. It is useful to annotation frameworks, such as Hibernate, Struts, and Spring.

Back to the original topic, "What is the purpose? "--- Here is just a demo to let everyone know what annotation is like...

In the process, we seldom use our own defined annotations. If we do, this blog may be helpful. ^_^

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

/Java_annotation/src/com/b510/hongten/annotation/JDBCAnnotation. java
Copy codeThe Code is as follows :/**
*
*/
Package com. b510.hongten. annotation;

Import java. lang. annotation. incluented;
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
*/
@ Brief ented
@ 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 codeThe Code is 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 loading annotation completed ...");
}
}

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 codeThe Code is 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.exe cuteQuery ();
While (rs. next ()){
System. out. println ("id:" + rs. getInt (1) + "name:" + rs. getString (2) + "mail:" + rs. getString (3 ));
}
// Close the record set
If (rs! = Null ){
Try {
Rs. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}

// Close the Declaration
If (ps! = Null ){
Try {
Ps. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}

// Close the link object
If (conn! = Null ){
Try {
Conn. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
} Catch (Exception e ){
E. printStackTrace ();
}
}

}

Related Article

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.