Guest User

Untitled

a guest
May 10th, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.ArrayList;
  3.  
  4. public class GetFeedback {
  5.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  6.     static final String DB_URL = "jdbc:mysql://localhost/Contact";
  7.     static final ArrayList<Feedback> mFeedback = new ArrayList<>();
  8.     //  Database credentials
  9.     static final String USER = "username";
  10.     static final String PASS = "password";
  11.  
  12.     public static void main(String[] args) {
  13.         Connection conn = null;
  14.         Statement stmt = null;
  15.         try{
  16.             Class.forName(JDBC_DRIVER);
  17.  
  18.             //STEP 3: Open a connection
  19.             System.out.println("Connecting to database...");
  20.             conn = DriverManager.getConnection(DB_URL, USER, PASS);
  21.  
  22.  
  23.             System.out.println("Creating statement...");
  24.             stmt = conn.createStatement(
  25.                     ResultSet.TYPE_SCROLL_INSENSITIVE,
  26.                     ResultSet.CONCUR_READ_ONLY);
  27.             String sql;
  28.             sql = "SELECT id, first, last, age FROM Employees";
  29.             ResultSet rs = stmt.executeQuery(sql);
  30.             while (rs.next()) {
  31.  
  32.                 Feedback feedback = new Feedback(rs.getString("name"),rs.getString("feedback"),rs.getString("email"));
  33.                 mFeedback.add(feedback);
  34.             }
  35.             rs.close();
  36.             stmt.close();
  37.             conn.close();
  38.         } catch(Exception e){
  39.             e.printStackTrace();
  40.         }finally {
  41.             try {
  42.                 if (stmt != null)
  43.                     stmt.close();
  44.             } catch (SQLException ignored) {
  45.  
  46.                 try {
  47.                     conn.close();
  48.                 } catch (SQLException se) {
  49.                     se.printStackTrace();
  50.                 }
  51.             }
  52.             System.out.println("Goodbye!");
  53.         }
  54.     }
  55.     public static ArrayList<Feedback> getFeedbacks() {
  56.         return mFeedback;
  57.     }
  58. }
  59.  
  60.  
  61. =============================================
  62. public class Feedback {
  63.     private String mEmail,mFeedback,mName;
  64.  
  65.     public Feedback(String name,String feed,String email) {
  66.         this.mName = name;
  67.         this.mFeedback = feed;
  68.         this.mFeedback = feed;
  69.     }
  70.     public String getEmail() {
  71.         return mEmail;
  72.     }
  73.  
  74.  
  75.     public String getFeedback() {
  76.         return mFeedback;
  77.     }
  78.  
  79.  
  80.     public String getName() {
  81.         return mName;
  82.     }
  83.  
  84. }
Add Comment
Please, Sign In to add comment