UserData.java

package com.tutorialspoint.test; import java.io.Serializable; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.event.ComponentSystemEvent; @ManagedBean(name = "userData", eager = true) @SessionScoped public class UserData implements Serializable { private static final long serialVersionUID = 1L; public List<Author> getAuthors() { ResultSet rs = null; PreparedStatement pst = null; Connection con = getConnection(); String stm = "Select * from authors"; List<Author> records = new ArrayList<Author>(); try { pst = con.prepareStatement(stm); pst.execute(); rs = pst.getResultSet(); while(rs.next()) { Author author = new Author(); author.setId(rs.getInt(1)); author.setName(rs.getString(2)); records.add(author); } } catch (SQLException e) { e.printStackTrace(); } return records; } public Connection getConnection() { Connection con = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user1"; String password = "user1"; try { con = DriverManager.getConnection(url, user, password); System.out.println("Connection completed."); } catch (SQLException ex) { System.out.println(ex.getMessage()); } finally { } return con; } }
UserData.java

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.