Tentei reescrever tudo, fiz o download do projeto do professor e nada adiantou, continua o mesmo problema Acho q tenha relação com o fato de ele não estar encontrando o usuário que estou usando
//codigo da ConnectionFactory
package br.com.alura.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
public Connection recuperarConexao() throws SQLException {
return DriverManager
.getConnection
("jdbc:mysql://localhost/loja_virtual?useTimezone=true&serverTimezone=UTC",
"root", "minhasenha");
}
}
//-------------------------
//codigo testa listagem
package br.com.alura.jdbc;
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:mysql://localhost/loja_virtual?useTimezone=true&serverTimezone=UTC", "root", "minhasenha");
Statement stm = connection.createStatement();
boolean resultado = stm.execute("SELECT * FROM PRODUTO");
ResultSet rst = stm.getResultSet();
while(rst.next()) {
int id = rst.getInt("id");
String nome = rst.getString("nome");
String descricao = rst.getString("descricao");
System.out.println(id);
System.out.println(nome);
System.out.println(descricao);
}
}
}
//----------------------------------------
//codigo testa insercao
package br.com.alura.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestaInsercao {
public static void main(String[] args) throws SQLException {
ConnectionFactory factory = new ConnectionFactory();
Connection connection = factory.recuperarConexao();
Statement stm = connection.createStatement();
stm.execute("INSERT INTO PRODUTO (nome, descricao) VALUES ('Mouse', 'Mouse sem fio')"
, Statement.RETURN_GENERATED_KEYS);
ResultSet rst = stm.getGeneratedKeys();
while(rst.next()) {
Integer id = rst.getInt(1);
System.out.println("O id criado foi: " + id);
}
rst.close();
stm.close();
connection.close();
}
}
//-----------------------
a exception:
Exception in thread "main" java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:827)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:447)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:237)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251)
at com.mysql.cj.jdbc.admin.TimezoneDump.main(TimezoneDump.java:70)