MySqlConnect.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.util.mysql;
  16. import java.sql.Connection;
  17. import java.sql.DriverManager;
  18. import java.sql.SQLException;
  19. import java.sql.Statement;
  20. import java.util.Formatter;
  21. import javax.swing.JOptionPane;
  22. /**
  23. *
  24. * @author mrTJO
  25. */
  26. public class MySqlConnect
  27. {
  28. Connection con = null;
  29. public MySqlConnect(String host, String port, String user, String password, String db, boolean console)
  30. {
  31. try
  32. {
  33. Class.forName("com.mysql.jdbc.Driver").newInstance();
  34. con = DriverManager.getConnection(
  35. new Formatter().format("jdbc:mysql://%1$s:%2$s", host, port).toString(),
  36. user, password);
  37. Statement st = con.createStatement();
  38. st.execute("CREATE DATABASE IF NOT EXISTS `"+db+"`");
  39. st.execute("USE `"+db+"`");
  40. st.close();
  41. }
  42. catch (SQLException e)
  43. {
  44. if (console)
  45. e.printStackTrace();
  46. else
  47. JOptionPane.showMessageDialog(null, "MySQL Error: "+e.getMessage(),
  48. "Connection Error", JOptionPane.ERROR_MESSAGE);
  49. }
  50. catch (InstantiationException e)
  51. {
  52. if (console)
  53. e.printStackTrace();
  54. else
  55. JOptionPane.showMessageDialog(null, "Instantiation Exception: "+e.getMessage(),
  56. "Connection Error", JOptionPane.ERROR_MESSAGE);
  57. }
  58. catch (IllegalAccessException e)
  59. {
  60. if (console)
  61. e.printStackTrace();
  62. else
  63. JOptionPane.showMessageDialog(null, "Illegal Access: "+e.getMessage(), "Connection Error",
  64. JOptionPane.ERROR_MESSAGE);
  65. }
  66. catch (ClassNotFoundException e)
  67. {
  68. if (console)
  69. e.printStackTrace();
  70. else
  71. JOptionPane.showMessageDialog(null, "Cannot find MySQL Connector: "+e.getMessage(),
  72. "Connection Error", JOptionPane.ERROR_MESSAGE);
  73. }
  74. }
  75. public Connection getConnection()
  76. {
  77. return con;
  78. }
  79. public Statement getStatement()
  80. {
  81. try
  82. {
  83. return con.createStatement();
  84. }
  85. catch (SQLException e)
  86. {
  87. e.printStackTrace();
  88. System.out.println("Statement Null");
  89. return null;
  90. }
  91. }
  92. }