把数据库的url里的IP地址部分写上数据库的ip地址就OK

解决方案 »

  1.   

    class Connjaca
    {
      static Connection conn;
      static void conn_oracle() throws SQLException{
       // Load the Oracle JDBC driver  
       // 显式注册
       DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
       // 隐式注册
    //   Class.forName(oracle.jdbc.driver.OracleDriver);    
       String url = "jdbc:oracle:thin:username/[email protected]:1521:ORCL";
       System.out.println("开始连接ORACLE;");
       conn =DriverManager.getConnection (url);// Connect to the database    // You can put a database name after the @ sign in the connection URL.  
      }
      
      public static void main (String args [])
           throws SQLException
      {
       //连接ODBC
         //conn_odbc();
        //连接ORACLE
         conn_oracle();
        System.out.println("连接数据库成功;");
        // Create a Statement
        Statement stmt = conn.createStatement ();//创建一个声明
        //单结果集
        ResultSet rset = stmt.executeQuery ("select psname from tblname where colname='aa'");
        // Iterate through the result and print the employee names
        while (rset.next ()){//单结果集:通过字段索引,输出一个值
          System.out.println (rset.getString (1));
        }
      }
    }