Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Ardianto Satriawan
- */
- @ManagedBean(name="kontak")
- @SessionScoped
- public class kontakBean implements Serializable {
- public List<kontak> getKontak() throws ClassNotFoundException, SQLException {
- Connection connect = null;
- String url = "jdbc:mysql://localhost:3306/bukutelepon";
- String username = "root";
- String password = "";
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connect = DriverManager.getConnection(url, username, password);
- System.out.println("Connection established"+connect);
- } catch (SQLException ex) {
- System.out.println("in exec");
- System.out.println(ex.getMessage());
- }
- List<kontak> kontak = new ArrayList<>();
- PreparedStatement pstmt = connect
- .prepareStatement("SELECT Id, Nama, Alamat, Telepon FROM Kontak");
- ResultSet rs = pstmt.executeQuery();
- while (rs.next()) {
- kontak kontakbaru = new kontak();
- kontakbaru.setId(rs.getInt("Id"));
- kontakbaru.setNama(rs.getString("Nama"));
- kontakbaru.setAlamat(rs.getString("Alamat"));
- kontakbaru.setTelepon(rs.getString("Telepon"));
- kontak.add(kontakbaru);
- }
- rs.close();
- pstmt.close();
- connect.close();
- return kontak;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement