Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1.  
  2. import java.io.Serializable;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import javax.faces.bean.ManagedBean;
  11. import javax.faces.bean.SessionScoped;
  12.  
  13. /*
  14.  * To change this license header, choose License Headers in Project Properties.
  15.  * To change this template file, choose Tools | Templates
  16.  * and open the template in the editor.
  17.  */
  18.  
  19. /**
  20.  *
  21.  * @author Ardianto Satriawan
  22.  */
  23.  
  24. @ManagedBean(name="kontak")
  25. @SessionScoped
  26. public class kontakBean implements Serializable {
  27.     public List<kontak> getKontak() throws ClassNotFoundException, SQLException {
  28.         Connection connect = null;
  29.  
  30.         String url = "jdbc:mysql://localhost:3306/bukutelepon";
  31.  
  32.         String username = "root";
  33.         String password = "";
  34.  
  35.         try {
  36.  
  37.             Class.forName("com.mysql.jdbc.Driver");
  38.  
  39.             connect = DriverManager.getConnection(url, username, password);
  40.             System.out.println("Connection established"+connect);
  41.  
  42.         } catch (SQLException ex) {
  43.             System.out.println("in exec");
  44.             System.out.println(ex.getMessage());
  45.         }
  46.        
  47.         List<kontak> kontak = new ArrayList<>();
  48.         PreparedStatement pstmt = connect
  49.                 .prepareStatement("SELECT Id, Nama, Alamat, Telepon FROM Kontak");
  50.        
  51.         ResultSet rs = pstmt.executeQuery();
  52.        
  53.         while (rs.next()) {
  54.             kontak kontakbaru = new kontak();
  55.             kontakbaru.setId(rs.getInt("Id"));
  56.             kontakbaru.setNama(rs.getString("Nama"));
  57.             kontakbaru.setAlamat(rs.getString("Alamat"));
  58.             kontakbaru.setTelepon(rs.getString("Telepon"));
  59.            
  60.             kontak.add(kontakbaru);
  61.         }
  62.        
  63.         rs.close();
  64.         pstmt.close();
  65.         connect.close();
  66.        
  67.         return kontak;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement