如何从Android应用程序发送数据到SQL数据库?

时间:2018-04-17 18:38:12

标签: java android sql android-studio jdbc

我在从我开发的Android应用程序向sql数据库发送基本数据时遇到了很多麻烦。我有一个基本的连接类,然后我认为这是一个非常简单的查询,应该发生在点击按钮上....

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());
    }
}

但无论我尝试什么,我都会继续得到一个IllegalStateException,它指向“与con.createStatement();' 我的连接类

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;
    }
}

0 个答案:

没有答案