Home  >  Article  >  Database  >  eclipse连接oracle 11g数据库图文教程

eclipse连接oracle 11g数据库图文教程

WBOY
WBOYOriginal
2016-06-07 16:22:451139browse

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。 1.首先先建立一个项目DB,右键点击DB,选择Build Path--Configure Build Path进入 通过Add External JARs..选择D:or

   Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。Eclipse 附带了一个标准的插件集,,包括Java开发工具(Java Development Kit,JDK)。

  1.首先先建立一个项目DB,右键点击DB,选择Build Path-->Configure Build Path进入

eclipse连接oracle 11g数据库图文教程 三联

  通过Add External JARs..选择D:orclapphrproduct11.2.0dbhome_1owbwflib下的ojdbc14.jar,点击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";这个url可能无效

  private String url = "jdbc:oracle:thin:@hr-PC:1521:orcl";hr-PC和1521很关键,也就是侦听串里有没有这个,在

  D:orclapphrproduct11.2.0dbhome_1NETWORKADMINtnsnames.ora这个文件里有没有,一定要根据tnsnames.ora这个文件来配置url路径,否则会出错!!!

eclipse连接oracle 11g数据库图文教程

  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("关闭con对象失败!");

  e.printStackTrace();

  }

  }

  public static void main(String[] args)

  {

  ConnectOracle c=new ConnectOracle();

  c.getCon();

  c.closed();

  }

  }

  3.运行结果:

eclipse连接oracle 11g数据库图文教程

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Oracle xml 转换Next article:ORACLE数据库常见问题汇总