Advertisement
MrGG4ming

Untitled

Feb 6th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. package com.finalelite.testes;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.HashMap;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. // create a users class
  13. class Users {
  14. private int Id;
  15. private String Fname;
  16. private String Lname;
  17. private int Age;
  18.  
  19. public Users() {
  20. }
  21.  
  22. public Users(int id, String fname, String lname, int age) {
  23. this.Id = id;
  24. this.Fname = fname;
  25. this.Lname = lname;
  26. this.Age = age;
  27. }
  28.  
  29. public int getId() {
  30. return this.Id;
  31. }
  32.  
  33. public String getFname() {
  34. return this.Fname;
  35. }
  36.  
  37. public String getLname() {
  38. return this.Lname;
  39. }
  40.  
  41. public int getAge() {
  42. return this.Age;
  43. }
  44.  
  45. // create a tostring method to diplay data
  46. public String ToString() {
  47. return this.Id + " " + this.Fname + " " + this.Lname + " " + this.Age;
  48. }
  49. }
  50.  
  51. public class Work {
  52.  
  53. // create a method to get the connection
  54. public static Connection getConnection() {
  55.  
  56. Connection con = null;
  57.  
  58. try {
  59. con = DriverManager.getConnection("jdbc:mysql://localhost/batt_356?autoReconnect=true", "batt_356",
  60. "ddb38916fd");
  61. } catch (SQLException ex) {
  62. Logger.getLogger(Work.class.getName()).log(Level.SEVERE, null, ex);
  63. }
  64.  
  65. return con;
  66. }
  67.  
  68. public static void main(String[] args) {
  69.  
  70. // create the hashmap
  71. HashMap<Integer, Users> map = new HashMap<Integer, Users>();
  72.  
  73. Statement st = null;
  74. ResultSet rs = null;
  75. Connection con = getConnection();
  76. Users u;
  77.  
  78. try {
  79. st = con.createStatement();
  80. rs = st.executeQuery("SELECT * FROM users");
  81. while (rs.next()) {
  82. Integer id = rs.getInt("id");
  83. String fname = rs.getString("fname");
  84. String lname = rs.getString("lname");
  85. int age = rs.getInt("age");
  86.  
  87. u = new Users(id, fname, lname, age);
  88.  
  89. // set data in the hashmap
  90. map.put(id, u);
  91. }
  92. } catch (Exception ex) {
  93. ex.printStackTrace();
  94. }
  95.  
  96. // display data from the hashmap
  97. for (Integer i : map.keySet()) {
  98. Users us = map.get(i);
  99. System.out.println(us.getId() + " " + us.getFname() + " " + us.getLname() + " " + us.getAge());
  100. }
  101.  
  102. // show data from hashmap using our ToString Method
  103. System.out.println("______With ToString______");
  104. for (Integer i : map.keySet()) {
  105. Users us = map.get(i);
  106. System.out.println(us.ToString());
  107. }
  108.  
  109. // now your data are displayed from mysql database using hashmap
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement