JDBC (本机Oracle连接)
本机连接Oracle流程:
首先,查看一下自己的计算机名:
进入Oracle配置文件Oracle\product\11.2.0\dbhome_1\NETWORK\ADMIN把host值修改为本机(只需修改host值)
listenter.ora文件:
# listener.ora Network Configuration File: D:\Oracle11g\product\11.2.0\dbhome_1\network\admin\listener.ora # Generated by Oracle configuration tools. SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (SID_NAME = CLRExtProc) (ORACLE_HOME = D:\Oracle11g\product\11.2.0\dbhome_1) (PROGRAM = extproc) (ENVS = "EXTPROC_DLLS=ONLY:D:\Oracle11g\product\11.2.0\dbhome_1\bin\oraclr11.dll") ) ) LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = 本机名)(PORT = 1521)) ) ) ADR_BASE_LISTENER = D:\Oracle11g
tnsnames.ora文件:
# tnsnames.ora Network Configuration File: D:\Oracle11g\product\11.2.0\dbhome_1\network\admin\tnsnames.ora # Generated by Oracle configuration tools. LISTENER_ORCL = (ADDRESS = (PROTOCOL = TCP)(HOST = 本机名)(PORT = 1521)) ORACLR_CONNECTION_DATA = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)) ) (CONNECT_DATA = (SID = CLRExtProc) (PRESENTATION = RO) ) ) ORCL= (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 本机名)(PORT = 1521)) (CONNECT_DATA = (SERVER = orcl) (SERVICE_NAME = orcl) ) )
(在导入了ojdbc.jar驱动的前提下)
JDBC测试:
package cn.library.hnucm.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBUtil { public static Connection getConnection() { //连接Oracle String url="jdbc:oracle:thin:@127.0.0.1:1521/orcl"; String username = "system"; String password = "Oracle11g"; String driver="oracle.jdbc.driver.OracleDriver"; try { Class.forName(driver); Connection con= DriverManager.getConnection(url, username, password); return con; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { // TODO Auto-generated method stub Connection con = DBUtil.getConnection(); System.out.println(con.toString()); } }
测试成功: