Java Operations Oracle Database Tools Class (JDBC)

Source: Internet
Author: User

Tag:jdbc    Connection database    

Package Com.util;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.preparedstatement;import Java.sql.resultset;import java.sql.sqlexception;/** * * 2015-3-5 PM 2:45:56 * *musicweb.util.dbutil * Connect database class */public Class Dbutil {private static String Driver = "Oracle.jdbc.driver.OracleDriver";p rivate String url = "Jdbc:oracle:thin: @lo Calhost:1521:orcl ";p rivate string user =" XXX "; user name of//oracle database private string pwd =" XXXXXX "; user password for//oracle database private PreparedStatement sta = null;private ResultSet rs = null;private Connection conn = null;/** * Load driver */static {try {Class. forname (driver);} catch (ClassNotFoundException e) {e.printstacktrace ();}} /** * @return Connection object */public Connection getconn () {try {conn = drivermanager.getconnection (URL, user, pwd);} catch (Sqlexce Ption e) {e.printstacktrace ();} Return conn;} /** * @param SQL * SQL statement Add, delete, modify * @param obj * parameter * @return */public int update (String sql, OBJEC T ... obj) {int count = 0;conn = Getconn (); try {sTA = conn.preparestatement (sql), if (obj! = null) {for (int i = 0; i < obj.length; i++) {Sta.setobject (i + 1, obj[i]);} }count = Sta.executeupdate ();} catch (SQLException e) {e.printstacktrace ();} finally{close ();} return count;}  /** * @param SQL SQL statement * @param obj parameter * @return data collection */public ResultSet Query (String sql,object...obj) {conn=getconn (); try {sta=conn.preparestatement (SQL), if (Obj!=null) {for (int i=0;i<obj.length;i++) {sta.setobject (i+1, Obj[i]);}} Rs=sta.executequery ();} catch (SQLException e) {e.printstacktrace ();} return RS;} /** * Close resource */public void Close () {try {if (rs! = null) {Rs.close ();}} catch (SQLException e) {e.printstacktrace ();} finall Y {try {if (STA! = null) {Sta.close ()}} catch (SQLException E2) {e2.printstacktrace ();} finally {if (conn! = null) {try { Conn.close ();} catch (SQLException e) {e.printstacktrace ();}}}}}

Java Operations Oracle Database Tools Class (JDBC)

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.