Erro para carregar date do banco transformar em String e exibir [Resolvido]

2 respostas
Mapko

Estou tentando trazer do banco uma data e transformar em String,
em outra classe eu uso esse método e coloco o resultado em um jText.
Aqui o código caso alguém possa me ajudar:

Classe ConexaoBanco:
public String  selectData(int cod) throws SQLException {
        Statement pst = getConexao().createStatement();
        ResultSet rs  = pst.executeQuery("select from data where (codigo = " + cod + ")");
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
        Date data = rs.getDate("data"); 
        String DataString = df.format(data); 
        
        return DataString ;
    }
Classe app:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    
        ConexaoBanco conn = new ConexaoBanco();
        try {
            int i = Integer.parseInt(jdCod.getText());
            String fimdata = conn.selectData(i);
            jdRes.setText(fimdata);
        } catch (SQLException ex) {
            Logger.getLogger(app.class.getName()).log(Level.SEVERE, null, ex);
        }
 
    }

Erros
21/03/2008 02:27:07 testeimagem.app jButton3ActionPerformed
SEVERE: null
org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544569. Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, char 8
from
at org.firebirdsql.jdbc.AbstractStatement.executeQuery(AbstractStatement.java:169)
at org.firebirdsql.gds.GDSException: Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, char 8
from
at org.firebirdsql.jgds.GDS_Impl.readStatusVector(GDS_Impl.java:1761)
at org.firebirdsql.jgds.GDS_Impl.receiveResponse(GDS_Impl.java:1714)
at org.firebirdsql.jgds.GDS_Impl.isc_dsql_prepare(GDS_Impl.java:1226)
at org.firebirdsql.jca.FBManagedConnection.prepareSQL(FBManagedConnection.java:764)
at org.firebirdsql.jdbc.AbstractConnection.prepareSQL(AbstractConnection.java:950)
at org.firebirdsql.jdbc.AbstractStatement.prepareFixedStatement(AbstractStatement.java:1036)
at org.firebirdsql.jdbc.AbstractStatement.internalExecute(AbstractStatement.java:1023)
at org.firebirdsql.jdbc.AbstractStatement.executeQuery(AbstractStatement.java:156)

2 Respostas

marciofermino

Ola, veja se isso ajuda:

import java.util.Date;

SimpleDateFormat dataformatada = new SimpleDateFormat("dd/MM/yyyy");
            Date minhaDate = new Date();
            String grava_data = dataformatada.format(minhaDate);
            calendar1.setText(grava_data);
1.	Date para String , data
	dd/MM/yyyy 
DateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");
String texto = formatador.format(new Date());
	dd/MM/yyyy HH:mm 
DateFormat formatador = new SimpleDateFormat("dd/MM/yyyy HH:mm");
String texto = formatador.format(new Date());
1.	String para Date 
	dd/MM/yyyy 
DateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");
String texto = "15/04/1977";
Date data = formatador.parse(texto);
	dd/MM/yyyy HH:mm 
DateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");
String texto = "15/04/1977 21:30";
Date data = formatador.parse(texto);
Mapko

Ajudou sim..obrigado

fiz mais um teste e funcionou....
agora vou adaptar pro meu trabalho.....
ficou assim...

Conexão:
public String select() throws SQLException  {
         Statement st = getConexao().createStatement();
         ResultSet rs = st.executeQuery("select codigo, data from tdata");
         rs.next();
         Date data = rs.getDate("data"); 
         SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
         String DataString = sdf.format(data); 
        
         return DataString;
     }
Exibir:
ServicoBanco servico = new ServicoBanco();
     
     try {
     jData.setText(servico.select());
     } catch (Exception ex) {
         ex.printStackTrace();         
     }
Criado 21 de março de 2008
Ultima resposta 24 de mar. de 2008
Respostas 2
Participantes 2