JDBC Java Web and SQL Server 2014 connection

Asked

Viewed 645 times

1

I am starting the development of a portal using JAVA Web and database in SQL Server 2014.

I have never worked with SQL Server and there is a problem that I am unable to find the solution.

Here’s the problem: The connection usually happens through this class:

public class ConexaoFactory {
private static String URL = "jdbc:sqlserver://localhost:1433;user=sa;password=XXXXXX;databa‌​seName=XXXXXX";

public static Connection getConnection() throws SQLException {
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
        return DriverManager.getConnection(URL);

    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

}

But when trying to make a request in the database, I have an error:

com.microsoft.sqlserver.jdbc.SQLServerException: Nome de objeto 'AGENDA' inválido.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1655)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:885)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:778)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:166)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeQuery(SQLServerStatement.java:677)
at br.com.portalpedsys.dao.UsuarioDAO.listar(UsuarioDAO.java:22)
at br.com.portalpedsys.test.TesteConexao.main(TesteConexao.java:16)

The request is being made from the following DAO class:

public class UsuarioDAO {
    public ArrayList<Usuario> listar() throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * FROM AGENDA");

        Connection conexao = ConexaoFactory.getConnection();

        PreparedStatement comando = conexao.prepareStatement(sql.toString());
        ResultSet resultado = comando.executeQuery();

        ArrayList<Usuario> fabArray = new ArrayList<Usuario>();
        while (resultado.next()) {
            Usuario usuario = new Usuario();
            usuario.setCgccpf(resultado.getString("cgccpf"));
            usuario.setNomrep(resultado.getString("nomrep"));
            fabArray.add(usuario);
        }
        return fabArray;
    }
}

The database is local, on my computer.

What’s wrong with this place? Why not recognize the database tables?

Please, if anyone can help me.

1 answer

1


Try using a USE [DatabaseName] before your select, this error is usually may be caused by two :

  • Lack of user permissions used to access the account;
  • jdbc doesn’t know which database to point to (I think that’s your case);
  • Hello Lucas, it worked with your tip. Thank you so much for your help.

Browser other questions tagged

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