how to resolve Got minus one from a read call in oracle 11g jdbc 7/14 jdk 1.7?

前端 未结 4 1511
旧巷少年郎
旧巷少年郎 2021-01-20 08:44

I am using netbeans and jdk 7 updt 9 with 1.7 and following is my code.

public class jd {
    public static void main(String[] args) throws ClassNotFoundExcep         


        
相关标签:
4条回答
  • 2021-01-20 09:12

    oracle.jdbc.driver.OracleDriver is deprecated. You will probably have better success by replacing it with oracle.jdbc.OracleDriver

    See: http://www.oracle.com/technetwork/database/enterprise-edition/111070-readme-083278.html and Difference between Oracle jdbc driver classes?

    0 讨论(0)
  • 2021-01-20 09:13

    DriverManager throws this error when it tries to get a connection with invalid URL. Make sure your URL is valid. Things to checkout:

    1. Port: Oracle db runs on 1521. (note: don't confuse yourself with webport of oracle, which can be any other port as in your URL 1158).
    2. DB name: Get the db name from Oracle Web UI: (home->adminIstration->aboutdatabase->settings)
    3. URL format: "jdbc:oracle:thin:@localhost:1521:YourDbName"
    0 讨论(0)
  • 2021-01-20 09:28

    I think the error is in the port number 1158 in this line:

        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1158:ORCL","system", "system");
    

    Normally you connect to an Oracle database using port 1521. Try replacing 1158 with 1521.

    Your database may be running the Enterprise Manager on port 1158. That's a web application that you access, often by navigating to https://localhost:1158/em. The Oracle database itself will typically be listening on port 1521.

    When using JDBC you need to connect direct to the database, not to some web application instead. The Oracle JDBC driver understands the proprietary binary protocol it uses to communicate with the database, but it doesn't understand the HTTP it gets back from the Enterprise Manager web application. Hence you get an odd network error. You can expect similar random network errors if you attempt to connect the JDBC driver to something else that isn't an Oracle database either.

    0 讨论(0)
  • 2021-01-20 09:30

    Try this code:

    OracleDataSource ods = new OracleDataSource();    
    ods.setUser(DB_USER));    
    ods.setPassword(DB_PASSWORD);    
    ods.setURL(jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservicename))));    
    // New AutoClosable syntax applicable to connection. This syntax will    
    // close the connection automatically    
    try (OracleConnection connection = (OracleConnection) (ods.getConnection())) {      
     // Statement and ResultSet are AutoClosable by this syntax   
     try (Statement statement = connection.createStatement()) {      
        try (ResultSet resultSet = statement.executeQuery("select sysdate from dual")) {        
          while (resultSet.next())          
               System.out.print("Today's Date is: " + resultSet.getString(1));
    
        }   
      }
    }
    
    0 讨论(0)
提交回复
热议问题