5
respostas

Problema no código da aula 1

Estou com o problema no código.

package br.com.caelum.jdbc;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestaListagem {
    public static void main(String[] args) throws SQLException {
         Connection connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/loja-virtual", "SA", "");
            Statement statement = connection.createStatement();
            boolean resultado = statement.execute("select * from Produto");
            ResultSet resultSet = statement.getResultSet();
            while(resultSet.next()) {
                int id = resultSet.getInt("id");
                String nome = resultSet.getString("nome");
                String descricao = resultSet.getString("descricao");
                System.out.println(id);
                System.out.println(nome);
                System.out.println(descricao);
            }
            resultSet.close();
            statement.close();

            connection.close();
    }
}

estou com erro nas linhas 12,13,14 e 24. O banco de dados está criado e com as tabelas também.

5 respostas

Oi Thiago, qual o erro? Coloca aqui para a gente poder te ajudar mais.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: Type mismatch: cannot convert from java.sql.Statement to java.beans.Statement The method execute() in the type Statement is not applicable for the arguments (String) The method getResultSet() is undefined for the type Statement The method close() is undefined for the type Statement

at br.com.caelum.jdbc.TestaListagem.main(TestaListagem.java:12)

Oi Thiago, você importou o statement errado.. perceba que pegou do pacote java.beans, quando deveria pegar de java.sql.Statement.

Fiz isso, pareceu isso agora

Exception in thread "main" java.sql.SQLTransientConnectionException: java.net.ConnectException: Connection refused: connect
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source)
    at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
    at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at br.com.caelum.jdbc.TestaListagem.main(TestaListagem.java:12)
Caused by: org.hsqldb.HsqlException: java.net.ConnectException: Connection refused: connect
    at org.hsqldb.ClientConnection.openConnection(Unknown Source)
    at org.hsqldb.ClientConnection.initConnection(Unknown Source)
    at org.hsqldb.ClientConnection.<init>(Unknown Source)
    ... 6 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at org.hsqldb.server.HsqlSocketFactory.createSocket(Unknown Source)
    ... 9 more

e o código está assim:

package br.com.caelum.jdbc;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestaListagem {
    public static void main(String[] args) throws SQLException {
         Connection connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/loja-virtual", "SA", "");
            Statement statement = connection.createStatement();
            boolean resultado = statement.execute("select * from Produto");
            ResultSet resultSet = statement.getResultSet();
            while(resultSet.next()) {
                int id = resultSet.getInt("id");
                String nome = resultSet.getString("nome");
                String descricao = resultSet.getString("descricao");
                System.out.println(id);
                System.out.println(nome);
                System.out.println(descricao);
            }
            resultSet.close();
            statement.close();

            connection.close();
    }
}

Oi Thiago, tudo bem?

O servidor do banco de dados parece não ter sido iniciado.

Na versão mais nova do hsqldb, a classe é org.hsqldb.Server. Então executei o seguinte comando:

$ java -cp hsqldb.jar org.hsqldb.Server --dbname.0 loja-virtual --database.0 file:loja-virtual

Só lembrando que o comando assume que você está no diretório do projeto, onde está o jar do hsqldb (para alterar o diretório você deve utilizar o comando cd):

iniciando-servidor

Testa e vê se funciona? Abraço!