Connect to oracle 11 c

Asked

Viewed 309 times

1

I am trying to connect on oracle 11 c and the connection is not performed. Gives error:

ES Error: Got Minus one from a read call >

I’m testing the query in a Servlet, I’m not searching for the form yet. Let’s see:

package connection; 

import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.SQLException;  

public class Connect {

    public static Connection conexao;
    private static String servidor = "jdbc:mysql://127.0.0.1:3128/";

    public static boolean conectar() {
        try {

            Class.forName("oracle.jdbc.driver.OracleDriver");

            conexao = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:3128:xe", "andre", "root");
            if (conexao == null) {
                return false;
            }
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

        return true;
    }

}

Code to query:

package hospede;  

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import connection.Connect;

//@WebServlet("/Hospede")
public class Hospede extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        testar();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    private static void testar() {
        boolean conectado=Connect.conectar();
        String query = "select * from hospede";
        Statement busca = null;
        try {
            if (Connect.conexao!=null) {
                busca = Connect.conexao.createStatement();
                ResultSet resultado = busca.executeQuery(query);
                System.out.println(resultado.getString("nome"));
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
} 

Note: On the line conexao = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:3128:xe", "andre", "root"); The xe is the type of service? The file I downloaded is ojdbc6.jar.

  • Hi André, let’s try to break the problem into smaller pieces. What error do you get when trying to connect? Can connect from some other client (e.g., SQL Developer) from your machine to the host?

  • !Anthony Accioly can connect to the oracle command screen.

  • Your "jdbc:oracle:thin:@host:3128:nome do serviço" is exactly like this in the program or you put host and nome do serviço here in substitution for what is true in your code to hide confidential details?

  • @Victor Stafusa nothing to hide. I didn’t know what this service is, so I didn’t put it in properly. I’ve already edited the code.

  • Try the following jdbc:oracle:thin:@//localhost:1521:xe (unless you have changed the standard door). Something else, instead of printing the getMessage() try to get the entire stack trace (e.printStackTrace()).

  • @Anthony Accioly the door I changed because I was using the 8080 and was giving conflict with the Tomcat.

  • André, http port and connection port are different. See the link of the comment above (you find the connection port in the file listener.ora).

Show 2 more comments

1 answer

2

First, Oracle XE uses the default port 1521. Port 3128 is normally used by Squid. Unless you have changed the door on purpose, this is probably not the correct number.

Do not confuse the port used by the database with the port used by Tomcat. These are totally different things.

The default Mysql port is 3306. The SQL Server is 1433. The Firebird is the 3050. The Postgresql is the 5432 or 5433.

I revised your class code Connect. If it will connect to Oracle, you do not need to have the Mysql connection string in the middle of it. Also, keep a single Connection in a static variable is bad programming practice, since this does not close the connection properly.

In addition, the method getConnection never returns null, then your method would never return false. Capture the SQLException just to give a System.out.println and then pretend the exception didn’t happen and return true in the same way is also a bad idea, since you make the method say that the connection was successful when in fact it failed. The correct in this case is to propagate the exception.

If the ClassNotFoundException, then your application is broken due to a classpath error. In this case, you can propagate the same error.

Here’s the revised code:

package connection;

import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.SQLException;  

public class Connect {

    private static final int porta = 1521;
    private static final String host = "127.0.0.1";
    private static final String servico = "xe";
    private static final String usuario = "andre";
    private static final String senha = "root"

    static {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public Connection static conectar() throws SQLException {
        return DriverManager.getConnection("jdbc:oracle:thin:@" + host + ":" + porta + ":" + servico, usuario , senha);
    }
}

Now it’s Servlet’s turn:

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import connection.Connect;

//@WebServlet("/Hospede")
public class Hospede extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        testar();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    private static void testar() {
        String query = "select * from hospede";
        try (
            Connection c = Connect.conectar();
            Statement busca = c.createStatement();
            ResultSet resultado = busca.executeQuery(query)
        ) {
            System.out.println(resultado.getString("nome"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Here, you can use the syntax Try-with-Resources java 7+ to ensure that its Connection, your Statement and its ResultSet will be closed properly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.