请教大家,希望得以解决,谢谢
import java.sql.*;public class JDBCDemo {
  public static void main (String args[]) {
    try {
         Statement stmt;
     PreparedStatement pstmt;
     ResultSet rs;
         Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("加载驱动");
         String url = "jdbc:oracle:thin://localhost:1521:oraDB";
    
  // 得到与数据库的连接
  Connection con = DriverManager.getConnection(url,"scott","tiger");
  // 显示url和连接信息
  System.out.println("URL"+ url);
  System.out.println("Connection"+ con);   // 得到一个Statement对象
  stmt = con.createStatement();
  // 如果Demo表存在,则删除掉,否则抛出异常
  System.out.println("DROP TABLE Demo, if it exists.");
  try
    {
      stmt.executeUpdate("DROP TABLE Demo"); 
        }
    catch (Exception ex)
    {
   System.out.println(ex);
  System.out.println("No exitsting table to delete!");     }   //在数据库中创建一个表Demo
   stmt.executeUpdate("CREATE TABLE Demo ("+"test_id int,test_val char(15) not null)");
   System.out.println("表已创建");
  //在表中插入一些值
   stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(1,'One')");
   stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(2,'Two')");
   stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(3,'Three')");
   stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(3,'Foue')");
   stmt.executeUpdate("INSERT INTO Demo ("+" test_id, test_val) VALUES(3,'Five')");
       System.out.println("rows1: " + stmt.getMaxRows());  
  //得到另外一个Statement对象
   stmt = con.createStatement();
  //查询表Demo,并将结果存储在ResultSet对象rs中
   rs = stmt.executeQuery("SELECT * FROM Demo ORDER BY test_id");
     
      //显示表的所有记录
   System.out.println("Display all results:");
   System.out.println("rows: " + stmt.getMaxRows());
   while (rs.next())
   {
      int testId = rs.getInt("test_id");
      String testVal = rs.getString("test_val");
    System.out.println("test_id"+testId + "test_val" + testVal);
   }
  //更新表中test_val的值
        pstmt = con.prepareStatement("UPDATE Demo SET test_val = ? WHERE test_id = ?");
  //更新表中的第二条记录的test_val字段的值
  //填充UPDATE语句中的"?"
    pstmt.setString(1,"hello");
    pstmt.setInt(2,2);
        pstmt.executeUpdate();
   System.out.println("UPDATE row number 2 OK");
  //显示表中更新后的第二条记录
        stmt = con.createStatement();
    rs = stmt.executeQuery("SELECT * FROM Demo ORDER BY test_id");
    System.out.println("Display row 2:");
       while (rs.next())
    {
     int testId = rs.getInt("test_id");
     String testVal = rs.getString("test_val");
     System.out.println("test_id" + testId + "test_val" + testVal);
 
        }
    stmt.close();
    pstmt.close();
      con.close();
    } catch (Exception e) {
  e.printStackTrace();
    }
  }
}以下是我运行时抛出的异常:java.sql.SQLException: IO 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=150999279)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
     at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
     at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
     at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:333)
     at oracle.jdbc.driver.OracleConnection.(init)(OracleConnection.java:404)     at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:468)
     at java.sql.DriverManager.getConnection(DriverManager.java:525)
     at java.sql.DriverManager.getConnection(DriverManager.java:171)
     at JDBCDemo.main(JDBCDemo.java:27)
Press any key to contine...

解决方案 »

  1.   

    jdbc:oracle:thin:@squall:1521:oradb没连上,URL 不对,或许还有其他问题。
      

  2.   

    回doway(john):
      谢谢你的热心,一样的url在教室里我能连上的,但不知道为什么在家就连不上
      

  3.   

    把这句String url = "jdbc:oracle:thin://localhost:1521:oraDB";
    里的localhost改成具体的ip地址
      

  4.   

    up楼上的 把localhost改成要连接数据库的ip地址
    另 家里保证能连到这个数据库服务器
      

  5.   

    String url = "jdbc:oracle:thin://192.168.1.12:1521:oraDB";
    String url = "jdbc:oracle:thin://127.0.0.1:1521:oraDB";
    我改成这样,结果是一样的错误.
      

  6.   

    首先确定是否是连接错误。
    其次确定oracle是否打开。
    最后就是端口1521是否打开。
      

  7.   

    实验室与DB服务器是一个防火墙内吧?首先要确定是否有防火墙还要确定DBserver在防火墙内外是否IP一致还要确定在防火墙上数据库端口是否打开