DBInstallerConsole.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.dbinstaller.console;
  16. import java.sql.Connection;
  17. import java.util.Scanner;
  18. import java.util.prefs.Preferences;
  19. import com.l2jserver.dbinstaller.DBOutputInterface;
  20. import com.l2jserver.dbinstaller.RunTasks;
  21. import com.l2jserver.dbinstaller.util.mysql.MySqlConnect;
  22. /**
  23. *
  24. * @author mrTJO
  25. */
  26. public class DBInstallerConsole implements DBOutputInterface
  27. {
  28. Connection _con;
  29. public DBInstallerConsole(String db, String dir, String cleanUp)
  30. {
  31. System.out.println("Welcome to L2J DataBase installer");
  32. Preferences prop = Preferences.userRoot();
  33. Scanner scn = new Scanner(System.in);
  34. while (_con == null)
  35. {
  36. System.out.printf("%s (%s): ", "Host", prop.get("dbHost_"+db, "localhost"));
  37. String dbHost = scn.nextLine();
  38. System.out.printf("%s (%s): ", "Port", prop.get("dbPort_"+db, "3306"));
  39. String dbPort = scn.nextLine();
  40. System.out.printf("%s (%s): ", "Username", prop.get("dbUser_"+db, "root"));
  41. String dbUser = scn.nextLine();
  42. System.out.printf("%s (%s): ", "Password", "");
  43. String dbPass = scn.nextLine();
  44. System.out.printf("%s (%s): ", "Database", prop.get("dbDbse_"+db, db));
  45. String dbDbse = scn.nextLine();
  46. dbHost = dbHost.isEmpty() ? prop.get("dbHost_"+db, "localhost") : dbHost;
  47. dbPort = dbPort.isEmpty() ? prop.get("dbPort_"+db, "3306") : dbPort;
  48. dbUser = dbUser.isEmpty() ? prop.get("dbUser_"+db, "root") : dbUser;
  49. dbDbse = dbDbse.isEmpty() ? prop.get("dbDbse_"+db, db) : dbDbse;
  50. MySqlConnect connector = new MySqlConnect(dbHost, dbPort, dbUser,
  51. dbPass, dbDbse, true);
  52. _con = connector.getConnection();
  53. }
  54. RunTasks rt = null;
  55. System.out.print("(C)lean install, (U)pdate or (E)xit? ");
  56. String resp = scn.next();
  57. if (resp.equalsIgnoreCase("c"))
  58. {
  59. System.out.print("Do you really want to destroy your db (Y/N)?");
  60. if (scn.next().equalsIgnoreCase("y"))
  61. {
  62. rt = new RunTasks(this, db, dir, cleanUp, true);
  63. }
  64. else
  65. System.exit(0);
  66. }
  67. else if (resp.equalsIgnoreCase("u"))
  68. rt = new RunTasks(this, db, dir, cleanUp, false);
  69. else
  70. System.exit(0);
  71. rt.run();
  72. }
  73. @Override
  74. public void appendToProgressArea(String text)
  75. {
  76. System.out.println(text);
  77. }
  78. @Override
  79. public Connection getConnection()
  80. {
  81. return _con;
  82. }
  83. @Override
  84. public void setProgressIndeterminate(boolean value) { }
  85. @Override
  86. public void setProgressMaximum(int maxValue) { }
  87. @Override
  88. public void setProgressValue(int value) { }
  89. @Override
  90. public void setFrameVisible(boolean value) { }
  91. @Override
  92. public int requestConfirm(String title, String message, int type) {
  93. System.out.print(message);
  94. Scanner scn = new Scanner(System.in);
  95. String res = scn.next();
  96. if (res.equalsIgnoreCase("y"))
  97. return 0;
  98. return 1;
  99. }
  100. @Override
  101. public void showMessage(String title, String message, int type) {
  102. System.out.println(message);
  103. }
  104. }