almarti
Janeiro 10, 2007, 1:45pm
#1
Fiz a seguinte instrução:
conexao cnx = new conexao();
ResultSet rs = cnx.ExecutaSQL(strSql);
ResultSetMetaData nCols = rs.getMetaData();
Porem, no ResultSetMetaData está dando o erro Unhandled exception type SQLException
O Que está errado?
Precisa colocar esse código dentro de um try/catch que pegue por SQLException.
almarti
Janeiro 10, 2007, 1:54pm
#3
Pode me dar um exemplo? O código completo é:
package classes;
import util.conexao;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
public class GetDataGrid
{
public String Gerar(String strSql)
{
conexao cnx = new conexao();
ResultSet rs = cnx.ExecutaSQL(strSql);
ResultSetMetaData nCols = rs.getMetaData();
String retorno;
retorno = "";
retorno = retorno + "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'> " + "\n";
retorno= retorno + "<script type='text/javascript' src='../../js/Bs_Misc.lib.js'></script> " + "\n";
retorno= retorno + "<script type='text/javascript' src='../../js/Bs_DataGrid.class.js'></script> " + "\n";
retorno= retorno + "<link rel='stylesheet' href='../../css/style1.css'> " + "\n";
retorno= retorno + "<script type='text/javascript'> " + "\n";
retorno= retorno + "function init() { " + "\n";
retorno= retorno + "myGrid = new Bs_DataGrid('myGrid'); " + "\n";
retorno= retorno + "myGrid.bHeaderFix = false; " + "\n";
retorno= retorno + "var header = new Array({text:'#'}, 'Foo', {text:'Bar', align:'right', width:50}); " + "\n";
retorno= retorno + "var data = new Array(); " + "\n";
int i = 0;
String strArray="";
while(rs.next())
{
for (i=0;i<(nCols.getColumnCount()+1);i++)
{
if (nCols.getColumnCount()==1)
strArray="'"+ nCols.getColumnName(i+1)+"'";
else{
if (i==nCols.getColumnCount())
strArray=strArray+"'"+ nCols.getColumnName(i+1)+"'";
else{
strArray=strArray+"'"+ nCols.getColumnName(i+1)+"',";
}
}
}
//Retorna o ponteiro do contador para zero
i=0;
retorno= retorno + "data[data.length] = new Array("+strArray+"); " + "\n";
}
retorno= retorno + "myGrid.setHeaderProps(header); " + "\n";
retorno= retorno + "myGrid.setData(data); " + "\n";
retorno= retorno + "myGrid.drawInto('myGridDiv'); " + "\n";
retorno= retorno + "} " + "\n";
retorno= retorno + "</script> " + "\n";
retorno= retorno + "</head> " + "\n";
retorno= retorno + " <body onLoad='init();'> " + "\n";
retorno= retorno + "<h1>JavaScript Bs_DataGrid Example 1</h1> " + "\n";
retorno= retorno + " <div id='myGridDiv'></div> " + "\n";
retorno= retorno + "</body> " + "\n";
cnx.FechaConexaoBD();
return retorno;
}
}
O método getMetaData() do ResultSet lança uma SQLException. Sendo a SQLException uma exceção checada (é do tipo Exception, mas não herda de RuntimeException) ela deve ser tratada ou declarada na assinatura do método que a utilize, por exemplo:
Tratando a exceção:
public String gerar(String strSql){
...
try{
...
ResultSetMetaData nCols = rs.getMetaData();
...
catch(SQLException e){
//Aqui vc trata a exceção do jeito que vc achar melhor
}
...
}
Repassando a exceção para um nível acima:
pubic String gerar(String strSql) throws SQLException{
...
ResultSetMetaData nCols = rs.getMetaData();
...
}
Mas nesse 2° caso vc terá que tratá-la futuramente quando for usar o método public String gerar(String) ou repassá-la novamente.
Espero ter ajudado!!
Flw aew e t+!!
almarti
Janeiro 10, 2007, 4:15pm
#5
Obrigado, funcionou. Se ajudar alguem, o código ficou assim: é uma grid dinamica que estou montando.
[code]
package classes;
import util.conexao;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
public class GetDataGrid
{
public String Gerar(String strSql)
{
try{
conexao cnx = new conexao();
ResultSet rs = cnx.ExecutaSQL(strSql);
ResultSetMetaData nCols = rs.getMetaData();
String retorno;
//System.out.print(nCols.getColumnCount());
retorno = "";
retorno = retorno + "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'> " + "\n";
retorno= retorno + "<html>" + "\n";
retorno= retorno + "<head>" + "\n";
retorno= retorno + "<script type='text/javascript' src='js/Bs_Misc.lib.js'></script> " + "\n";
retorno= retorno + "<script type='text/javascript' src='js/Bs_DataGrid.class.js'></script> " + "\n";
retorno= retorno + "<link rel='stylesheet' href='css/style1.css'> " + "\n";
retorno= retorno + "<script type='text/javascript'> " + "\n";
retorno= retorno + "function init() { " + "\n";
retorno= retorno + "myGrid = new Bs_DataGrid('myGrid'); " + "\n";
retorno= retorno + "myGrid.bHeaderFix = false; " + "\n";
int i = 0;
String strArray="";
String strTitulo="";
String tretorno="";
while(rs.next())
{
strTitulo="";
for (i=0;i<(nCols.getColumnCount());i++)
{
if (nCols.getColumnCount()==1)
{
strArray="'"+ rs.getString(nCols.getColumnName(i+1))+"'";
strTitulo = "{text:'" + nCols.getColumnName(i+1) + "', align:'center'}";
}
else{
if (i==nCols.getColumnCount()-1)
{
strArray=strArray+"'"+ rs.getString(nCols.getColumnName(i+1))+"'";
strTitulo = strTitulo + "{text:'" + nCols.getColumnName(i+1) + "', align:'center'}";
}
else{
strArray=strArray+"'"+ rs.getString(nCols.getColumnName(i+1))+"',";
strTitulo = strTitulo + "{text:'" + nCols.getColumnName(i+1) + "', align:'center'},";
}
}
}
//Retorna o ponteiro do contador para zero
tretorno = tretorno + "data[data.length] = new Array(" + strArray + "); " + "\n";
i=0;
strArray="";
}
retorno= retorno + "var header = new Array("+strTitulo+"); " + "\n";
retorno= retorno + "var data = new Array(); " + "\n";
retorno= retorno + tretorno;
retorno= retorno + "myGrid.setHeaderProps(header); " + "\n";
retorno= retorno + "myGrid.setData(data); " + "\n";
retorno= retorno + "myGrid.drawInto('myGridDiv'); " + "\n";
retorno= retorno + "} " + "\n";
retorno= retorno + "</script> " + "\n";
retorno= retorno + "</head> " + "\n";
retorno= retorno + " <body onLoad='init();'> " + "\n";
retorno= retorno + " <div id='myGridDiv'></div> " + "\n";
retorno= retorno + "</body> " + "\n";
cnx.FechaConexaoBD();
return retorno;
}
catch(Exception e)
{
return ""+ e;
}
}
}[/code]