0

I'm developing an application with eclipse and using an object related mapping (ORMLite) to access my mysql server. The datalayer is another project compiled in java and bound as a reference to my android application.

I've referenced in the datalayer project every *.jar file used with ormlite. But when running my application I get this error message:

"Could not find class 'com.j256.ormlite.jdbc.JdbcConnectionSource',
     referenced from method
     com.pos.datalayer.querybuilder.DatabaseWorker.connectionSetup"

Here is my code of the DatabaseWorker-class:

public class DatabaseWorker implements DatalayerWorkable {

  private Type tableMapping = null;
  private String actionOnDatabase = null;
  private UserBO persistedUser = null;
  private Type retVal = null;

  public static String hostString = "jdbc:mysql://lucid.selfhost.me/pos";
  private static JdbcConnectionSource connection = null;

  public DatabaseWorker () { };

  public Type loadData(  String databaseAction, final Type tableMap ) {
      actionOnDatabase = databaseAction;
      tableMapping = tableMap;

        Thread workerThread = new Thread(new Runnable(){
            public void run() {
                try {
                    connection = connectionSetup();

                    if(actionOnDatabase == "GetLogin"){
                        retVal = loadUserData(tableMap); 

                  }else if (actionOnDatabase == "loadHotDealz"){

                  }
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    if(connection != null){
                        //closeConnection(connection);
                        connection = null;
                    }
                }
            }  
          });
         workerThread.start();

    return retVal;
  }

  public boolean persistData( String databaseAction, Type tableMap ) {
      Thread workerThread = new Thread(new Runnable(){
          public void run(){
          }
      }); 
      return false;
  }

  private JdbcConnectionSource connectionSetup() throws SQLException{
        //set up sql-connection
        JdbcConnectionSource connection = new JdbcConnectionSource(hostString);
        ((JdbcConnectionSource) connection).setUsername("guest");
        ((JdbcConnectionSource) connection).setPassword("ObivanK3nobi");

        return connection;
  }

    private void closeConnection(JdbcConnectionSource openedConnection){
        try{
            if(openedConnection != null){
                openedConnection.close();
            }
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private Type loadUserData(Type tableMapping) throws SQLException{
        Dao<UserBO, String> accountDao = null;

        if(connection != null){
            accountDao = DaoManager.createDao(connection, tableMapping.getClass());
            String currentUser = ((UserBO) tableMapping).getUser();
            //execute query
            persistedUser = accountDao.queryForId(currentUser);

            if(persistedUser != null){
               persistedUser.setIsLoggedin(1);
               accountDao.update(persistedUser);
            }
        }
        return persistedUser;
    }

    public Type getDatabaseResult(){
        return retVal;
    }

    public void databaseObjCallback(Type databaseResult) {
        throw new UnsupportedOperationException("Not implemented yet");
    }

}

I would appreciate any help to solve my problem!

Gray
  • 115,027
  • 24
  • 293
  • 354
Ralph Lo
  • 33
  • 1
  • 10

1 Answers1

2

I would think that this is a classpath issue somehow if you are not finding key ORMLite classes. You should have downloaded the ormlite-core and ormlite-jdbc jars with the same version number. The latest version is 4.45.

In eclipse, under your project, you should see a "Referenced Libraries" folder or, if you are using Maven, a "Maven Dependencies" folder. Both ORMLite jars should be in one of those two folders showing that they are in the build path.

You may need to right click on your project, pull down the menu to "Build Path", and then pull down to "Configure Build Path". Then click on the "Libraries" tab and make sure both ORMLite jars are there. If they aren't then you need to add them there.

In terms of MySQL JDBC connector versions, I use version 5.1.18 but I would think newer ones would work as well. I don't think that an incompatible version would cause that error.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.18</version>
</dependency>

Hope this helps.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • Hi Gray, thank you for the quick response! Specifically I'm not using ORMLite under Android. I'm using ORMLite in a java-project which is being referenced in an android project. The java project containing the ORMlite references therefor doesn't knows anything about android. – Ralph Lo Apr 30 '13 at 09:26
  • Hi Gray, me again. I think this error could also accure because of the wrong jdk version in my java project. Could you tell me which jdk version is supported on using ormlite V4.45 and which mysqlconnector version I do need! Thank you in advance. – Ralph Lo Apr 30 '13 at 09:36
  • ORMLite should be Java1.5 compliant. Every once and a while a 1.6 feature creeps in but I would expect a compilation error, not a class not found @RalphHeerich. I'll edit my answer to be not about Android. – Gray Apr 30 '13 at 12:38
  • Hi Gray, after still messing around with the same error, I found a question here on stackoverflow.com which could solve my problem. I just wanted to know if this could cause the issue of getting the "no class found exception". The answers in the question i found point to proguard. Do you think i should modify the proguard settings? Here is the link to the issue i found: http://stackoverflow.com/questions/15686593/java-lang-classnotfoundexception-in-dalvik-system-basedexclassloader-findclass Thank you for the help!! I really appreciate it ;-). Greetings Ralph – Ralph Lo May 21 '13 at 08:33
  • Sorry @RalphHeerich. I have no experience with proguard. Sounds like it could be the culprit. – Gray May 21 '13 at 12:10
  • Hi Gray, thank you for your answer. I'll evaluate it and will post my results here! Best regards, Ralph – Ralph Lo May 21 '13 at 13:10
  • Hi Gray, I solved the problem. Fortunately I saved my old projects where I had ORMLite working in an Android App, I used one of these projects to evaluate the problem. The only difference between the old android projects and the recent one is that the current one used Android 4.2.2 libraries and the old one used Android 4.1.2 libraries. I created a new project using android 4.1.2 libraries and referencing the datalayer java-project in my android app solved the problem. Best regards, Ralph – Ralph Lo May 22 '13 at 12:07
  • can you tell me something about the SSL implementation in ormlite? Dies ormlite uses a public key of the Android keystore to authenticate against a server? If not, where do I habe to store the SSL public key for usage in the Android App? Thank you in advance! – Ralph Lo Mar 15 '14 at 17:00
  • I have no idea @RalphHeerich. ORMLite doesn't do anything with SSL since the database is local. If you figure out that something is missing, I'd be happy to entertain making changes. – Gray Mar 15 '14 at 21:37
  • My fault, mixed things up. I want to use a mysql server with ssl authentication. You can specify ssl=true for the connection string which can be used at JDBConnectionSource constructor to prepare a SSL connection. But I dont know how to implementent SSL connection any further. Do you know which public key is used and where it's stored in an android project? – Ralph Lo Mar 16 '14 at 10:01
  • Sorry I don't know @RalphHeerich. Sounds like some google-ing is in order. – Gray Mar 16 '14 at 13:54