[Resolvido] Montando uma tabela de um resultset

3 respostas
FernandoCartaxo

tou querdno montar uma tabela (html) de um resultset do meu banco
Só que está dando um erro e eu não tou conseguindo achar…
alguem pode me ajudar?

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<%			
	Class.forName("org.postgresql.Driver");
	String url = "jdbc:postgresql://localhost/dwbd";
	Connection c = DriverManager.getConnection(url,"postgres","321321");
	
	Statement st = conn.createStatement();
	ResultSet rs = st.executeQuery("Select * from ger_pessoa");
	st.close(); 
%>
<table width="50%" align="center" border="1" >
	<tr>
    	<td>Código </td>
        <td>Nome </td>
    </tr>
<% while rs.next() {
%>
	<tr>
    	<td> <%= rs.getInt("pes_cod") %> </td>
        <td> <%= rs.getString("pes_nome") %> </td>
    </tr>
<%
}
%>
</table>    
</body>

</html>

o erro é esse:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 
JSP FileName:/index.jsp
Java FileName:/C:/Arquivos de programas/Apache Software Foundation/Tomcat 5.5/webapps/dwbd/work//org/apache/jsp\index_jsp.java

An error occurred at line: 14 in the jsp file: /index.jsp
conn cannot be resolved
11: 	String url = "jdbc:postgresql://localhost/dwbd";
12: 	Connection c = DriverManager.getConnection(url,"postgres","321321");
13: 	
14: 	Statement st = conn.createStatement();
15: 	ResultSet rs = st.executeQuery("Select * from ger_pessoa");
16: 	st.close(); 
17: %>


Stacktrace:
	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:98)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:435)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:298)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:302)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.25 logs.
Apache Tomcat/5.5.25

Pelo que eu entendi, o erro está no resultset…
alguem pode me ajudar?

3 Respostas

C

O problema é que na linha 12 vc declarou a variavel “c” como uma Connection, mas na linha 14 ao invés de utilizar a variável “c” vc está utilizando a variável “conn” que não foi declarada.

FernandoCartaxo

É... esse erro eu já tinha resolvido... tinha me passado memso, tinha esquecido de vim aquir e postar...
agora tou com outro erro

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<%	
    try{		
		Class.forName("org.postgresql.Driver");
		String url = "jdbc:postgresql://localhost/dwbd";
		Connection c = DriverManager.getConnection(url,"postgres","321321");
	
		Statement st = c.createStatement();
		ResultSet rs = st.executeQuery("Select * from ger_pessoa");
		st.close(); 
	}catch (Exception e) {}
%>
	
<table width="50%" align="center" border="1" >
	<tr>
    	<td>Código </td>
        <td>Nome </td>
    </tr>
<% while ( rs.next() ) {
%>

	<tr>
    	<td> <%= rs.getInt("pes_cod") %> </td>
        <td> <%= rs.getString("pes_nome") %> </td>
    </tr>
<%
}
%>
</table>   

</body>

</html>

o erro:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 
JSP FileName:/index.jsp
Java FileName:/C:/Arquivos de programas/Apache Software Foundation/Tomcat 5.5/webapps/dwbd/work//org/apache/jsp\index_jsp.java

An error occurred at line: 28 in the jsp file: /index.jsp
rs cannot be resolved
25:     </tr>
26: 
27: 	<tr>
28:     	<td> <%= rs.getInt(1) %> </td>
29:         <td> <%= rs.getString(2) %> </td>
30:     </tr>
31: 

JSP FileName:/index.jsp
Java FileName:/C:/Arquivos de programas/Apache Software Foundation/Tomcat 5.5/webapps/dwbd/work//org/apache/jsp\index_jsp.java

An error occurred at line: 29 in the jsp file: /index.jsp
rs cannot be resolved
26: 
27: 	<tr>
28:     	<td> <%= rs.getInt(1) %> </td>
29:         <td> <%= rs.getString(2) %> </td>
30:     </tr>
31: 
32: </table>   


Stacktrace:
	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:98)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:435)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:298)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:302)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.25 logs.
Apache Tomcat/5.5.25

esse erro é no resultset, onde eu uso o rs dá o erro

alguem me ajuda de novo?

*** CORRIGIDO ***
eu fechava o Statment no inico, ai num rodava... isso que dar ficar copiando e colando

segue o código corrido

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<head>
<title>Teste</title>
</head>
<body>
<%	
	Class.forName("org.postgresql.Driver");
	String url = "jdbc:postgresql://localhost/dwbd";
	Connection c = DriverManager.getConnection(url,"postgres","321321");

	Statement st = c.createStatement();
	ResultSet result = st.executeQuery("Select pes_cod, pes_nome from ger_pessoa");
	String pes_nome = "";
	int pes_cod;

%>
<table width="50%" align="center" border="1" >
	<tr>
    	<td>Código </td>
        <td>Nome </td>
    </tr>
<% while ( result.next() ) {
%>

	<tr>
    	<td> <%= result.getInt("pes_cod") %> </td>
        <td> <%= result.getString("pes_nome") %> </td>
    </tr>
<%
}
%>
</table>  

</body>

</html>
R

Podia da erro no try antes do rs ser declarado e tem q cuidar para não deixar nada preso no BD!
Testa essa versão ai:

<%@ page contentType=text/html; charset=utf-8 language=java import=java.sql.* errorPage="" %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>”>

<html xmlns=“<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>”>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />

<title>Untitled Document</title>

</head>

<body>

[color=red]<table width=“50%” align=“center” border=“1” >

<tr>

<td>Código </td>

<td>Nome </td>

</tr>

</table>

<%

Connection c = null;

Statement st = null;

ResultSet rs = null;

try{

Class.forName(org.postgresql.Driver);

String url = jdbc:postgresql://localhost/dwbd;

c = DriverManager.getConnection(url,postgres,321321);
st = c.createStatement();   
    rs = st.executeQuery("Select * from ger_pessoa");

while ( rs.next() ) {
%>

&lt;tr&gt;   
    &lt;td&gt; &lt;%= rs.getInt("pes_cod") %&gt; &lt;/td&gt;   
    &lt;td&gt; &lt;%= rs.getString("pes_nome") %&gt; &lt;/td&gt;   
&lt;/tr&gt;

<%
}

}catch (Exception e) {
} finally {
if(c != null)

c.close();

if(st != null)

st.close();

if(rs != null)

rs.close();

}

%>   [/color]

</body>

</html>

Criado 24 de novembro de 2007
Ultima resposta 24 de nov. de 2007
Respostas 3
Participantes 3