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;databaseName=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.
Hello Lucas, it worked with your tip. Thank you so much for your help.
– Rogério Eduard Schaefer