Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.coding4you.ban.util;
- /*
- *
- * (c) by coding4you.io you are not permit to sell it!
- *
- */
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import java.sql.*;
- public class MySQL {
- Connection connection;
- public MySQL(String host, String username, String password, String database){
- try{
- Class.forName("com.mysql.jdbc.Driver");
- setConnection(DriverManager.getConnection("jdbc:mysql://" + host + ":"
- + 3306 + "/" + database, username, password));
- if(!getConnection().isClosed()){
- Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "MYSQL connected!");
- }else {
- Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "MYSQL disconnected!");
- }
- }catch(SQLException ex){
- ex.printStackTrace();
- }catch(ClassNotFoundException ex){
- ex.printStackTrace();
- }
- }
- public MySQL(String host, String username, String password, String database, int port){
- try{
- Class.forName("com.mysql.jdbc.Driver");
- setConnection(DriverManager.getConnection("jdbc:mysql://" + host + ":"
- + port + "/" + database, username, password));
- }catch(SQLException ex){
- ex.printStackTrace();
- }catch(ClassNotFoundException ex){
- ex.printStackTrace();
- }
- }
- public Connection getConnection(){
- return connection;
- }
- public void setConnection(Connection connection){
- this.connection = connection;
- }
- public void createTable(String name, String value){
- try{
- PreparedStatement statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS " + name + " (" + value + ")");
- statement.execute();
- }catch(SQLException ex){
- ex.printStackTrace();
- }
- }
- public Object get(String table, String from, String value, String get){
- try{
- PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM " + table + " WHERE "+ from + "=?");
- statement.setString(1, value);
- ResultSet resultSet = statement.executeQuery();
- resultSet.next();
- return resultSet.getObject(get);
- }catch(SQLException ex){
- ex.printStackTrace();
- }
- return null;
- }
- public void update(String table, String a, String b,String what, String value){
- try{
- PreparedStatement statement = getConnection().prepareStatement("UPDATE FROM " + table + " " + what + "=?" + " WHERE" + a + "=?");
- statement.setString(1, value);
- statement.setString(2, b);
- ResultSet resultSet = statement.executeQuery();
- }catch(SQLException ex){
- ex.printStackTrace();
- }
- }
- public void disconnect(){
- if(isConnected()){
- try{
- getConnection().close();
- Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "MYSQL disconnected");
- }catch(SQLException ex){
- ex.printStackTrace();
- }
- }
- }
- public boolean isConnected(){
- try{
- if(!getConnection().isClosed()){
- return true;
- }
- }catch(SQLException ex){
- ex.printStackTrace();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement