1

I'm trying to connect to my MYSQL database on db4free directly, security/reliability is of no concern here, it should just work good enough for a minimum viable product that will be later totally reconstructed from scratch, that's why I'm trying to connect this way. This is just some kind of concept application.

For one of the requirements, we need a server database.

I'm using ORMLite-core paired with ORMLite-jdbc, and trying the following code in Android Studio:

public class DatabaseHelper {

private ConnectionSource connectionSource;
private Dao<User, Integer> userDao;
private Dao<Settings, Integer> settingsDao;
private Dao<Prize, Integer> itemDao;
private Dao<CulturalCenter, Integer> localDao;
private Dao<Event, Integer> eventDao;

private static final DatabaseHelper dbHelper = new DatabaseHelper();

public static DatabaseHelper getInstance() {
    return dbHelper;
}


private DatabaseHelper() {
    try {
        this.connectionSource = new JdbcConnectionSource("jdbc:mysql://85.10.205.173/mydb", "myacc", "mypass");

        Log.d("Connection", "Do we reach here?" + connectionSource.getDatabaseType().getDatabaseName());

        /**
         * DAOS
         */
        this.userDao = DaoManager.createDao(connectionSource, User.class);
        TableUtils.createTable(connectionSource, User.class);
   } catch (SQLException e) {
        e.printStackTrace();
    }
}

public Dao<User, Integer> getUserDao() {
    return userDao;
}

This crashes on the TableUtils with the following error:

D/Connection: Do we reach here?MySQL W/art: Suspending all threads took: 8.202ms I/art: Background partial concurrent mark-sweep GC freed 5335(349KB) AllocSpace objects, 4(110KB) LOS objects, 40% free, 3MB/5MB, paused 9.637ms total 33.576ms I/TableUtils: creating table 'user' W/System.err: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure W/System.err: The last packet sent to the server was 0 ms ago.

I'm unable to connect like this, but I can connect just fine in my MySQL workbench on my windows pc with the credentials I provided in my code. I've googled a lot but I'm still very inexperienced working with databases on Android, and couldn't find any viable solution so far, I'd appreciate any help!

Thank you!

Gray
  • 115,027
  • 24
  • 293
  • 354
  • Welcome to StackOverflow! First, I wouldn't think of your application as a concept application only, because more often than not, concept code which was written in a quick and dirty manner will be used in the final application because "it works" I would therefore always suggest that you put all the effort into the concept app as if it was a product sold to customers. Secondly, did you verify that your database is actually reachable from your phone (and not sitting behind some firewall)? – vatbub Aug 11 '18 at 14:14
  • Hi! Thank you for the welcoming words! I was said that the app would indeed be completely rewrited by a 'proper' company (we're a small team of three developers helping a friend), however, we don't have a lot of time left to do this properly, unfortunely. Sorry, I'm not sure how to check that! I've tried using WAMP on my local computer and still no success :\ – Rui Almeida Aug 11 '18 at 14:45
  • Usually, you can open Google Chrome on th ephone and open the database's ip address and see if it works... – vatbub Aug 11 '18 at 14:47
  • I got it working! It was a silly mistake on my end. I was using localhost instead of 10.0.2.2 ip on my android. I managed to connect to my local database hehe, thanks for your help tho! – Rui Almeida Aug 11 '18 at 15:26

0 Answers0