fauzt
fauzt

Reputation: 81

Can not Drivermanager.getConnection(...) in android app

I have a problem to connect on my DB from android application. The lib for connect (mysql-connector-java.5.1.41-bin) is added to project.

Function for connect and insert to DB

public void insert(String SMS_ID, String SMS_ADRESS, String SMS_TEXT, String SMS_CREATOR, String SMS_DATE) throws SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://192.168.1.14:3306/smschecker";
            Connection c = DriverManager.getConnection(url, "root", "root"); //there it jumps to catch (ClassNotFoundException e)
            PreparedStatement st = c.prepareStatement("insert into SMS values (?,?,?,?,?)");

            st.setString(1, SMS_ID);
            st.setString(2, SMS_ADRESS);
            st.setString(3, SMS_TEXT);
            st.setString(4, SMS_CREATOR);
            st.setString(5, SMS_DATE);

            st.execute();
            st.close();
            c.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

Screenshots from DB GUI

https://i.sstatic.net/vZ1bv.jpg //Connection informations

https://i.sstatic.net/ODDPm.jpg //Users

The problem is may be between users, that the app has no permission to enter the DB, but I do not know how to add a permission for my app.

Upvotes: 2

Views: 2238

Answers (1)

N00b Pr0grammer
N00b Pr0grammer

Reputation: 4647

Based on the screenshot that you've posted in your comments, identified the issue.

Caused by: android.os.NetworkOnMainThreadException

You just can't be doing a Network IO on the main thread, you might have to move that to an Async task or a new thread.

Hope that this helps!

Upvotes: 2

Related Questions