如题!~
有成功的连接经验的大侠来只要获得 Connection 就可以了。注意是  JDK1.6  和 Oracle 11g  的环境谢谢了。!~

解决方案 »

  1.   

    不就是驱动包的问题吗?难道还有别的不同之处???JDBC的接口不可能修改的吧!
      

  2.   

    我的代码如下,反正死活连不上,驱动用的是oracle 11g自带的驱动,ojdbc6.jar private static String driver = "oralce.jdbc.driver.OracleDriver";
    private static String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
    private static String user = "system";
    private static String password = "system"; public static Connection getConnection() {
    try {
    Class.forName(driver);
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    Connection conn = null;
    try {
    conn = DriverManager.getConnection(url, user, password);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return conn;
    }另外还有一点就是。。
    我看到JDBC/LIB文件夹中的  readme.txt有写。
    以下是我截取其中的部分内容:
    Installation
    ------------Please do not try to put multiple versions of the Oracle JDBC drivers
    in your CLASSPATH.  The Oracle installer installs the JDBC Drivers in
    the [ORACLE_HOME]/jdbc directory.
    Setting Up Your Environment
    ---------------------------On Windows platforms:
      - Add [ORACLE_HOME]\jdbc\lib\ojdbc5.jar to
        your CLASSPATH if you use JDK 1.5 or
        [ORACLE_HOME]\jdbc\lib\ojdbc6.jar if you use JDK 1.6.
      - Add [ORACLE_HOME]\jlib\orai18n.jar to your CLASSPATH if needed.
      - Add [ORACLE_HOME]\bin to your PATH if you are using the JDBC OCI
        driver.On Solaris/Digital Unix:
      - Add [ORACLE_HOME]/jdbc/lib/ojdbc5.jar to your CLASSPATH if you
        use JDK 1.5 or [ORACLE_HOME]/jdbc/lib/ojdbc6.jar if you use JDK 1.6
      - Add [ORACLE_HOME]/jlib/orai18n.jar to your CLASSPATH if needed.
      - Add [ORACLE_HOME]/jdbc/lib to your LD_LIBRARY_PATH if you use
        the JDBC OCI driver.On HP/UX:
      - Add [ORACLE_HOME]/jdbc/lib/ojdbc5.jar to your CLASSPATH if you
        use JDK 1.5 or [ORACLE_HOME]/jdbc/lib/ojdbc6.jar if you use JDK 1.6
      - Add [ORACLE_HOME]/jlib/orai18n.jar to your CLASSPATH if needed.
      - Add [ORACLE_HOME]/jdbc/lib to your SHLIB_PATH and LD_LIBRARY_PATH
        if you use the JDBC OCI driver.On AIX:
      - Add [ORACLE_HOME]/jdbc/lib/ojdbc5.jar to your CLASSPATH if you
        use JDK 1.5 or [ORACLE_HOME]/jdbc/lib/ojdbc6.jar if you use JDK 1.6
      - Add [ORACLE_HOME]/jlib/orai18n.jar to your CLASSPATH if needed.
      - Add [ORACLE_HOME]/jdbc/lib to your LIBPATH and LD_LIBRARY_PATH
        if you use the JDBC OCI driver.
    Some Useful Hints In Using the JDBC Drivers
    -------------------------------------------Please refer to "JDBC Developer's Guide and Reference" for details
    regarding usage of Oracle's JDBC Drivers.  This section only offers
    useful hints.  These hints are not meant to be exhaustive.These are a few simple things that you should do in your JDBC program: 1. Import the necessary JDBC classes in your programs that use JDBC.
        For example:      import java.sql.*;
          import java.math.*; // if needed    To use OracleDataSource, you need to do:
          import oracle.jdbc.pool.OracleDataSource; 2. Create an OracleDataSource instance.       OracleDataSource ods = new OracleDataSource(); 3. set the desired properties if you don't want to use the
        default properties. Different connection URLs should be
        used for different JDBC drivers.      ods.setUser("my_user");
          ods.setPassword("my_password");    For the JDBC OCI Driver:
          To make a bequeath connection, set URL as:
          ods.setURL("jdbc:oracle:oci:@");      To make a remote connection, set URL as:
          ods.setURL("jdbc:oracle:oci:@<database>");      where <database> is either a TNSEntryName 
          or a SQL*net name-value pair defined in tnsnames.ora.
     
        For the JDBC Thin Driver, or Server-side Thin Driver:
          ods.setURL("jdbc:oracle:thin:@<database>");      where <database> is either a string of the form
          //<host>:<port>/<service_name>, or a SQL*net name-value pair,
          or a TNSEntryName.    For the JDBC Server-side Internal Driver:
          ods.setURL("jdbc:oracle:kprb:");      Note that the trailing ':' is necessary. When you use the 
          Server-side Internal Driver, you always connect to the
          database you are executing in. You can also do this:      Connection conn =
            new oracle.jdbc.OracleDriver().defaultConnection(); 4. Open a connection to the database with getConnection()
        methods defined in OracleDataSource class.      Connection conn = ods.getConnection();
    -----------------------------------------------------------------------看后我又这么写了代码,还是不行哎。。!~
    private static String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
    private static String user = "dell";
    private static String password = "dell";

    public static void main(String[] args) {
    Connection conn =null;
    try {
    OracleDataSource ods = new OracleDataSource();
    ods.setUser(user);
    ods.setPassword(password);
    ods.setURL(url);
    conn = ods.getConnection();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    System.out.println(conn);
    }
      

  3.   


    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;public class ConnHelper {
       private Connection con;
       private Statement stmt;
       private ResultSet rs; public Connection getConn() throws ClassNotFoundException, SQLException
    {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    String url = "jdbc:oracle:thin:@localhost:1521:orcl";
     con = DriverManager.getConnection(url,"admin","admin");
    return con;
    }

    public Statement getStatement() throws ClassNotFoundException, SQLException
    {
    if(con==null)
    this.con=this.getConn();
     stmt=con.createStatement();
    return stmt;

    }

    public ResultSet getResult(String sql) throws SQLException
    {
     rs=stmt.executeQuery(sql);
     return rs;
    }
    public void doExecute(String sql)
    {

    }

    }
    注意驱动是:ojdbc6.jar
      

  4.   

    我更正一下上面最后的代码第一行URL是这样写的。
    private static String url = "jdbc:oracle:thin:@localhost:1521/ORCL"; 
      

  5.   

    4楼的,奇怪了,你能连上吗?
    为什么我不能呢??
    我第一个就报了2个异常。1.
    java.lang.ClassNotFoundException: oralce.jdbc.driver.OracleDriver2.
    java.sql.SQLException: Listener refused the connection with the following error:
    ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
    The Connection descriptor used by the client was:
    localhost:1521:ORCL
      

  6.   

    反正我的sqlplus 是能登陆进去进行操作。
    就是用其他的方法连就连不上。
    包括 oracle SQL  developer
      

  7.   

    这帖子是08年的``
    但我现在正好遇到这种情况``一直无法连接``
    出现下面这些异常``琢磨了好久``还是没什么头绪``只能发这边来了`
    java.sql.SQLRecoverableException: Io 异常: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:101)
    at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:173)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:229)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:458)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:411)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:490)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:202)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:465)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at ConnHelper.getConn(ConnHelper.java:15)
    at TestJDBC.main(TestJDBC.java:10)