java连接Oracle数据库的步骤

时间:2021-07-17 07:45:34
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.log4j.Logger;

public class Test1 {
    private static Logger logger=Logger.getLogger(Test1.class.getName());
    public static void main (String [] args){    
        Connection conn=null;
        //加载驱动
        try {
            Class.forName("oracle.jdbc.OracleDriver");
        } catch (ClassNotFoundException e) {
            logger.error(e);
        }
        //建立连接
        try {
            conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","java78","java78");
            System.out.println("建立连接成功!");
        } catch (SQLException e) {
            logger.error(e);
        }finally{
            //关闭连接
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    logger.error(e);
                }
                System.out.println("关闭连接成功!");
            }
        }
    }
}