尝试将android连接到sql server

时间:2016-07-04 07:13:55

标签: android sql-server jdbc

我在我的Android应用程序上进行登录活动。尝试连接数据库时,我不断收到IOException。我几乎尝试了一切: 1-我已将端口设置为1433 2 - 我已经启动了sql浏览器服务 3 - 我已经包含了jtds .jar文件 4-我已禁用防火墙 5-我在配置管理器中启用了TCP / IP服务

这是查询代码(点击按钮时调用的函数)

public void logIn(View view)
    {
        Toast.makeText(getApplicationContext(),ipValue,Toast.LENGTH_LONG).show();
        try {
            DBConnect dbConnect = new DBConnect(getApplicationContext());
            dbConnect.execute();
            LogInStatement="select count(*) from users where username=? AND password=?";
            preparedStatement=dbConnect.conn.prepareStatement(LogInStatement);
            preparedStatement.setString(1,userNameEditText.getText().toString());
            preparedStatement.setString(2,passwordEditText.getText().toString());
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                count = rs.getInt(1);
            }
            if (count != 0) {
                Intent userScreenIntent = new Intent(this, UserScreen.class);
                startActivity(userScreenIntent);
            } else {
                Toast.makeText(getApplicationContext(), "بيانات الدخول غير صحيحة", Toast.LENGTH_LONG).show();
            }
        }
        catch(Exception e)
        {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

这是连接代码:

public class DBConnect extends AsyncTask<String, Void, String>
    {
        private Context ctx;
        SharedPreferences prefs;
        Connection conn;
        String ipValue;
        public DBConnect(Context ctx)
        {
            this.ctx=ctx;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //get sharedPreferences here
            prefs = ctx.getSharedPreferences("DoorSystem_SHPREF",
                    Context.MODE_PRIVATE);
            ipValue=prefs.getString("IP_VALUE",null);
        }
        @Override
        protected String doInBackground(String... params) {


            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
            String ConnURL = null;
            try {

                Class.forName("net.sourceforge.jtds.jdbc.Driver");
                ConnURL = "jdbc:jtds:sqlserver://" + ipValue + ":1433;databaseName=doors;user=sam123;password=111;";
                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 null;
        }
    }

修改 堆栈跟踪现在显示NullPointerException:

07-04 10:17:32.233 20120-20120/port.doorsystem W/System.err: java.lang.NullPointerException
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at port.doorsystem.LogIn.logIn(LogIn.java:91)
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at android.view.View.performClick(View.java:4508)
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at android.view.View$PerformClick.run(View.java:18675)
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at android.os.Handler.handleCallback(Handler.java:733)
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at android.os.Looper.loop(Looper.java:136)
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5584)
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
07-04 10:17:32.243 20120-20120/port.doorsystem W/System.err:     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

我对此并不十分肯定,但我猜你在conn得到空指针。这可能是因为您在Async Task中初始化它并在其外部使用它。

在理想的方法中,您应该只使用Async Task返回的输出。您可以返回连接引用(conn),也可以将PreparedStatement代码放入Async Task并返回ResultSet,然后在后台准备语句。

如果你可以指出你获得例外的确切行,我可以帮助你。

更新:这应该有效:

后台主题 -

  protected Connection doInBackground(String... params) {

        try {

            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            ConnURL = "jdbc:jtds:sqlserver://" + ipValue + ":1433;databaseName=doors;user=sam123;password=111;";
            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;
    }

登录 -

DBConnect dbConnect = new DBConnect(getApplicationContext());
Connection conn = dbConnect.execute().get();
LogInStatement="select count(*) from users where username=? AND password=?";
preparedStatement=conn.prepareStatement(LogInStatement);