Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Mysql {
- private Main plugin;
- public static Connection con;
- public String host;
- public int port;
- public String database;
- public String username;
- public String password;
- public int MySQLSchedulerID;
- public Mysql(Main main) {
- this.plugin = main;
- host = "ip";
- port = 3306;
- database = "databasename";
- username = "username";
- password = "password";
- }
- public void connect() {
- if(!isConnected()) {
- try {
- con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username ,password);
- System.out.println("[MySQL] Verbunden!");
- onReconnectScheduler();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public boolean isConnected() {
- return(con == null ? false : true);
- }
- public void disconect() {
- if(con != null){
- try {
- con.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
- public ResultSet query(String query) {
- if(!isConnected()){
- connect();
- }
- ResultSet rs = null;
- try {
- Statement stm = con.createStatement();
- rs = stm.executeQuery(query);
- } catch (SQLException ex) {
- connect();
- ex.printStackTrace();
- }
- return rs;
- }
- public void update(String query) {
- if(!isConnected()){
- connect();
- }
- try {
- Statement stm = con.createStatement();
- System.out.println("Ausgefuehrtes Abfrage: "+query);
- stm.executeUpdate(query);
- } catch (SQLException ex) {
- connect();
- ex.printStackTrace();
- }
- }
- private void onReconnectScheduler()
- {
- MySQLSchedulerID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()
- {
- public void run()
- {
- onReconnect();
- }
- }, 20 * 60 * 60 *6, 20 * 60 * 60 *6);
- }
- private void onReconnect()
- {
- if(con != null)
- {
- try
- {
- con.close();
- System.out.println("[MySQL] MySQL-Verbindung beendet!");
- }
- catch (SQLException e)
- {
- System.err.println("[MySQL] MySQL-Verbindung konnte nicht getrennt werden!");
- e.printStackTrace();
- }
- }
- Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
- {
- public void run()
- {
- try
- {
- con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username ,password);
- System.out.println("[MySQL] MySQL-Verbindung hergestellt!");
- }
- catch (SQLException e)
- {
- System.err.println("[MySQL] MySQL-Verbindung konnte nicht hergestellt werden!");
- e.printStackTrace();
- }
- }
- }, 1L);
- }
- public boolean isInDatabase(Player target, String tabelle, String spalte) throws SQLException {
- String uuid = target.getUniqueId().toString();
- boolean isInDatabase = false;
- ResultSet rs = query("SELECT * FROM "+tabelle+" WHERE "+spalte+" = '" + uuid + "'");
- if(rs.next()){
- isInDatabase = true;
- }else{
- isInDatabase = false;
- }
- return Boolean.valueOf(isInDatabase).booleanValue();
- }
- public boolean isInDatabase(String target, String tabelle, String spalte) throws SQLException {
- boolean isInDatabase = false;
- ResultSet rs = query("SELECT * FROM "+tabelle+" WHERE "+spalte+" = '" + target + "'");
- if(rs.next()){
- isInDatabase = true;
- }else{
- isInDatabase = false;
- }
- return Boolean.valueOf(isInDatabase).booleanValue();
- }
- public boolean isInDatabase(int target, String tabelle, String spalte) throws SQLException {
- boolean isInDatabase = false;
- ResultSet rs = query("SELECT * FROM "+tabelle+" WHERE "+spalte+" = '" + target + "'");
- if(rs.next()){
- isInDatabase = true;
- }else{
- isInDatabase = false;
- }
- return Boolean.valueOf(isInDatabase).booleanValue();
- }
- public ResultSet getResult(String query){
- if(isConnected()){
- try {
- return con.createStatement().executeQuery(query);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }else{
- connect();
- getResult(query);
- }
- return null;
- }
- public ResultSet getPrepairedResult(String query){
- if(isConnected()){
- try {
- return con.prepareStatement(query).executeQuery();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }else{
- connect();
- getPrepairedResult(query);
- }
- return null;
- }
- public void createTableIfNotExist(String query){
- update("CREATE TABLE IF NOT EXIST "+query);
- }
- public void createTable(String query){
- update("CREATE TABLE "+query);
- }
- public void createDatabaseEintrag(String table, String spalten, String werte){
- update("INSERT INTO "+table+"("+spalten+") VALUES("+werte+")");
- }
- public void setWertInTable(String table, String spalte, String wert, String fromSpalte, String wertFromSpalte){
- update("UPDATE "+table+" SET "+spalte+"='"+wert+"' WHERE "+fromSpalte+"='"+wertFromSpalte+"'");
- }
- public void setWertInTable(String table, String spalte, int wert, String fromSpalte, String wertFromSpalte){
- update("UPDATE "+table+" SET "+spalte+"="+wert+" WHERE "+fromSpalte+"='"+wertFromSpalte+"'");
- }
- public void setNULLInTable(String table, String spalte, String fromSpalte, String wertFromSpalte){
- update("UPDATE "+table+" SET "+spalte+"="+null+" WHERE "+fromSpalte+"='"+wertFromSpalte+"'");
- }
- public void setWertInTable(String table, String spalte, double wert, String fromSpalte, String wertFromSpalte){
- update("UPDATE "+table+" SET "+spalte+"='"+wert+"' WHERE "+fromSpalte+"='"+wertFromSpalte+"'");
- }
- public ResultSet getWert(String table, String spalte, String fromSpalte){
- return getResult("SELECT * FROM "+table+" WHERE "+spalte+ " ='"+fromSpalte+"'");
- }
- public ResultSet getWert(String table, String spalte, int fromSpalte){
- return getResult("SELECT * FROM "+table+" WHERE "+spalte+ " ="+fromSpalte);
- }
- public ResultSet getWert(String table, String spalte, double fromSpalte){
- return getResult("SELECT * FROM "+table+" WHERE "+spalte+ " ="+fromSpalte);
- }
- public ResultSet getWertfromNULL(String table, String spalte){
- return getResult("SELECT * FROM "+table+" WHERE "+spalte+ " ="+null);
- }
- public ResultSet sortierTable(String table, String bySpalte){
- return getPrepairedResult("SELECT * FROM "+table+" ORDER BY "+bySpalte+" DESC");
- }
- public ResultSet giveTop3(String table, String bySpalte){
- return getPrepairedResult("SELECT * FROM "+table+" ORDER BY "+bySpalte+" DESC LIMIT 3");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement