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 MySQL(Main main) {
- this.plugin = main;
- File file =new File(plugin.getDataFolder(), "MySQL.yml");
- FileConfiguration sql = YamlConfiguration.loadConfiguration(file);
- String m = "MySQL.";
- sql.addDefault(m + "host", "localhost");
- sql.addDefault(m + "port", Integer.valueOf(3306));
- sql.addDefault(m + "database", "database");
- sql.addDefault(m + "username", "username");
- sql.addDefault(m + "password", "password");
- sql.options().copyDefaults(true);
- try {
- sql.save(file);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- host = sql.getString(m + "host");
- port = sql.getInt(m + "port");
- database = sql.getString(m + "database");
- username = sql.getString(m + "username");
- password = sql.getString(m + "password");
- //password ="";
- }
- public void connect() {
- try{
- con = DriverManager.getConnection("jdbc:mysql://" + host + ":"+ port + "/" + database+ "?autoReconnect=true", username, password);
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- public boolean isConected() {
- try {
- if(con.isClosed()|| con == null){
- return false;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return true;
- }
- public void disconect() {
- if(con != null){
- try {
- con.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
- public ResultSet query(String query) {
- if(!isConected()){
- 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(!isConected()){
- connect();
- }
- try {
- Statement stm = con.createStatement();
- System.out.println("Ausgefuehrtes Abfrage: "+query);
- stm.executeUpdate(query);
- } catch (SQLException ex) {
- connect();
- ex.printStackTrace();
- }
- }
- 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(isConected()){
- 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(isConected()){
- 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");
- }
- public static int getRank(Player p){
- int i = 1;
- try {
- PreparedStatement ps = con.prepareStatement("SELECT * FROM JumpStats ORDER BY POINTS DESC");
- ResultSet rs = ps.executeQuery();
- rs.first();
- while (!rs.isAfterLast()) {
- if(rs.getString("UUID").equals(p.getUniqueId().toString()))
- return i;
- i++;
- rs.next();
- }} catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement