Make connection in oracle 11c

1

I'm trying to connect to oracle 11c and the connection is not made. Give the error:

  

ES Error: Got one from a read call

I'm testing the query in a servlet, I'm still not looking for the form. 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 do the 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 line conexao = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:3128:xe", "andre", "root"); What xe is the type of service? The file I downloaded is ojdbc6.jar.

    
asked by anonymous 15.11.2016 / 15:54

1 answer

2

First, Oracle XE uses the 1521 port by default. . Port 3128 is typically used by Squid . Unless you've 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. They are totally different things.

The default MySQL port is 3306 . A in SQL Server is 1433 . Firebird's is 3050 . The one for PostgreSQL is 5432 or 5433 .

I gave a revision in the code of your class Connect . If it will connect to Oracle, you do not need to have the MySQL Connection String in the middle of it. Also, keeping a single Connection in a static variable is a bad programming practice, as this does not close the connection properly.

Furthermore, the getConnection method never returns null , so its method would never return false . Capturing SQLException just to give System.out.println and then pretending that the exception did not happen and return true likewise is a lousy idea, since you make the method say that the connection was successful when in fact she failed. The correct in this case is to propagate the exception.

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

Here is 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 the 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 try-with-resources syntax for Java 7+ to ensure that your Connection , Statement and ResultSet will be properly closed.

    
15.11.2016 / 19:15