title: Connection to Oracle Database with Java
body: <p>I can't connect to my <code>Oracle database server</code>. This is the code :</p>
  
  <pre><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);
      }
  }
  
  }
  </code></pre>
  
  <p>And I am getting this message:</p>
  
  <pre><code>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))))
  </code></pre>
  
  <p>In school, this example works. Where is the problem??</p>