Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Reference a mysql table from java code

Options

Comments

  • Moderators, Sports Moderators, Regional Abroad Moderators Posts: 2,646 Mod ✭✭✭✭TrueDub


    I have a mysql database called "bars" on my local PC that i can get to with the following address in my browser.

    http://localhost/phpmyadmin/db_structure.php?db=bars#PMAURL-0:db_structure.php?db=bars&table=&server=1&target=&token=dc9eefce21adff97525fbf0feb8ccf3e


    However, i don't know how to code the address of it in the Java code to set up connection. What should be in the below?

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/bars";

    you're missing the port number, which is probably 3306, assuming you installed MySQL with the default options.

    So your URL should be jdbc:mysql://localhost:3306/bars


  • Registered Users Posts: 5,557 ✭✭✭veryangryman


    Thanks i think thats correct - however there is mention of StrictMode below. I think this needs to be disabled before it will work but im not sure how

    01-08 16:00:49.159 2916-2916/com.example.erayfra.myapplication W/System.err﹕ [ 01-08 16:00:49.159 2916: 2916 W/System.err ]
    android.os.NetworkOnMainThreadException
    01-08 16:00:49.159 2916-2916/com.example.user.myapplication W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)


  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    NetworkOnMainThreadException is the thing to Google here. It's considered bad practice to do work on the UI thread as it makes applications unresponsive. It looks like Android goes out of it's way to throw an exception if you try.


  • Registered Users Posts: 5,557 ✭✭✭veryangryman


    Sort of got around this but connection refused. I know this is running on port 3307 and i know the user and password is ok


    mysql> status
    C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe Ver 14.14 Distrib 5.6.21, for Win64 (x86_64)

    Connection id: 15
    Current database:
    Current user: root@localhost
    SSL: Not in use
    Using delimiter: ;
    Server version: 5.6.21-log MySQL Community Server (GPL)
    Protocol version: 10
    Connection: Shared memory: MYSQL
    Server characterset: utf8
    Db characterset: utf8
    Client characterset: utf8
    Conn. characterset: utf8
    TCP port: 3307
    Uptime: 19 hours 2 min 57 sec


    But still get done by exception

    01-09 11:50:41.611 1913-1913/com.example.user.myapplication W/System.err﹕ java.sql.SQLException: Server connection failure during transaction. Due to underlying exception: 'java.net.SocketException: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 3307): connect failed: ECONNREFUSED (Connection refused)'.


  • Closed Accounts Posts: 2,267 ✭✭✭h57xiucj2z946q


    Sort of got around this but connection refused. I know this is running on port 3307 and i know the user and password is ok


    mysql> status
    C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe Ver 14.14 Distrib 5.6.21, for Win64 (x86_64)

    Connection id: 15
    Current database:
    Current user: root@localhost
    SSL: Not in use
    Using delimiter: ;
    Server version: 5.6.21-log MySQL Community Server (GPL)
    Protocol version: 10
    Connection: Shared memory: MYSQL
    Server characterset: utf8
    Db characterset: utf8
    Client characterset: utf8
    Conn. characterset: utf8
    TCP port: 3307
    Uptime: 19 hours 2 min 57 sec


    But still get done by exception

    01-09 11:50:41.611 1913-1913/com.example.user.myapplication W/System.err﹕ java.sql.SQLException: Server connection failure during transaction. Due to underlying exception: 'java.net.SocketException: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 3307): connect failed: ECONNREFUSED (Connection refused)'.

    Your android app is trying to connect to localhost:3307 ? You have MySql on your laptop!


  • Advertisement
  • Registered Users Posts: 5,557 ✭✭✭veryangryman


    Your android app is trying to connect to localhost:3307 ? You have MySql on your laptop!

    Sorry to sound silly but im not following. I do have mysql running on laptop yes.


  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    So the Android app is using the wrong hostname.


  • Registered Users Posts: 50 ✭✭EamonnDunne


    What others are pointing out is that localhost is only accessible from the local machine, the laptop. Android can't access it using that name. It needs an IP address or hostname.

    Just also want to point out that having Android connect directly the database is very bad practice, ideally you should be exposing the database using a REST webservice. Regarding doing operations on the main thread which as you have seen is not allowed. You need to learn about AsyncTask developer.android.com/reference/android/os/AsyncTask.html


  • Registered Users Posts: 5,557 ✭✭✭veryangryman


    What others are pointing out is that localhost is only accessible from the local machine, the laptop. Android can't access it using that name. It needs an IP address or hostname.

    Just also want to point out that having Android connect directly the database is very bad practice, ideally you should be exposing the database using a REST webservice. Regarding doing operations on the main thread which as you have seen is not allowed. You need to learn about AsyncTask developer.android.com/reference/android/os/AsyncTask.html

    So this...?

    static final String DB_URL = "jdbc:mysql://137.58.72.178:3307/bars?autoReconnect=true";

    Agree with point 2 - just want to see how the syntax of code works - will be doing it on a remote web server when app is put out - this is my first time to use a DB in it. It shows ;)


  • Registered Users Posts: 50 ✭✭EamonnDunne


    So this...?

    static final String DB_URL = "jdbc:mysql://137.58.72.178:3307/bars?autoReconnect=true";

    Agree with point 2 - just want to see how the syntax of code works - will be doing it on a remote web server when app is put out - this is my first time to use a DB in it. It shows ;)

    Its highly likely that IP address won't work as its your IP on the internet (I'm guessing). It will work if you open ports on your router and do port forwarding. However thats extremely bad security, you should be accessing it using your computers internal LAN IP address. These internal addresses typically start with 192. or 10.


  • Advertisement
  • Closed Accounts Posts: 2,267 ✭✭✭h57xiucj2z946q


    I recognise that IP address range, and that signum!, Someone doing side projects in work? :-p

    If that IP don't allow incomming connections on port 3307 from whever your android connects from, you will still get conmection refused.

    Are you using your phone or emulator?


  • Registered Users Posts: 50 ✭✭EamonnDunne


    I recognise that IP address range, and that signum!, Someone doing side projects in work? :-p

    If that IP don't allow incomming connections on port 3307 from whever your android connects from, you will still get conmection refused.

    Are you using your phone or emulator?

    Yeah we all now know what company you both work for ;)


  • Closed Accounts Posts: 2,267 ✭✭✭h57xiucj2z946q


    Yeah we all now know what company you both work for ;)

    Haha as of lately, I don't work for them anymore!


  • Registered Users Posts: 5,557 ✭✭✭veryangryman


    Using the laptop for now.


  • Closed Accounts Posts: 2,267 ✭✭✭h57xiucj2z946q


    Using the laptop for now.

    So if you are using the Android SDK emulator to run your app, you can use 1.0.2.2 to collect to your dev machine.

    Have a read here:
    http://developer.android.com/tools/devices/emulator.html#networkaddresses


Advertisement