Advertisement
Guest User

Untitled

a guest
Apr 15th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. package com.scrap.mysqlmanager;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class MySQLConnection {
  10.  
  11.     public Connection connection;
  12.     public Statement statement;
  13.     public String host;
  14.     public String db;
  15.     public String user;
  16.     public String pw;
  17.     public int port;
  18.    
  19.     public MySQLConnection(String host, String db, String user, String pw, int port) {
  20.         this.host = host;
  21.         this.db = db;
  22.         this.user = user;
  23.         this.pw = pw;
  24.     }
  25.    
  26.     public void openConnection() throws SQLException, ClassNotFoundException {
  27.         try {
  28.             if (connection != null && !connection.isClosed()) {
  29.                 return;
  30.             }
  31.          
  32.             synchronized (this) {
  33.                 if (connection != null && !connection.isClosed()) {
  34.                     return;
  35.                 }
  36.                 Class.forName("com.mysql.jdbc.Driver");
  37.                 connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.db, this.user, this.pw);
  38.                 statement = connection.createStatement();
  39.             }
  40.         }catch (ClassNotFoundException e) {
  41.             e.printStackTrace();
  42.         }catch (SQLException e) {
  43.             e.printStackTrace();
  44.         }
  45.     }
  46.    
  47.     public void setData(String table, String data, String valuename, String value) throws SQLException {
  48.         try {
  49.             statement.executeUpdate("INSERT INTO " + table + data + " VALUES ('" + valuename + "', " + value + ");");
  50.         } catch (SQLException e) {
  51.             e.printStackTrace();
  52.         }
  53.     }
  54.    
  55.     public ResultSet getData(String table, String data, String valuename) throws SQLException {
  56.         try {
  57.             ResultSet result = statement.executeQuery("SELECT * FROM PlayerData WHERE BALANCE = 0;");
  58.             return result; 
  59.         } catch (SQLException e) {
  60.             e.printStackTrace();
  61.             return null;
  62.         }
  63.     }
  64.    
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement