AccountApp.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import com.j256.ormlite.dao.Dao;
  2. import com.j256.ormlite.dao.DaoManager;
  3. import com.j256.ormlite.jdbc.JdbcConnectionSource;
  4. import com.j256.ormlite.support.ConnectionSource;
  5. import java.util.*;
  6. import java.sql.SQLException;
  7. public class AccountApp {
  8. // we are using a MySQl database
  9. private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/orm_lab?useUnicode=true";
  10. private Dao<Account, Integer> accountDao;
  11. public static void main(String[] args) throws Exception {
  12. // turn our static method into an instance of Main
  13. new AccountApp().doMain(args);
  14. }
  15. private void doMain(String[] args) throws Exception {
  16. ConnectionSource connectionSource = null;
  17. try {
  18. // create our data-source for the database
  19. connectionSource = new JdbcConnectionSource(DATABASE_URL, "root", "");
  20. // setup our DAOs
  21. setupDao(connectionSource);
  22. // read, write and delete some data
  23. boolean flag = true;
  24. while (flag) {
  25. Scanner in = new Scanner(System.in);
  26. System.out.println("What do you like to do? Create/ Read/ Update/ Delete");
  27. String command = in.nextLine();
  28. if (command.equalsIgnoreCase("create")) {
  29. System.out.println("Enter your username.");
  30. String name = in.nextLine();
  31. System.out.println("Enter your password.");
  32. String password = in.nextLine();
  33. this.createAccount(name, password);
  34. } else if (command.equalsIgnoreCase("read")) {
  35. this.readInfo();
  36. } else if (command.equalsIgnoreCase("update")) {
  37. System.out.println("What is the id that you want to make a change to?");
  38. int id = in.nextInt();
  39. System.out.println("Enter your username.");
  40. String name = in.nextLine();
  41. System.out.println("Enter your password.");
  42. String password = in.nextLine();
  43. this.updateInfo(id, name, password);
  44. } else if (command.equalsIgnoreCase("delete")) {
  45. System.out.println("What is the id that you want to make a change to?");
  46. int id = in.nextInt();
  47. this.deleteInfo(id);
  48. } else if (command.equalsIgnoreCase("exit")){
  49. flag = false;
  50. }
  51. // destroy the data source which should close underlying connections
  52. if (connectionSource != null) {
  53. connectionSource.close();
  54. }
  55. }
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. /**
  61. * Read and write some example data.
  62. */
  63. // create, read, update, delete;
  64. private void createAccount (String name, String password) throws SQLException {
  65. Account account = new Account(name);
  66. account.setPassword(password);
  67. accountDao.create(account); //Create;
  68. }
  69. private void readInfo () throws SQLException {
  70. List<Account> accounts = accountDao.queryForAll();
  71. for (Account acc : accounts) {
  72. System.out.print("ID" + " " + acc.getId() + " Name" + " " + acc.getName()+ "\n");
  73. }
  74. }
  75. private void updateInfo ( int id, String name, String password) throws Exception {
  76. if (accountDao.idExists(id)) {
  77. Account account = accountDao.queryForId(id);
  78. account.setName(name);
  79. account.setPassword(password);
  80. accountDao.update(account);
  81. }
  82. }
  83. private void deleteInfo ( int id) throws Exception {
  84. if (accountDao.idExists(id)) {
  85. accountDao.deleteById(id);
  86. }
  87. }
  88. /**
  89. * Setup our DAOs
  90. */
  91. private void setupDao (ConnectionSource connectionSource) throws Exception {
  92. accountDao = DaoManager.createDao(connectionSource, Account.class);
  93. }
  94. }