vm继续关闭连接android与ms SQL服务器

时间:2015-06-24 05:05:46

标签: java android sql-server

我遇到了一个将android与sql联系起来的问题。即使代码没有显示任何错误,android应用程序也会继续关闭。这是logcat

Shutting down VM
FATAL EXCEPTION: main
Process: com.example.temp, PID: 4882
java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference
at com.example.temp.CountryList$1.onClick(CountryList.java:96)
at android.view.View.performClick(View.java:4780)
...

所以我查看CountryList java类的第96行。就像这样

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String querycmd = "select * from countries";
            try {

                Statement statement = connect.createStatement(); <--this is the line 96
                rs = statement.executeQuery(querycmd);
                List<Map<String, String>> data = null;
                data = new ArrayList<Map<String, String>>();

                while (rs.next()) {
                    Map<String, String> datanum = new HashMap<String, String>();
                    datanum.put("A", rs.getString("CountryName"));
                    data.add(datanum);
                }
                String[] fromwhere = { "A" };
                int[] viewswhere = { R.id.lblcountryname };
                ADAhere = new SimpleAdapter(CountryList.this, data,
                        R.layout.listtemplate, fromwhere, viewswhere);
                lstcountry.setAdapter(ADAhere);

            } catch (SQLException e) {
                Toast.makeText(CountryList.this, e.getMessage().toString(),
                        Toast.LENGTH_LONG).show();
            }

        }
    });

但我不知道如何处理这条线。

CONN方法

ConnectionClass connectionclass;
String usernameS;
String datets;
String call, db, un, passwords;
Connection connect;
ResultSet rs;

@SuppressLint("NewApi")
private Connection CONN(String _user, String _pass, String _DB,
        String _server) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection conn = null;
    String ConnURL = null;
    try {

        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        ConnURL = "jdbc:jtds:sqlserver://" + _server + ";"
                + "databaseName=" + _DB + ";user=" + _user + ";password="
                + _pass + ";";
        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;
}

0 个答案:

没有答案