Implementation code for Java Annotation (Java annotations)

Source: Internet
Author: User

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,

Write our configuration information directly to the program ... This will not parse the comments we wrote.

But what are the pros and cons of configuration files such as annotation and xml,properties?

Personal opinion: When writing notes, it is more convenient ... Can improve the efficiency of development. Useful to the framework of annotations, such as: hibernate,struts,spring, etc.

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

Process, we seldom use our own definition of the note (Annotation), if it is really used, then this blog may be helpful. ^_^

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

/**
*
*/
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
*/
@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

/**
*
*/
Package COM.B510.HONGTEN.JDBC;

Import com.b510.hongten.annotation.JDBCAnnotation;

@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 comment 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

/**
*
*/
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;

/**
*
*/
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));
}
To close a record set
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 linked objects
IF (conn! = null) {
try {
Conn.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
} catch (Exception e) {
E.printstacktrace ();
}
}

}

Implementation code for Java Annotation (Java annotations)

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.