Advertisement
Guest User

Sdasd

a guest
Dec 30th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. public static String url;
  2. public static String driver;
  3. public static Connection con = null;
  4.  
  5. public MySQL(final String host, final String db, final String username, final String password) {
  6. //Tworzymy nową instancje bazy danych i nawiązujemy połączenie. w hoscie musi znajdować się też port servera MySQL
  7. MySQL.url = "jdbc:mysql://" + host + "/" + db + "?user=" + username + "&password=" + password;
  8. MySQL.driver = ("com.mysql.jdbc.Driver");
  9. }
  10.  
  11. // Metoda od otwierania połączenia, zwykle używasz tylko raz :P
  12. public Connection open() throws SQLException, ClassNotFoundException {
  13. Class.forName(MySQL.driver);
  14. MySQL.con = DriverManager.getConnection(MySQL.url);
  15. return MySQL.con;
  16. }
  17.  
  18. // Tej metody używasz w onEnable by połączyć się z bazą danych. Oczywiście nie musi byc boolean
  19. // ale dzięki temu mogę odrazu sprawdzic czy sie w miare udało :P
  20. public static boolean connectToDatabase() {
  21. try {
  22. MySQL.con = new MySQL("localhost", "baza", "root", "dobrehaslo33").open();
  23. return true;
  24. } catch (ClassNotFoundException | SQLException e) {
  25. e.printStackTrace();
  26. return false;
  27. }
  28. }
  29.  
  30. // Tutaj sobie tworze tabelki :>
  31. public static void createTables(String ...sb) throws SQLException {
  32. DbStatement stat = createStatement(sb);
  33.  
  34. // Bardzo ważne by zawsze na końiec zamykać statement
  35. stat.runAndClose();
  36. }
  37. private static void isConnected() throws SQLException{
  38. if(con.isClosed() || con == null){
  39. connectToDatabase();
  40. }
  41. }
  42.  
  43.  
  44. // Tutaj są 4 wygodne metody od używania bazy danych :>
  45.  
  46. //Tworzy nowy statement, od używania UUPDATE, INSERT itp w argumentach podajesz wszystkie potrzebne SQL-e
  47. public static DbStatement createStatement(final String... sqls) throws SQLException {
  48. isConnected();
  49. return new DbStatement(con.createStatement(), sqls);
  50. }
  51.  
  52. // To odrazu tworzy i uruchamia i zamyka statement :> czyli jak chcesz tylko szybko wykonać jakąs komende i tyle.
  53. public static void runStatement(final String... sqls) throws SQLException {
  54. isConnected();
  55. new DbStatement(con.createStatement(), sqls).runAndClose();
  56. }
  57.  
  58. //To samo co 1, ale jest czysty na początku (mozna potem dodac itp :>)
  59. public static DbStatement createStatement() throws SQLException {
  60. isConnected();
  61. return new DbStatement(con.createStatement());
  62. }
  63.  
  64. // A to od używania SELECT :P po skończeniu zabawy z select też należy zamknąc :>
  65. public static DbResult getResult(String sql) throws SQLException {
  66. isConnected();
  67. return new DbResult(con.createStatement(), sql);
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement