DBInstallerConsole.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. else
  63. System.exit(0);
  64. }
  65. else if (resp.equalsIgnoreCase("u"))
  66. rt = new RunTasks(this, db, dir, cleanUp, false);
  67. else
  68. System.exit(0);
  69. rt.run();
  70. }
  71. @Override
  72. public void appendToProgressArea(String text)
  73. {
  74. System.out.println(text);
  75. }
  76. @Override
  77. public Connection getConnection()
  78. {
  79. return _con;
  80. }
  81. @Override
  82. public void setProgressIndeterminate(boolean value)
  83. {
  84. }
  85. @Override
  86. public void setProgressMaximum(int maxValue)
  87. {
  88. }
  89. @Override
  90. public void setProgressValue(int value)
  91. {
  92. }
  93. @Override
  94. public void setFrameVisible(boolean value)
  95. {
  96. }
  97. @Override
  98. public int requestConfirm(String title, String message, int type)
  99. {
  100. System.out.print(message);
  101. Scanner scn = new Scanner(System.in);
  102. String res = scn.next();
  103. if (res.equalsIgnoreCase("y"))
  104. return 0;
  105. return 1;
  106. }
  107. @Override
  108. public void showMessage(String title, String message, int type)
  109. {
  110. System.out.println(message);
  111. }
  112. }