Advertisement
Guest User

Untitled

a guest
May 13th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. package cz.Spekynek123.Auth.Managers;
  2.  
  3. import java.sql.*;
  4.  
  5. public class MySQLManager {
  6.  
  7. private static Connection connection;
  8. public static String host, database, username, password;
  9. public static int port;
  10.  
  11. public static void connect(){
  12. host = "my_host";
  13. port = 3306;
  14. database = "my_database";
  15. username = "my_username";
  16. password = "my_password";
  17.  
  18. try {
  19. if (connection != null && !connection.isClosed()){
  20. return;
  21. }
  22.  
  23. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  24.  
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29.  
  30. public static void disconnect(){
  31. try {
  32. if (connection != null && !connection.isClosed()){
  33. connection.close();
  34. }
  35. } catch (SQLException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39.  
  40. public static void createTable(){
  41. try {
  42. if (!connection.isClosed()){
  43. Statement st = connection.createStatement();
  44. st.executeUpdate("create table if not exists users (id int (16) not null auto_increment, username varchar (30) not null, password varchar (256) not null, active int (1) not null default 1, admin int (1) not null default 0, primary key (id))");
  45. st.close();
  46.  
  47. }
  48. } catch (SQLException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52.  
  53. public static boolean userExist(String user){
  54. try {
  55. ResultSet rs = connection.createStatement().executeQuery("select * from users where username = '" + user + "'");
  56. while (rs.next()){
  57. if (rs.getString("username") != null){
  58. return true;
  59. }
  60. }
  61.  
  62. rs.close();
  63.  
  64. } catch (SQLException e) {
  65. e.printStackTrace();
  66. }
  67. return false;
  68. }
  69.  
  70. public static void unregisterPlayer(String user){
  71. try {
  72. Statement st = connection.createStatement();
  73. st.executeUpdate("delete from users where username = '" + user + "'");
  74. st.close();
  75. } catch (SQLException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79.  
  80. public static void registerPlayer(String user, String password){
  81. try {
  82. Statement st = connection.createStatement();
  83. st.executeUpdate("insert into users (username, password) values ('" + user + "', '" + password + "')");
  84. st.close();
  85. } catch (SQLException e) {
  86. e.printStackTrace();
  87. }
  88. }
  89.  
  90. public static String getPassword(String user){
  91. try {
  92. ResultSet rs = connection.createStatement().executeQuery("select * from users where username = '" + user + "'");
  93. if (rs.next()){
  94. return rs.getString("password");
  95. }
  96. } catch (SQLException e) {
  97. e.printStackTrace();
  98. }
  99. return null;
  100. }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement