Eclipse是一个基于Java的开源可扩展开发平台。Eclipse附带了一组标准插件,包括java开发工具包(jdk)。
1.首先建立一个项目DB,右键DB选择构建路径-配置构建路径进入。
By adding an external jar . select ojdbc14.jar under d: \ orcl \ app \ hr \ product \ 11.2.0 \ dbhome _ 1 \ owb \ wf \ lib, and click OK.
2.编写Connectoracle.java文件
package com.wuy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectOracle {
private Connection con;
private String user='scott';
//private String user='sys as sysdba';
private String password='18233188050';
private String className='oracle.jdbc.driver.OracleDriver';
//private string URL=' JDBC: Oracle: OCI @ localhost: 1158: orcl' This Uniform Resource Locator may not be valid.
私有字符串URL=' JDBC:Oracle:thin:@ HR-PC:1521:orcl 'Hr-PC和1521很重要,就是监听串里有没有这个,在
d:\ orcl \ app \ HR \ product \ 11。2 .0 \ dbhome _ 1 \ NETWORK \ ADMIN \ tnsnames。奥拉这个文件里有没有,
一定要根据tnsnames.ora文件配置url路径,否则会出错!
public ConnectOracle(){
try{
Class.forName(className);
System.out.println('数据库驱动程序加载成功!');
}catch(ClassNotFoundException e){
System.out.println('未能加载数据库驱动程序!');
e.printStackTrace();
}
}
/* *创建数据库连接*/
public Connection getcon(){
try {
con=DriverManager.getConnection(url,user,password);
System.out.println('数据库连接创建成功!');
} catch (SQLException e) {
System.out.print(con);
System.out.println('无法创建数据库连接!');
con=null;
e.printStackTrace();
}
return con;
}
public void closed(){
try{
if(con!=null){
con.close();
}
}catch(SQLException e){
System.out.println ('Failed to close intensive reading object!' );
e.printStackTrace();
}
}
public static void main(String[] args)
{
ConnectOracle c=new ConnectOracle();
c.getCon();
c.closed();
}
}
3.操作结果: