简体   繁体   中英

Connection to Oracle Database with Java

I can't connect to my Oracle database server . This is the code :

import java.util.*;
import java.sql.*;
import java.io.IOException;

public class Knigi {
public static void main(String[] args) {


  String baza_DRIVER="oracle.jdbc.driver.OracleDriver";
  String baza_STRING="jdbc:oracle:thin:@localhost:1521:";
  String baza_USERNAME="knigi";
  String baza_PASSWORD="knigi";

  Locale.setDefault(Locale.ENGLISH); // Vazhno e bidejkji Oracle treba da znae kakvi poraki da pojavuva

  try {
      Driver Driver = (Driver)Class.forName(baza_DRIVER).newInstance(); 
      Connection Conn = DriverManager.getConnection(baza_STRING,baza_USERNAME,baza_PASSWORD);

      PreparedStatement Statement = Conn.prepareStatement("SELECT * FROM zhanrovi");
      ResultSet zhanrovi = Statement.executeQuery();
      boolean isEmpty = !zhanrovi.next();
      boolean hasData = !isEmpty;
      while (hasData) {
        System.out.println("Zhanr: "+zhanrovi.getString("ZH_IME"));

        PreparedStatement Statement2 = 
           Conn.prepareStatement("select * from knigi where ZH_BR = ?");
        Statement2.setInt(1,zhanrovi.getInt("ZH_BR"));
        ResultSet knigi = Statement2.executeQuery();
        boolean isEmpty2 = !knigi.next();
        boolean hasData2 = !isEmpty2;
        if (isEmpty2) {
          System.out.println("      - nema knigi");
        } else {
          System.out.println("      - Knigi:");
        };
        while (hasData2) {
          System.out.println(
            "                 " +
            knigi.getString("ISBN") +
            " - " +
            knigi.getString("NASLOV")+" ");
          hasData2=knigi.next();
        }
        knigi.close();
        hasData=zhanrovi.next();
      }
      zhanrovi.close();
      Conn.close();
    } catch (Exception e) {
      System.out.println(e);
    }
}

}

And I am getting this message:

java.sql.SQLException: Io exception: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=186646784)(ERR=12504)(ERROR_STACK=(ERROR=(CODE=12504)(EMFI=4))(ERROR=(CODE=12504)(EMFI=4))))

In school, this example works. Where is the problem??

Do you have your DB server running at your local machine at: localhost:1521?

Your school machine might have DB server running on it but your home machine simply doesn't.

The db connection URL "jdbc:oracle:thin:@localhost:1521:" is normally not hard coded. Point it to localhost almost always fail it when you run your code on a different machine.

Try this, might help

  jdbc:oracle:thin://<host>:<port>/<SERVICE_NAME>

service_name=SID of the database, most often ORCL.

cheers,

There are three major instances when you get such Connection refused error

  1. There is no database server running at the host:port you have specified ie at localhost:1521
  2. Password you have entered is wrong.
  3. The port you have specified is blocked by a firewall or you are behind some proxy that does not allow you to connect to that port.

You can manually try to connect to your database from command line and see if the connection is live.

Question has been closed ... I had to do a tunneling because my Faculty server has closed connection from out side the faculty network, also they told me that the password had been changed .... btw tnx for the answers they helped me to connect to my local server and access my local data base ..

Thanks again mates ^_^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM