package zti.model;
import java.util.List;
import jakarta.faces.component.UIParameter;
import jakarta.faces.event.ActionEvent;
import jakarta.inject.Named;
import jakarta.enterprise.context.RequestScoped;
@Named ("personBean")
@RequestScoped
public class Person {
private String lname ;
private String fname ;
private String city ;
private String email ;
private String tel ;
private Integer id;
public Person() {}
public Person(String lname, String fname, String city, String email, String tel, Integer id){
this.lname = lname;
this.fname = fname;
this.city = city;
this.email = email;
this.tel = tel;
this.id = id;
}
public void setPerson(Person person){
this.setLname(person.getLname());
this.setFname(person.getFname());
this.setCity(person.getCity());
this.setEmail(person.getEmail());
this.setTel(person.getTel());
this.setId(person.getId());
}
public Person getPerson(){
return new Person(this.getLname(),
this.getFname(),
this.getCity(),
this.getEmail(),
this.getTel(),
this.getId() );
}
public void setFname( String newValue ){ fname = newValue ; }
public String getFname() { return fname ; }
public void setLname ( String newValue ) { lname = newValue ; }
public String getLname() { return lname ; }
public void setCity ( String newValue ) { city = newValue ; }
public String getCity() { return city ; }
public void setEmail ( String newValue ) { email = newValue ; }
public String getEmail() { return email ; }
public void setTel ( String newValue ) { tel = newValue ; }
public String getTel() { return tel ; }
public void setId ( Integer newValue ) { id = newValue ; }
public Integer getId() { return id ; }
public List<Person> getPeople() {
Database baza = new Database() ;
List<Person> people = baza.getPersonList();
return people ;
}
}
package zti.model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Named;
@Named("databaseBean")
@RequestScoped
public class Database {
private Person person;
private Connection conn = null;
private PreparedStatement prestmt = null;
private Statement stmt = null;
private ResultSet rset = null;
private List<Person> list = new ArrayList<Person>();
public Database() {
// System.out.println("Init managed Bean");
try {
Class.forName("org.postgresql.Driver");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
String url = "jdbc:postgresql://qdjjtnkv.db.elephantsql.com:5432/<<database>>";
String username = "<<user>>";
String password = "<<pass>>";
try {
conn = DriverManager.getConnection(url, username, password);
// System.out.println("Connect to Database");
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
public List<Person> getPersonList() {
try {
stmt = conn.createStatement();
String sql = "SELECT * FROM public.person ORDER BY lname";
rset = stmt.executeQuery(sql);
while (rset.next()) {
person = new Person();
person.setFname(rset.getString("fname"));
person.setLname(rset.getString("lname"));
person.setEmail(rset.getString("email"));
person.setTel(rset.getString("tel"));
person.setCity(rset.getString("city"));
person.setId(rset.getInt("id"));
list.add(person);
}
rset.close();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
} finally {
try {
if (prestmt != null) {
prestmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
return list;
}
}