Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.Spekynek123.Auth.Managers;
- import java.sql.*;
- public class MySQLManager {
- private static Connection connection;
- public static String host, database, username, password;
- public static int port;
- public static void connect(){
- host = "my_host";
- port = 3306;
- database = "my_database";
- username = "my_username";
- password = "my_password";
- try {
- if (connection != null && !connection.isClosed()){
- return;
- }
- connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void disconnect(){
- try {
- if (connection != null && !connection.isClosed()){
- connection.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void createTable(){
- try {
- if (!connection.isClosed()){
- Statement st = connection.createStatement();
- 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))");
- st.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static boolean userExist(String user){
- try {
- ResultSet rs = connection.createStatement().executeQuery("select * from users where username = '" + user + "'");
- while (rs.next()){
- if (rs.getString("username") != null){
- return true;
- }
- }
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- public static void unregisterPlayer(String user){
- try {
- Statement st = connection.createStatement();
- st.executeUpdate("delete from users where username = '" + user + "'");
- st.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void registerPlayer(String user, String password){
- try {
- Statement st = connection.createStatement();
- st.executeUpdate("insert into users (username, password) values ('" + user + "', '" + password + "')");
- st.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static String getPassword(String user){
- try {
- ResultSet rs = connection.createStatement().executeQuery("select * from users where username = '" + user + "'");
- if (rs.next()){
- return rs.getString("password");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement