Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
A conexao com o BD está dando erro no metodo listarDisciplina(), mas está correto no metodo cadastrarDisciplina():
import java.sql.SQLException;
import java.sql.*;
public class Conexao
{
protected Connection getConnection() throws Exception
{
String driverName = "org.postgresql.Driver";
String databaseURL = "jdbc:postgresql://localhost/postgres";
String user = "postgres";
String pass = "postgres";
Class.forName(driverName);
return DriverManager.getConnection(databaseURL, user, pass);
}
}
import java.sql.SQLException;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;
public class Disciplina extends Conexao{
private int coddisc;
private String nome;
private int ncreditos;
//gets e sets
public void cadastrarDisciplina(Disciplina u){
try {
Connection conn = getConnection();
String sql = "INSERT INTO disciplina VALUES (?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, u.getCoddisc());
ps.setString(2, u.getNome());
ps.setInt(3, u.getNcreditos());
ps.executeUpdate();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
public List<Disciplina> listarDisciplina() {
List<Disciplina> lista = new LinkedList<Disciplina>();
try {
Connection con = getConnection();
Statement statement = con.createStatement();
String query = "SELECT CODDISC,NOME,NCREDITOS FROM DISCIPLINA";
ResultSet results = statement.executeQuery(query);
while (results.next()) {
Integer cod = results.getInt("CODDISC");
String nom = results.getString("TITLE");
Integer cred = results.getInt("NCREDITOS");
Disciplina disciplina = new Disciplina();
disciplina.setCoddisc(cod);
disciplina.setNome(nom);
disciplina.setNcreditos(cred);
lista.add(disciplina);
}
results.close();
statement.close();
} catch (SQLException e) {
throw new UnsupportedOperationException(e.getMessage());
}
return lista;
}
}
Erro:
error: unreported exception Exception; must be caught or declared to be
thrown
Connection con = getConnection();
Carregando comentários...