Database Fehler

Dieses Thema im Forum "Programmierung" wurde erstellt von Godcraft, 3. April 2014.

  1. Godcraft
    Offline

    Godcraft

    Registriert seit:
    2. Dezember 2012
    Beiträge:
    36
    Code (Text):
    1. package DuB.lv;
    2.  
    3. import com.google.common.collect.ImmutableMap;
    4. import java.sql.Connection;
    5. import java.sql.DatabaseMetaData;
    6. import java.sql.DriverManager;
    7. import java.sql.PreparedStatement;
    8. import java.sql.ResultSet;
    9. import java.sql.SQLException;
    10. import java.sql.Statement;
    11. import java.util.Map;
    12. import java.util.logging.Logger;
    13.  
    14. public class Database
    15. {
    16.   private String user;
    17.   private String pass;
    18.   private String url;
    19.   public Logger log;
    20.   private Connection connection;
    21.   private BungeePortals plugin;
    22.   private boolean queryInProgress;
    23.   private ImmutableMap<String, String> tables;
    24.  
    25.   public Database(String user, String pass, String url, BungeePortals instance, Map<String, String> tables)
    26.   {
    27.     this.queryInProgress = false;
    28.  
    29.     this.user = user;
    30.     this.pass = pass;
    31.     this.url = url;
    32.     this.plugin = instance;
    33.  
    34.     this.tables = ImmutableMap.copyOf(tables);
    35.   }
    36.  
    37.   private boolean initialize() {
    38.     try {
    39.       Class.forName("com.mysql.jdbc.Driver");
    40.       return true;
    41.     } catch (ClassNotFoundException e) {
    42.       this.plugin.getLogger().severe("MySQL driver class missing: " + e.getMessage() + ".");
    43.     }return false;
    44.   }
    45.  
    46.   public String getTable(String name)
    47.   {
    48.     return (String)this.tables.get(name);
    49.   }
    50.  
    51.   public boolean checkConnection() {
    52.     if (open() == null)
    53.       return false;
    54.     return true;
    55.   }
    56.  
    57.   public Connection open() {
    58.     if (!initialize())
    59.       return null;
    60.     try {
    61.       if (this.connection == null)
    62.         return DriverManager.getConnection(this.url, this.user, this.pass);
    63.       if (this.connection.isValid(3)) {
    64.         return this.connection;
    65.       }
    66.  
    67.       return DriverManager.getConnection(this.url, this.user, this.pass);
    68.     }
    69.     catch (SQLException e) {
    70.       this.plugin.getLogger().severe(this.url);
    71.       this.plugin.getLogger().severe("Could not be resolved because of an SQL Exception: " + e.getMessage() + ".");
    72.     }
    73.     return null;
    74.   }
    75.  
    76.   public void close() {
    77.     if (this.queryInProgress) {
    78.       return;
    79.     }
    80.     this.connection = open();
    81.     try {
    82.       if (this.connection != null) {
    83.         this.connection.close();
    84.         this.connection = null;
    85.       }
    86.     } catch (Exception e) {
    87.       this.plugin.getLogger().severe("Failed to close database connection: " + e.getMessage());
    88.     }
    89.   }
    90.  
    91.   public ResultSet query(String query) {
    92.     Statement statement = null;
    93.     ResultSet result = null;
    94.     this.queryInProgress = true;
    95.     try {
    96.       this.connection = open();
    97.       statement = this.connection.createStatement();
    98.      
    99.       switch (1.$SwitchMap$DuB$lv$BungeePortals$Database$Statements[getStatement(query).ordinal()]) {
    100.       case 1:
    101.         result = statement.executeQuery(query);
    102.         this.queryInProgress = false;
    103.         return result;
    104.       }
    105.  
    106.       statement.executeUpdate(query);
    107.       this.queryInProgress = false;
    108.       return result;
    109.     }
    110.     catch (SQLException e) {
    111.       this.plugin.getLogger().warning("Error in SQL query: " + e.getMessage());
    112.       this.plugin.getLogger().warning(query);
    113.     }
    114.     return result;
    115.   }
    116.  
    117.   public int updateQuery(String query) {
    118.     Connection connection = null;
    119.     Statement statement = null;
    120.     try {
    121.       connection = open();
    122.       statement = connection.createStatement();
    123.       return statement.executeUpdate(query);
    124.     }
    125.     catch (SQLException e) {
    126.       this.plugin.getLogger().warning("Error in SQL query: " + e.getMessage());
    127.       this.plugin.getLogger().warning(query);
    128.     }
    129.     return 0;
    130.   }
    131.  
    132.   public PreparedStatement prepare(String query) {
    133.     Connection connection = null;
    134.     PreparedStatement ps = null;
    135.     try {
    136.       connection = open();
    137.       return connection.prepareStatement(query);
    138.     }
    139.     catch (SQLException e) {
    140.       if (!e.toString().contains("not return ResultSet")) {
    141.         this.plugin.getLogger().warning("Error in SQL prepare() query: " + e.getMessage());
    142.         this.plugin.getLogger().warning(query);
    143.       }
    144.     }
    145.     return ps;
    146.   }
    147.  
    148.   protected Statements getStatement(String query) {
    149.     String trimmedQuery = query.trim();
    150.     if (trimmedQuery.substring(0, 6).equalsIgnoreCase("SELECT"))
    151.       return Statements.SELECT;
    152.     if (trimmedQuery.substring(0, 6).equalsIgnoreCase("INSERT"))
    153.       return Statements.INSERT;
    154.     if (trimmedQuery.substring(0, 6).equalsIgnoreCase("UPDATE"))
    155.       return Statements.UPDATE;
    156.     if (trimmedQuery.substring(0, 6).equalsIgnoreCase("DELETE"))
    157.       return Statements.DELETE;
    158.     if (trimmedQuery.substring(0, 6).equalsIgnoreCase("CREATE"))
    159.       return Statements.CREATE;
    160.     if (trimmedQuery.substring(0, 5).equalsIgnoreCase("ALTER"))
    161.       return Statements.ALTER;
    162.     if (trimmedQuery.substring(0, 4).equalsIgnoreCase("DROP"))
    163.       return Statements.DROP;
    164.     if (trimmedQuery.substring(0, 8).equalsIgnoreCase("TRUNCATE"))
    165.       return Statements.TRUNCATE;
    166.     if (trimmedQuery.substring(0, 6).equalsIgnoreCase("RENAME"))
    167.       return Statements.RENAME;
    168.     if (trimmedQuery.substring(0, 2).equalsIgnoreCase("DO"))
    169.       return Statements.DO;
    170.     if (trimmedQuery.substring(0, 7).equalsIgnoreCase("REPLACE"))
    171.       return Statements.REPLACE;
    172.     if (trimmedQuery.substring(0, 4).equalsIgnoreCase("LOAD"))
    173.       return Statements.LOAD;
    174.     if (trimmedQuery.substring(0, 7).equalsIgnoreCase("HANDLER"))
    175.       return Statements.HANDLER;
    176.     if (trimmedQuery.substring(0, 4).equalsIgnoreCase("CALL")) {
    177.       return Statements.CALL;
    178.     }
    179.     return Statements.SELECT;
    180.   }
    181.  
    182.   public boolean createTable(String query) {
    183.     Statement statement = null;
    184.     try {
    185.       this.connection = open();
    186.       if ((query.equals("")) || (query == null)) {
    187.         this.plugin.getLogger().severe("SQL query empty: createTable(" + query + ")");
    188.         return false;
    189.       }
    190.       statement = this.connection.createStatement();
    191.       statement.execute(query);
    192.       return true;
    193.     } catch (SQLException e) {
    194.       this.plugin.getLogger().severe(e.getMessage());
    195.       return false;
    196.     } catch (Exception e) {
    197.       this.plugin.getLogger().severe(e.getMessage());
    198.     }return false;
    199.   }
    200.  
    201.   public boolean checkTable(String table)
    202.   {
    203.     try {
    204.       this.connection = open();
    205.  
    206.       if (this.connection == null) {
    207.         this.plugin.getLogger().severe("Unable to check if tables exist");
    208.         return false;
    209.       }
    210.       Statement statement = this.connection.createStatement();
    211.  
    212.       ResultSet result = statement.executeQuery("SELECT * FROM " + table);
    213.  
    214.       if (result == null)
    215.         return false;
    216.       if (result != null)
    217.         return true;
    218.     } catch (SQLException e) {
    219.       if (e.getMessage().contains("exist")) {
    220.         return false;
    221.       }
    222.       this.plugin.getLogger().info("Error in SQL query: " + e.getMessage());
    223.     }
    224.  
    225.     if (query("SELECT * FROM " + table) == null)
    226.       return true;
    227.     return false;
    228.   }
    229.  
    230.   public boolean colExists(String table, String column) {
    231.     try {
    232.       this.connection = open();
    233.  
    234.       if (this.connection == null) {
    235.         this.plugin.getLogger().severe("Unable to check if tables exist");
    236.         return false;
    237.       }
    238.  
    239.       DatabaseMetaData metadata = this.connection.getMetaData();
    240.  
    241.       ResultSet result = metadata.getColumns(null, null, table, column);
    242.       if (result == null)
    243.         return false;
    244.       if ((result != null) &&
    245.         (result.next())) {
    246.         result.close();
    247.         return true;
    248.       }
    249.     }
    250.     catch (SQLException e) {
    251.       if (e.getMessage().contains("exist")) {
    252.         return false;
    253.       }
    254.       this.plugin.getLogger().info("Error in SQL query: " + e.getMessage());
    255.     }
    256.  
    257.     return false;
    258.   }
    259.  
    260.   protected static enum Statements
    261.   {
    262.     SELECT, INSERT, UPDATE, DELETE, DO, REPLACE, LOAD, HANDLER, CALL,
    263.  
    264.     CREATE, ALTER, DROP, TRUNCATE, RENAME;
    265.   }
    266. }
    Fehler ist switch (1.$SwitchMap$DuB$lv$BungeePortals$Database$Statements[getStatement(query).ordinal()])

    Code (Text):
    1. [19:03:20] [Server thread/ERROR]: Error occurred while enabling BungeePortals v1.1 (Is it up to date?)
    2. java.lang.Error: Unresolved compilation problems:
    3.     The type of the expression must be an array type but it resolved to double
    4.     Syntax error on token "$SwitchMap$DuB$lv$BungeePortals$Database$Statements", delete this token
    5.  
    6.     at DuB.lv.Database.query(Database.java:99) ~[?:?]
    7.     at DuB.lv.BungeePortals.onEnable(BungeePortals.java:96) ~[?:?]
    8.     at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) ~[spigot.jar:git-Spigot-1360]
    9.     at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:324) [spigot.jar:git-Spigot-1360]
    10.     at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-1360]
    11.     at org.bukkit.craftbukkit.v1_7_R2.CraftServer.loadPlugin(CraftServer.java:463) [spigot.jar:git-Spigot-1360]
    12.     at org.bukkit.craftbukkit.v1_7_R2.CraftServer.enablePlugins(CraftServer.java:381) [spigot.jar:git-Spigot-1360]
    13.     at org.bukkit.craftbukkit.v1_7_R2.CraftServer.reload(CraftServer.java:799) [spigot.jar:git-Spigot-1360]
    14.     at org.bukkit.Bukkit.reload(Bukkit.java:279) [spigot.jar:git-Spigot-1360]
    15.     at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:24) [spigot.jar:git-Spigot-1360]
    16.     at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) [spigot.jar:git-Spigot-1360]
    17.     at org.bukkit.craftbukkit.v1_7_R2.CraftServer.dispatchCommand(CraftServer.java:709) [spigot.jar:git-Spigot-1360]
    18.     at org.bukkit.craftbukkit.v1_7_R2.CraftServer.dispatchServerCommand(CraftServer.java:696) [spigot.jar:git-Spigot-1360]
    19.     at net.minecraft.server.v1_7_R2.DedicatedServer.ax(DedicatedServer.java:309) [spigot.jar:git-Spigot-1360]
    20.     at net.minecraft.server.v1_7_R2.DedicatedServer.v(DedicatedServer.java:274) [spigot.jar:git-Spigot-1360]
    21.     at net.minecraft.server.v1_7_R2.MinecraftServer.u(MinecraftServer.java:566) [spigot.jar:git-Spigot-1360]
    22.     at net.minecraft.server.v1_7_R2.MinecraftServer.run(MinecraftServer.java:472) [spigot.jar:git-Spigot-1360]
    23.     at net.minecraft.server.v1_7_R2.ThreadServerApplication.run(SourceFile:618) [spigot.jar:git-Spigot-1360]
    Ist aus dem Ban Management
    Ich möchte in das Bungeeporals eine Abfrage rein schreiben ob die Leute gebannt sind, kommischerweise ist die switch anweißung falsch und ich kann nicht auf die datenbanken connecten
     
    #1
  2. Paulomart
    Offline

    Paulomart

    Registriert seit:
    3. November 2013
    Beiträge:
    148
    Ort:
    C:/Eclipse/Workspace/
    Minecraft:
    Paulomart
    Code (Text):
    1.  switch (1.$SwitchMap$DuB$lv$BungeePortals$Database$Statements[getStatement(query).ordinal()]) {
    Da ist irgentwas faul.. Wofür ist das?
     
    #2
  3. Godcraft
    Offline

    Godcraft

    Registriert seit:
    2. Dezember 2012
    Beiträge:
    36
    ja wie gesagt ich möchte blos wie bei dem ban plugin die funktion kopieren aber es will ja nicht :(
    der swich frägt nach meiner meinung ab also getStatement ob der Double wert erreicht wird und der case setzt ein aber da blick ich net durch und was der programmierer sich dabei gedacht hat habe ich ebenfalls keine ahnung <<
     
    #3
  4. Benni1000
    Offline

    Benni1000 Ehem. Teammitglied

    Registriert seit:
    4. Mai 2012
    Beiträge:
    1.408
    #4
    1 Person gefällt das.
  5. Godcraft
    Offline

    Godcraft

    Registriert seit:
    2. Dezember 2012
    Beiträge:
    36
    jou der java decomailer hat dort ein fehler reingehauen << thx
     
    #5