java连接oracle数据库

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36771850/article/details/79968205

java连接oracle数据库

package student_DB;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
 * 加载驱动连接数据库类
 * 
 */
public class ConnectionDB {

    private static final String DRIVERS = "oracle.jdbc.driver.OracleDriver";
    private static final String URL = "jdbc:oracle:thin:@10.204.99.188:1521/XE";
    private static final String USER = "system"; //用户名
    private static final String PASSWORD = "123456"; //密码

    public ConnectionDB() {
        try {

            Class.forName(DRIVERS);     //1 --加载Oracle驱动程序           

        } catch (ClassNotFoundException e) {//找不到驱动程序类

            e.printStackTrace();
        }
    }

    /**
     * 建立数据库连接
     */
    //成功加载后,会将Driver类的实例注册到DriverManager类中。
    public static  Connection getConnection() {

        Connection connect = null;

        try {

            connect = DriverManager.getConnection(URL, USER, PASSWORD);//返回数据库连接对象

        } catch (SQLException e) {

            e.printStackTrace();
        }

        return connect;
    }

    /**
     * 关闭数据库连接
     */
    public static  void closeConnection(Connection connect) {
        try {

            if (connect != null)
                connect.close();

        } catch (SQLException e) {

            e.printStackTrace();
        }
    }
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页