MS SQL Query for JSP

1

I'm creating a website based on the JSP language but I do not know how to return a query that I performed on MSSQL Server , it looks like this:

DECLARE @Nomes VARCHAR(8000) 
SELECT @Nomes = COALESCE(@Nomes + ', ', '') + nome
FROM usuarios
WHERE nome IS NOT NULL
PRINT @Nomes
  

Result : name1, name2, name3

I have the following code in the JSP page, I want it to return the same value as the query performed on MSSQL Server in a String

<%
Connection currentCon = null;
Statement st = null;
ResultSet rs = null;

try {
    currentCon = ConnectionManager.getConnection();
    String sql = "DECLARE @Nomes VARCHAR(8000) SELECT @Nomes = COALESCE(@Nomes + ', ', '') + nome FROM usuarios WHERE nome IS NOT NULL";
    st = currentCon.createStatement();
    rs = st.executeQuery(sql);

    String resultado = null;

    while(rs.next()) {
        resultado = rs.getString(1);
    }

    System.out.println(test);
} catch (Exception e) {
    System.out.println("Falha ao acessar o banco de dados: " + e);
}
%>
    
asked by anonymous 08.03.2017 / 14:48

0 answers