Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. package ru.stqa.pft.addressbook.tests;
  2.  
  3. import org.testng.annotations.Test;
  4. import ru.stqa.pft.addressbook.model.ContactData;
  5. import ru.stqa.pft.addressbook.model.Contacts;
  6. import ru.stqa.pft.addressbook.model.GroupData;
  7. import ru.stqa.pft.addressbook.model.Groups;
  8.  
  9. import java.sql.*;
  10.  
  11. /**
  12. * Created by EOnegin on 22.08.2017.
  13. */
  14. public class DbConnectionTest {
  15.  
  16. @Test
  17. public void testDbConnection() {
  18. Connection conn = null;
  19. try {
  20. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/addressbook?user=root&password=&serverTimezone=UTC");
  21.  
  22. Statement st = conn.createStatement();
  23. ResultSet rs = st.executeQuery("select group_id, group_name, group_header, group_footer from group_list");
  24. Groups groups = new Groups();
  25. while (rs.next()) {
  26. groups.add(new GroupData().withId(rs.getInt("group_id")).withName(rs.getString("group_name"))
  27. .withHeader(rs.getString("group_header")).withFooter(rs.getString("group_footer")));
  28. }
  29. rs.close();
  30. st.close();
  31.  
  32. System.out.println(groups);
  33.  
  34. Statement stc = conn.createStatement();
  35. ResultSet rsc = stc.executeQuery("select id, firstname, middlename, lastname, nickname, title, company from addressbook");
  36. Contacts contacts = new Contacts();
  37. while (rsc.next()) {
  38. contacts.add(new ContactData().withId(rsc.getInt("id")).withFirstname(rsc.getString("firstname"))
  39. .withMiddlename(rsc.getString("middlename")).withLastname(rsc.getString("lastname")).withNickname(rsc.getString("nickname"))
  40. .withTitle(rsc.getString("title")).withCompany(rsc.getString("company")));
  41. }
  42. rsc.close();
  43. st.close();
  44.  
  45. System.out.println(contacts);
  46.  
  47. conn.close();
  48.  
  49. } catch (SQLException ex) {
  50. // handle any errors
  51. System.out.println("SQLException: " + ex.getMessage());
  52. System.out.println("SQLState: " + ex.getSQLState());
  53. System.out.println("VendorError: " + ex.getErrorCode());
  54.  
  55. }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement