JDBC Java Web Connection and SQL Server 2014

1

I'm starting to develop a portal using JAVA Web and database in SQL Server 2014.

I've never worked with SQL Server and there's a problem that I can not 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 to my computer.

What's wrong here? Why do not you recognize the database tables?

Please, if anyone can help me.

    
asked by anonymous 31.07.2017 / 12:29

1 answer

1

Try to use USE [DatabaseName] before your select, this error is usually can be caused by two reasons:

  • Lack of user permissions used to access the account;
  • jdbc does not know which database to point to (I think it's your case);
31.07.2017 / 12:55