MySqlConnect.java 2.6 KB

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