Advertisement
Guest User

Untitled

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