Android连接到Azure SQL数据库并选择语句

时间:2016-05-05 10:02:23

标签: android mysql sql sql-server azure

我正在尝试将Android移动应用程序连接到Azure门户上的数据库。我在下面有我的连接类(显然有来自Azure Connection的正确字段)

public class ConnectionClass {

String classs = "net.sourceforge.jtds.jdbc.Driver";

@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(classs);
        ConnURL = "jdbc:jtds:sqlserver://name.database.windows.net:1433;database=name;user=un;password=xxx;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
        conn = DriverManager.getConnection(ConnURL);
    } catch (SQLException se) {
        Log.e("ERRO", se.getMessage());
    } catch (ClassNotFoundException e) {
        Log.e("ERRO", e.getMessage());
    } catch (Exception e) {
        Log.e("ERRO", e.getMessage());
    }
    return conn;
}
}

我试图从数据库中的表中读取如下内容:

    public class FillList extends AsyncTask<String, String, String> {
    String z = "";

    List<Map<String, String>> prolist  = new ArrayList<Map<String, String>>();

    @Override
    protected void onPreExecute() {

        pbbar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(String r) {

        pbbar.setVisibility(View.GONE);
        Toast.makeText(AddProducts.this, r, Toast.LENGTH_SHORT).show();

        String[] from = { "A", "B", "C" };
        int[] views = { R.id.lblproid, R.id.lblproname,R.id.lblprodesc };
        final SimpleAdapter ADA = new SimpleAdapter(AddProducts.this,
                prolist, R.layout.lsttemplate, from,
                views);
        lstpro.setAdapter(ADA);


        lstpro.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int arg2, long arg3) {
                HashMap<String, Object> obj = (HashMap<String, Object>) ADA
                        .getItem(arg2);
                proid = (String) obj.get("A");
                String proname = (String) obj.get("B");
                String prodesc = (String) obj.get("C");
                edtprodesc.setText(prodesc);
                edtproname.setText(proname);
           //     qty.setText(qtys);
            }
        });



    }

    @Override
    protected String doInBackground(String... params) {

        try {
            Connection con = connectionClass.CONN();
            if (con == null) {
                z = "Error in connection with SQL server";
            } else {
                String query = "select * from Training";
                PreparedStatement ps = con.prepareStatement(query);
                ResultSet rs = ps.executeQuery();

                ArrayList<String> data1 = new ArrayList<String>();
                while (rs.next()) {
                    Map<String, String> datanum = new HashMap<String, String>();
                    datanum.put("A", rs.getString(1));
                    data1.add(rs.getString(1));
                    datanum.put("B", rs.getString(2));
                    data1.add(rs.getString(2));
                    datanum.put("C", rs.getString(3));
                    prolist.add(datanum);
                }


                z = "Success";
            }
        } catch (Exception ex) {
            z = "Error retrieving data from table";

        }
        return z;
    }
}

然而它只是运行到例外z:

  

从表

中检索数据时出错

有人可以指引我到我错的地方吗?是否有另一种检索此表数据的方法?在Visual Studio中构建并存储在Azure上的表。

0 个答案:

没有答案