Connect to Oracle Database

Source: Internet
Author: User

1. Write a connection directly in a fixed way in a Java class, for example:

 
Import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. sqlexception;/*** encapsulate Common Database Operations * @ author administrator **/public class dbutil {/*** get connection * @ return */public static connection getconnection () {connection conn = NULL; try {class. forname ("oracle. JDBC. driver. oracledriver "); string url =" JDBC: oracle: thin: @ 192.168.26.3: 1521: bjpowernode "; string username =" test "; string Password =" test "; Conn = drivermanager. getconnection (URL, username, password);} catch (classnotfoundexception e) {e. printstacktrace ();} catch (sqlexception e) {e. printstacktrace ();} return conn;} public static void main (string [] Arges) {system. out. println (dbutil. getconnection ());}}

2. Use the xml configuration file to flexibly connect to the database (easier to modify). For example:

Import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. sqlexception;/*** encapsulate Common Database Operations * @ author administrator **/public class dbutil {/*** get connection * @ return */public static connection getconnection () {connection conn = NULL; try {jdbcconfig = xmlconfigreader. getinstance (). getjdbcconfig (); Class. forname (jdbcconfig. getdrivername (); string url = jdbcconfig. geturl (); string username = jdbcconfig. getUserName (); string Password = jdbcconfig. getPassword (); Conn = drivermanager. getconnection (URL, username, password);} catch (classnotfoundexception e) {e. printstacktrace ();} catch (sqlexception e) {e. printstacktrace ();} return conn;} public static void main (string [] Arges) {system. out. println (dbutil. getconnection ());}}

Jdbcconfig. Java class (the tostring () method is also rewritten here)

/*** JDBC configuration information ** @ author administrator **/public class jdbcconfig {private string drivername; private string URL; private string username; private string password; Public String getdrivername () {return drivername;} public void setdrivername (string drivername) {This. drivername = drivername;} Public String geturl () {return URL;} public void seturl (string URL) {This. url = URL;} Public String GetUserName () {return username;} public void setusername (string username) {This. username = username;} Public String GetPassword () {return password;} public void setpassword (string password) {This. password = password;} @ overridepublic string tostring () {return this. getclass (). getname () + "{drivername:" + drivername + ", URL:" + URL + ", Username:" + username + ", password: "+ password + "}";}}

Xmlconfigreader. Java class (the singleton mode is used to read xml configuration information)

Import Java. io. inputstream; import Org. dom4j. document; import Org. dom4j. extends entexception; import Org. dom4j. element; import Org. dom4j. io. saxreader;/*** parse the sys-config.xml file in the standalone mode * @ Param ARGs */public class xmlconfigreader {// hungry (preloaded) // Private Static xmlconfigreader instance = new xmlconfigreader (); // Private xmlconfigreader () {//} // public static xmlconfigreader getinstance () {// return instance; // lazy (delayed loading) Private Static xmlconfigreader instance = NULL; // save JDBC-related configuration information private jdbcconfig = new jdbcconfig (); Private xmlconfigreader () {saxreader reader = new saxreader (); inputstream in = thread. currentthread (). getcontextclassloader (). getresourceasstream ("sys-config.xml"); try {document DOC = reader. read (in); // get the JDBC-related configuration element drivernameelt = (element) Doc. selectObject ("/config/DB-info/driver-name"); element urlelt = (element) Doc. selectObject ("/config/DB-info/url"); element usernameelt = (element) Doc. selectObject ("/config/DB-info/user-name"); element passwordelt = (element) Doc. selectObject ("/config/DB-info/password"); // set JDBC-related configuration jdbcconfig. setdrivername (drivernameelt. getstringvalue (); jdbcconfig. seturl (urlelt. getstringvalue (); jdbcconfig. setusername (usernameelt. getstringvalue (); jdbcconfig. setpassword (passwordelt. getstringvalue ();} catch (incluentexception e) {e. printstacktrace () ;}} public static synchronized xmlconfigreader getinstance () {If (instance = NULL) {instance = new xmlconfigreader ();} return instance ;} // return JDBC-related configuration information public jdbcconfig getjdbcconfig () {return jdbcconfig;} public static void main (string [] ARGs) {jdbcconfig = xmlconfigreader. getinstance (). getjdbcconfig (); system. out. println (jdbcconfig );}}

Sys-config.xml (profile)

 
<? XML version = "1.0" encoding = "UTF-8"?> <Config> <DB-Info> <driver-Name> oracle. JDBC. driver. oracledriver </driver-Name> <URL> JDBC: oracle: thin: @ 192.168.26.3: 1521: bjpowernode </URL> <user-Name> test </user-Name> <password> test </password> </DB-Info> </config>
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.