DBInstallerConsole.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.tools.dbinstaller.console;
  16. import java.sql.Connection;
  17. import java.util.Scanner;
  18. import java.util.prefs.Preferences;
  19. import com.l2jserver.tools.dbinstaller.DBOutputInterface;
  20. import com.l2jserver.tools.dbinstaller.RunTasks;
  21. import com.l2jserver.tools.dbinstaller.util.mysql.MySqlConnect;
  22. /**
  23. * @author mrTJO
  24. */
  25. public class DBInstallerConsole implements DBOutputInterface
  26. {
  27. Connection _con;
  28. public DBInstallerConsole(String db, String dir, String cleanUp)
  29. {
  30. System.out.println("Welcome to L2J DataBase installer");
  31. Preferences prop = Preferences.userRoot();
  32. Scanner scn = new Scanner(System.in);
  33. while (_con == null)
  34. {
  35. System.out.printf("%s (%s): ", "Host", prop.get("dbHost_" + db, "localhost"));
  36. String dbHost = scn.nextLine();
  37. System.out.printf("%s (%s): ", "Port", prop.get("dbPort_" + db, "3306"));
  38. String dbPort = scn.nextLine();
  39. System.out.printf("%s (%s): ", "Username", prop.get("dbUser_" + db, "root"));
  40. String dbUser = scn.nextLine();
  41. System.out.printf("%s (%s): ", "Password", "");
  42. String dbPass = scn.nextLine();
  43. System.out.printf("%s (%s): ", "Database", prop.get("dbDbse_" + db, db));
  44. String dbDbse = scn.nextLine();
  45. dbHost = dbHost.isEmpty() ? prop.get("dbHost_" + db, "localhost") : dbHost;
  46. dbPort = dbPort.isEmpty() ? prop.get("dbPort_" + db, "3306") : dbPort;
  47. dbUser = dbUser.isEmpty() ? prop.get("dbUser_" + db, "root") : dbUser;
  48. dbDbse = dbDbse.isEmpty() ? prop.get("dbDbse_" + db, db) : dbDbse;
  49. MySqlConnect connector = new MySqlConnect(dbHost, dbPort, dbUser, dbPass, dbDbse, true);
  50. _con = connector.getConnection();
  51. }
  52. RunTasks rt = null;
  53. System.out.print("(C)lean install, (U)pdate or (E)xit? ");
  54. String resp = scn.next();
  55. if (resp.equalsIgnoreCase("c"))
  56. {
  57. System.out.print("Do you really want to destroy your db (Y/N)?");
  58. if (scn.next().equalsIgnoreCase("y"))
  59. {
  60. rt = new RunTasks(this, db, dir, cleanUp, true);
  61. }
  62. }
  63. else if (resp.equalsIgnoreCase("u"))
  64. {
  65. rt = new RunTasks(this, db, dir, cleanUp, false);
  66. }
  67. if (rt != null)
  68. {
  69. rt.run();
  70. }
  71. else
  72. {
  73. System.exit(0);
  74. }
  75. }
  76. @Override
  77. public void appendToProgressArea(String text)
  78. {
  79. System.out.println(text);
  80. }
  81. @Override
  82. public Connection getConnection()
  83. {
  84. return _con;
  85. }
  86. @Override
  87. public void setProgressIndeterminate(boolean value)
  88. {
  89. }
  90. @Override
  91. public void setProgressMaximum(int maxValue)
  92. {
  93. }
  94. @Override
  95. public void setProgressValue(int value)
  96. {
  97. }
  98. @Override
  99. public void setFrameVisible(boolean value)
  100. {
  101. }
  102. @Override
  103. public int requestConfirm(String title, String message, int type)
  104. {
  105. System.out.print(message);
  106. Scanner scn = new Scanner(System.in);
  107. String res = scn.next();
  108. if (res.equalsIgnoreCase("y"))
  109. return 0;
  110. return 1;
  111. }
  112. @Override
  113. public void showMessage(String title, String message, int type)
  114. {
  115. System.out.println(message);
  116. }
  117. }