Guest User

Untitled

a guest
Feb 9th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. package de.mrelektronz.friend.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. public class Mysql
  11. {
  12.  
  13. public Mysql()
  14. {
  15. }
  16.  
  17. public static void connect()
  18. {
  19. if(!connected())
  20. try
  21. {
  22. con = DriverManager.getConnection((new StringBuilder("jdbc:mysql://")).append(host).append(":").append(port).append("/").append(database).toString(), usrname, passwd);
  23. System.out.println("[FriendSystem] MySQL Verbindung aufgebaut!");
  24. }
  25. catch(SQLException e)
  26. {
  27. System.out.println((new StringBuilder("[FriendSystem] ")).append(e.getMessage()).toString());
  28. }
  29. }
  30.  
  31. public static void disconnect()
  32. {
  33. if(connected())
  34. try
  35. {
  36. con.close();
  37. System.out.println("[FriendSystem] MySQL Verbindung geschlossen!");
  38. }
  39. catch(SQLException e)
  40. {
  41. e.printStackTrace();
  42. }
  43. }
  44.  
  45. public static boolean connected()
  46. {
  47. return con != null;
  48. }
  49.  
  50. public static void update(String qry)
  51. {
  52. try
  53. {
  54. PreparedStatement statement = con.prepareStatement(qry);
  55. statement.executeUpdate();
  56. statement.close();
  57. }
  58. catch(SQLException e)
  59. {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. public static ResultSet query(String qry)
  65. {
  66. ResultSet rs = null;
  67. try
  68. {
  69. Statement st = con.createStatement();
  70. rs = st.executeQuery(qry);
  71. }
  72. catch(SQLException e)
  73. {
  74. e.printStackTrace();
  75. }
  76. return rs;
  77. }
  78.  
  79. public static String host;
  80. public static String port;
  81. public static String database;
  82. public static String usrname;
  83. public static String passwd;
  84.  
  85. public static Connection con;
  86. }
Add Comment
Please, Sign In to add comment