0

I am having lots of trouble with sending basic data to a sql database from an android app that I have developed. I have a basic connection class, and then what I believe to be a very simple query that should occur on a button click like....

public void onUploadTest(View v) {
    try{
        Connection con = connectionClass.Conn();
        String query = "INSERT INTO photo (photoID, userID, date, title, description, location, selected) VALUES ("
                + 100 + ", "
                + 2 + ", "
                + "4/17/2018" + ", "
                + "awesome" + ", "
                + "yeppp" + ", "
                + "google.com" + ", "
                + 1
                +  ");";
        Statement stmt = con.createStatement();
        stmt.execute(query);
        con.close();
    } catch(SQLException e) {
        Log.d("sql", e.toString());
    } catch(IllegalStateException e) {
        Log.d("sql", e.toString());
    }
}

but not matter what I try I keep getting an IllegalStateException that points back to `the line with 'con.createStatement();' My connection class

public class ConnectionClass {

    String ip = "206.212.36.194";
    String db = "DroneInspDB";
    String un = "aksenov";
    String password = "Datapass123";

    //String ConnURL = null;
    @SuppressLint("NewApi")
    public Connection Conn() {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection conn = null;
        //String ConnURL =  null;

        try {
            Class.forName("net.sourceforge.jtds.jdbs.Driver");
            String ConnURL = "jdbc:jtds:sqlserver://" + db + ";user=" + un + ";password=" + password + ";";
            conn = DriverManager.getConnection(ConnURL);
        } catch(SQLException se) {
            Log.d("sql ", se.toString());
        } catch(ClassNotFoundException e) {
            Log.d("sql ", e.toString());
        } catch(NullPointerException e) {
            Log.d("sql", e.toString());
        } catch(Exception e){
            Log.d("sql ", e.toString());
        }

        return conn;
    }
}
Dorado
  • 411
  • 2
  • 15
  • Please read: [JDBC vs Web Service for Android](https://stackoverflow.com/q/15853367/295004) – Morrison Chang Apr 17 '18 at 18:42
  • Is the database on the phone or on a remote server? If it's on a phone, try the new ROOM database Google recommends for Android applications, https://developer.android.com/topic/libraries/architecture/room.html – Michael Osofsky Apr 17 '18 at 18:44
  • please elaborate what you're actually trying to achieve. Are you trying to send data to a server or you wanne write the data to a local database on the phone? – Peppermint Paddy Apr 17 '18 at 20:11
  • Sorry, trying to send to a remote database using JDBC. Databse is not on the phone. @PeppermintPaddy – flyingtornado65 Apr 17 '18 at 22:14

0 Answers0