Advertisement
Guest User

Untitled

a guest
May 7th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5.  
  6.  
  7. public class Main {
  8.  
  9. static final String DATABASE_URL = "jdbc:mysql://zutch.net/ai?autoReconnect=true&useSSL=false";
  10.  
  11. public static void main(String[] args) {
  12. Connection connection = null;
  13. PreparedStatement statement = null;
  14. ResultSet resultSet = null;
  15. String username="pi";
  16. String password="go";
  17.  
  18. String sqlStatement = "select * from employees";
  19. ArrayList<Employee> employees=new ArrayList<Employee>();
  20. try {
  21. connection = DriverManager.getConnection(DATABASE_URL, username, password);
  22.  
  23. statement = connection.prepareStatement(sqlStatement);
  24. // statement.setString(1, "M");
  25. // statement.setString(2, "X%");
  26. // statement.setString(3, "Z%");
  27.  
  28. resultSet =statement.executeQuery();
  29.  
  30. ResultSetMetaData metaData = resultSet.getMetaData();
  31. int numberOfColumns = metaData.getColumnCount();
  32.  
  33. for (int i = 1; i <= numberOfColumns; i++) {
  34. System.out.printf("%-12s\t", metaData.getColumnName(i));
  35.  
  36.  
  37. }
  38.  
  39. System.out.println();
  40.  
  41. while (resultSet.next()) {
  42. for (int i = 1; i <= numberOfColumns; i++) {
  43. System.out.printf("%-12s\t", resultSet.getObject(i));
  44.  
  45. }
  46. System.out.println();
  47. }
  48. while(resultSet.next())
  49. for(int i=1;i<=numberOfColumns/6;i++)
  50. employees.add(new Employee(resultSet.getObject("first_name").toString(), resultSet.getObject("last_name").toString()));
  51.  
  52. System.out.println("done printing now testing arraylist");
  53. for(int i=0;i<employees.size();i++)
  54. System.out.println(employees.get(i));
  55. } catch (SQLException SE) {
  56. SE.printStackTrace();
  57. System.out.println(SE.toString());
  58. } finally {
  59. try {
  60. resultSet.close();
  61. statement.close();
  62. connection.close();
  63. } catch (SQLException E) {
  64. E.printStackTrace();
  65. }
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement