Problem with Ucanaccess ClassNotFoundEsception

Discussion in 'Plugin Development' started by brunodm99, Aug 14, 2016.

Thread Status:
Not open for further replies.
  1. I'm developing a plugin that connects to a database access using the driverucanaccess, I have imported bookstore, here I leave the code:

    ConnectionDB.java
    Code:
    package com.gmail.bruno.primerpluginmc.SQLConnection;
    
    import com.gmail.bruno.primerpluginmc.Main;
    import java.io.File;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class ConnectionDB {
      
        private Connection conexion;
        private Statement st;
       
        private final String driver;
        private final String db_name;
               
        public ConnectionDB(){
           
            driver="net.ucanaccess.jdbc.UcanaccessDriver";
            this.db_name= Main.instance.getDataFolder()+ File.separator + "players.mdb";
           
            try{
                Class.forName(driver);
            }catch(ClassNotFoundException e){
                Logger.getLogger(ConnectionDB.class.getName()).log(Level.SEVERE, null, e);
            }
        }
       
        public boolean EstablecerConexion() throws SQLException{
            try{
                Class.forName(driver);
                conexion = DriverManager.getConnection("jdbc:ucanaccess://"+db_name);
            }catch(SQLException e){
                Main.instance.getLogger().log(Level.INFO, "SQLException! {0}", e);
                return false;
            }catch (ClassNotFoundException ex) {
                Logger.getLogger(ConnectionDB.class.getName()).log(Level.SEVERE, null, ex);
            }
           
            try{
                this.st=this.conexion.createStatement(
                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY);
            }catch(SQLException e){
                Main.instance.getLogger().log(Level.INFO, "SQLException! {0}", e);
                return false;
            }
            return true;
        }
       
        public ResultSet Ejecutar(String sql) throws SQLException{
            ResultSet rs;
            rs = st.executeQuery(sql);
            return rs;
        }
       
    }
    
    ControllerDB.java
    Code:
    package com.gmail.bruno.primerpluginmc.SQLConnection;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    
    public class ControllerDB {
       
        private final ConnectionDB con;
        public ControllerDB(){
           
            this.con = new ConnectionDB();
           
        }
       
        public void CrearConexion() throws SQLException{
            try{
                Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            }catch(ClassNotFoundException e){
                Logger.getLogger(ConnectionDB.class.getName()).log(Level.SEVERE, null, e);
            }
           
            this.con.EstablecerConexion();
        }
       
        public ResultSet mandarSql(String sql) throws SQLException{
           
            ResultSet rs=con.Ejecutar(sql);
            return rs;
           
        }
    }
    
    
    And I'm calling from another class

    Code:
     private HashMap<Player, Integer> entidadesMatadas = new HashMap<>();
       
         @EventHandler
         public void onKill(EntityDeathEvent event) throws SQLException{
         String killed = event.getEntity().getName();
         String killer1 = event.getEntity().getKiller().getName();
        
         Player killer = event.getEntity().getKiller();
        
          int kills = 0;
            if(entidadesMatadas.containsKey(killer)){
                kills = entidadesMatadas.get(killer);
            }
            kills ++;
            entidadesMatadas.put(killer, kills);
           
            ejecutar(killer1, kills);
         }
        
         public void ejecutar(String killer1, int kills) throws SQLException{
            
                try{
                    controller = new ControllerDB();
                    controller.CrearConexion();
                }catch(SQLException e){
                    plugin.getLogger().log(Level.INFO, "SQLException! {0}", e);
                }
               
                String sql = "SELECT * FROM jugadores WHERE nombre="+killer1;
               
                rs=controller.mandarSql(sql);
               
                if(rs.next() == false){
                    String sql2 = "INSERT INTO  jugadores (nombre, puntos) VALUES ("+killer1+","+kills+")";
                   
                    rs = controller.mandarSql(sql2);
                }else{
                String sql3 = "UPDATE jugadores SET nombre="+killer1+", puntos="+kills+" WHERE nombre="+killer1;
               
                rs = controller.mandarSql(sql);
               
         }
         }
    Add the server log.
     
  2. Code:
    Could not load 'plugins\PrimerPluginMc.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: Cannot find main class `com.bruno.primerpluginmc.Main'
    Bukkit can't find the main class (com.bruno.primerpluginmc.Main) defined in plugin.yml.

    As I see in your other classes, the package is actually
    com.gmail.bruno.primerpluginmc
     
  3. That I have it fixed and now I get this
    [Server thread/ERROR]: null java.lang.ClassNotFoundException: net.ucanaccess.jdbc.UcanaccessDriver

    That I have it fixed and now I get this
    [Server thread/ERROR]: null java.lang.ClassNotFoundException: net.ucanaccess.jdbc.UcanaccessDriver

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 14, 2016
  4. Offline

    Oxyorum

    You have to make sure that the class you are trying to retrieve (the driver) is accessible to the server. The error is telling you that the class you want is not available to the plugin for use.

    If you are familiar with Maven, just use maven shading or similar. I am sure you can look up a tutorial for other ways to do this on this site as well.
     
  5. Offline

    mythbusterma

    @brunodm99

    Why are you tacking unneccessary dependencies into your plugin? Nobody that uses Minecraft servers has Microsoft Access databases at the ready, and most hosting providers will have MySQL severs.

    Use MySQL instead, if you actually need SQL. As I've said before, though, avoid using SQL where a configuration file would suit your needs.

    The Bukkit server already includes the MySQL driver, so you wouldn't need to balloon up your plugin with the dependencies.
     
Thread Status:
Not open for further replies.

Share This Page