java操作Oracle

申明:个人学习总结,仅供参考,欢迎批评指正与交流。

/**
 *
 */
package com.XXX.dbclass;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.XXX.utils.Config;

/**
 * @author 
 *
 */
public class DateBaseClass {

    private Connection mConnection;
    private Statement mStatement;
    private String mUrl;
    private String mHost;
    private String mSid;
    private String mUser;
    private String mPassword;
    private Config mConfig;

    public DateBaseClass() {
        // TODO Auto-generated constructor stub
        initialize();
        connect();
    }

    private Connection connect() {
        // TODO Auto-generated method stub
        try {
            //1.加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //2.定义链接对象
            mConnection = DriverManager.getConnection(mUrl, mUser, mPassword);
            //3.创建用于执行静态sql语句的Statement对象
            mStatement = mConnection.createStatement();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }

    private void initialize() {
        // TODO Auto-generated method stub
        mConfig = new Config();
        mHost = mConfig.getHost();
        mSid = mConfig.getSid();
        mUser = mConfig.getUser();
        mPassword = mConfig.getPassword();
        mUrl = "jdbc:oracle:thin:@" + mHost + ":1521" + mSid;
    }

    private void update(String sql){
        try {
            mStatement.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            close();
        }
    }

    private void insert(String sql){
        try {
            mStatement.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            close();
        }
    }

    private void delete(String sql){
        try {
            mStatement.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            close();
        }
    }

    private ResultSet query(String sql){
        try {
            return mStatement.executeQuery(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            close();
        }
        return null;
    }

    public void close(){
        if (mConnection != null) {
            try {
                mConnection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (mStatement != null) {
            try {
                mStatement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

你可能感兴趣的:(oracle,java操作Oracle)