Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.ArrayList;
- public class GetFeedback {
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- static final String DB_URL = "jdbc:mysql://localhost/Contact";
- static final ArrayList<Feedback> mFeedback = new ArrayList<>();
- // Database credentials
- static final String USER = "username";
- static final String PASS = "password";
- public static void main(String[] args) {
- Connection conn = null;
- Statement stmt = null;
- try{
- Class.forName(JDBC_DRIVER);
- //STEP 3: Open a connection
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- System.out.println("Creating statement...");
- stmt = conn.createStatement(
- ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- String sql;
- sql = "SELECT id, first, last, age FROM Employees";
- ResultSet rs = stmt.executeQuery(sql);
- while (rs.next()) {
- Feedback feedback = new Feedback(rs.getString("name"),rs.getString("feedback"),rs.getString("email"));
- mFeedback.add(feedback);
- }
- rs.close();
- stmt.close();
- conn.close();
- } catch(Exception e){
- e.printStackTrace();
- }finally {
- try {
- if (stmt != null)
- stmt.close();
- } catch (SQLException ignored) {
- try {
- conn.close();
- } catch (SQLException se) {
- se.printStackTrace();
- }
- }
- System.out.println("Goodbye!");
- }
- }
- public static ArrayList<Feedback> getFeedbacks() {
- return mFeedback;
- }
- }
- =============================================
- public class Feedback {
- private String mEmail,mFeedback,mName;
- public Feedback(String name,String feed,String email) {
- this.mName = name;
- this.mFeedback = feed;
- this.mFeedback = feed;
- }
- public String getEmail() {
- return mEmail;
- }
- public String getFeedback() {
- return mFeedback;
- }
- public String getName() {
- return mName;
- }
- }
Add Comment
Please, Sign In to add comment