import java.sql.*;
public class Connexao {
public Connection con;
public Statement stm;
public ResultSet res = null;
private String nome_funcionario = null;
private String senha_pw=null;
private String morada=null;
private String nacionalidade=null;
private String naturalidade=null;
private String data_de_nascimento=null;
private String telefone=null;
private String codigo_funcionario=null;
public Connexao() {
try {
// conexão do java com a base de dados SQl usando o driver jdbc
Class.forName ("org.postgresql.Driver");
con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Inter Markenk Dados","postgres","654321");
stm = con.createStatement();
} catch (Exception e) {
System.out.println("não foi possível conectar ao banco" + e.getMessage());
}
}
public void inserirDados() {
try {
// Inserção de valores na tabela funcionario
String query = "insert into Funcionario(nome_funcionario,senha_pw,morada,nacionalidade,naturalidade,data_de_nascimento,telefone,codigo_funcionario) "
+ "values(\"" + nome_funcionario + "\"," + senha_pw + ",\"" + morada + "\"" + nacionalidade +"\"" + naturalidade +"\""
+ data_de_nascimento + "\"" + telefone + "\"" + codigo_funcionario + "\")";
stm.executeUpdate(query);
} catch (SQLException e)
{
System.out.println("Erro na inserção:" + e.getMessage());
}
}
public boolean alterarDados() {
boolean testa = false;
try {
String query = "update Funcionario " +
"set nome_funcionario = " + nome_funcionario +
", " + "senha_pw = \"" + senha_pw +
"\", " +" morada=\"" + morada +
"\", " +" nacionalidade=\"" + nacionalidade +
"\", " +" naturalidade=\"" + naturalidade +
"\", " +" data_de_nascimento=\"" + data_de_nascimento +
"\", " +" codigo_funcionario=\"" + codigo_funcionario +
"where nome = \"" + nome_funcionario + "\"";
int linhas = stm.executeUpdate(query);
if (linhas > 0) {
testa = true;
} else {
testa = false;
}
} catch (SQLException e) {
System.out.println("Erro na inserção:" + e.getMessage());
}
return testa;
}
public boolean excluirDados() {
boolean testa = false;
try {
String query = "delete from pessoa where nome_funcionario='" + nome_funcionario + "'";
int linhas = stm.executeUpdate(query);
if (linhas > 0) {
testa = true;
} else {
testa = false;
}
} catch (SQLException e) {
System.out.println("Erro na exclusão:" + e.getMessage());
}
return testa;
}
public boolean consultarDados() {
boolean testa = false;
try {
String query = "select * from pessoa where nome_funcionario='" + nome_funcionario + "'";
res = stm.executeQuery(query);
if (res.next()) {
testa = true;
} else {
testa = false;
}
} catch (SQLException e) {
System.out.println("Erro na inserção:" + e.getMessage());
}
return testa;
}
public void setConsulta() {
try {
res = stm.executeQuery("select * from Funcionario");
} catch (SQLException e) {
e.printStackTrace();
}
}
public ResultSet getResultado() {
return res;
}
esotu a usar o netbeans 6.9