Java database connection method (6)

Source: Internet
Author: User
Import COM. xxx. config. configmanager; import COM. xxx. config. configs;/*** connection management class * @ author */public class connectionmanager {// Database Type Private Static int dbtype =-1; Private Static connectionfactory _ connfactory; /*** Oracle Database Type flag */public static int Oracle = 1;/*** MySQL database type flag */public static int MySQL = 2; /*** obtain database type * @ return Database Type flag */public static int getdbtype () {If (dbtype =-1) {string t Ype = configmanager. getkey (configkeys. dbtypekey); If ("oracle ". equals (type) {dbtype = ORACLE;} else if ("MySQL ". equals (type) {dbtype = MySQL;} elsedbtype = 0;} return dbtype;}/*** get the connectionfactory object based on the specified database configuration. * @ Return connectionfactory object */public static connectionfactory getconnectionfactory (configs conf) {connectionfactory FAC = NULL; string type = Conf. getconfig (configkeys. dbconntypekey); If ("JNDI ". equals (type) {FAC = new jndiconnectionfactory ();} else {string CLS = Conf. getconfig (configkeys. dbconnclasskey); If (CLS = NULL | Cls. length () = 0) {CLS = simpleconnectionfactory. class. getname ();} Cl Assloader Cl = connectionmanager. class. getclassloader (); try {FAC = (connectionfactory) Cl. loadclass (CLS ). newinstance ();} catch (exception ex) {ex. printstacktrace (); FAC = new simpleconnectionfactory () ;}} FAC. init (CONF); Return FAC;}/*** get the connectionfactory object and database operation entry according to the default database configuration. The default implementation class simpleconnectionfactory * @ return connectionfactory object */Public synchronized static connectionfactory getconnectionfactory () {If (_ connfactory = NULL) {configs conf = configmanager. getdefaconfigconfigs (); _ connfactory = getconnectionfactory (CONF);} return _ connfactory;} static class connectionclass; static connectionobject newconnectionobject () {If (connectionclass = NULL) {string CLS = configmana Ger. getdefaconfigconfigs (). getconfig ("connectionobjectclassname"); If (CLS! = NULL & Cls. length ()> 0) {classloader Cl = connectionmanager. class. getclassloader (); try {connectionclass = Cl. loadclass (CLS);} catch (exception ex) {}} if (connectionclass = NULL) {connectionclass = connectionobject. class; return New connectionobject () ;}try {return (connectionobject) connectionclass. newinstance () ;}catch (exception ex) {} return New connectionobject ();}}

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.