Use myeclipse to connect to the Oracle database through JDBC, JDBC is one of the J2EE standards, the following uses an instance to describe the connection process, and compare to use ADO. Net to connect to sqlserver.
Preparedstatement the preparedstatement object is pre-compiled and contains compiled SQL statements. Therefore, the execution efficiency is high, and a complete set of parameter setting methods are provided, combining the connection and the placeholder SQL statement, you can complete common database operations. The similarities and differences between preparedstatement and statement will be discussed later. The following describes how to operate a database by adding a user instance. Configuration File
For flexible configuration, the database connection content must be decoupled from the persistent layer class.
<?xml version="1.0" encoding="UTF-8"?><config><db-info><driver-name>oracle.jdbc.driver.OracleDriver</driver-name><url>jdbc:oracle:thin:@localhost:1521:bjpowern</url><user-name>drp1</user-name><password>drp1</password></db-info></config>
The entity encapsulates database fields for convenient call and transfer.
/*** 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 + "}";}}
Read configuration specifically writes a class for reading the configuration file. Here we use the database connection information as an example.
/*** Resolve the sys-config.xml file in single-sample mode * @ author administrator **/public class xmlconfigreader {// lazy (delayed loading lazy) 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 JDBC-related configuration information 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 configuration * @ return */Public jdbcconfig getjdbcconfig () {return jdbcconfig;} // public static void main (string [] ARGs) {jdbcconfig = xmlconfigreader. getinstance (). getjdbcconfig (); system. out. println (jdbcconfig );}}
Encapsulate common database operations, similar to sqlhelper, to extract common functions in the persistent layer.
/*** Encapsulate Common Data 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 (); Conn = drivermanager. getconnection (jdbcconfig. geturl (), jdbcconfig. getUserName (), jdbcconfig. getPassword ();} catch (classno Tfoundexception e) {e. printstacktrace ();} catch (sqlexception e) {e. printstacktrace () ;}return conn ;}// close the database connection public static void close (connection conn) {If (Conn! = NULL) {try {Conn. close ();} catch (sqlexception e) {e. printstacktrace () ;}}// close statementpublic static void close (preparedstatement pstmt) {If (pstmt! = NULL) {try {pstmt. close ();} catch (sqlexception e) {e. printstacktrace () ;}}// close the result set public static void close (resultset RS) {If (RS! = NULL) {try {Rs. close ();} catch (sqlexception e) {e. printstacktrace () ;}}// server test public static void main (string [] ARGs) {system. out. println (dbutil. getconnection ());}}
Add users to assign values to parameters using preparedstatement.
/*** Add User * @ Param user */Public void adduser (User user) {string SQL = "insert into t_user (user_id, user_name, password, contact_tel, email, create_date) "+" values (?, ?, ?, ?, ?, ?) "; Connection conn = NULL; preparedstatement pstmt = NULL; try {conn = dbutil. getconnection (); pstmt = Conn. preparestatement (SQL); pstmt. setstring (1, user. getuserid (); pstmt. setstring (2, user. getUserName (); pstmt. setstring (3, user. getPassword (); pstmt. setstring (4, user. getcontacttel (); pstmt. setstring (5, user. getemail (); pstmt. settimestamp (6, new timestamp (New date().gettime(~~~~pstmt.exe cuteupdate ();} catch (sqlexception e) {e. printstacktrace ();} finally {dbutil. close (pstmt); dbutil. close (conn );}}
Summary: JDBC is rich in content. Here we only use basic operations as an example to describe how to combine them. In fact, JDBC and ADO. net, ADO, database operations are similar: Obtain database connections based on the database connection string, account, and password; enter the SQL naming statement to be operated; supplement the required parameters; execute. Contrast learning to build a knowledge network.
For more DRP blogs, visit DRP project summary.