使用android app在sql server中不执行查询

时间:2016-05-25 08:01:44

标签: android sql-server

我不知道为什么会发生这种情况,我的意思是建立了与数据库的连接,但查询本身并没有提供任何输出。没有例外或任何事情,我按下按钮,没有任何事情发生

这是Java代码:

public class LogIn extends AppCompatActivity {
    EditText pass;
    EditText usname;

    Connection con = null;
    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);
        connectTodatabase();
        Button btn=(Button) findViewById(R.id.button);
        TextView txtView1=(TextView)findViewById(R.id.textView1);
        txtView1.setText("ffgf");

    }
public void logIn(View v)
{
    EditText usname=(EditText) findViewById(R.id.editText);
    EditText pass=(EditText) findViewById(R.id.editText2);
    TextView txtView1=(TextView)findViewById(R.id.textView1);
    String username=usname.getText().toString();
    String password=pass.getText().toString();
    txtView1.setText("ffgf");
    String SQLStatment = "select * from users where usname='"+username+"' AND uspass='"+password+"'";
    try {

        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
        ResultSet rs = stmt.executeQuery(SQLStatment);
        int count=rs.getRow();
        txtView1.setText("ffgf");
        if(count>0)
       {
            Toast.makeText(this,"Query success",Toast.LENGTH_LONG).show();
            Intent intent=new Intent(getBaseContext(),appPage.class);
           startActivity(intent);
       }
        else
        {
            txtView1.setText("incorrect");
        }
    }
    catch(Exception e)
    {
       txtView1.setText(e.getMessage());
    }
}

    public void connectTodatabase()
    {
        Connection con;
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        String url = "jdbc:jtds:sqlserver://192.168.111.14:1433;instanceName=SQLEXPRESS;DatabaseName=test";
        String driver = "net.sourceforge.jtds.jdbc.Driver";
        String userName = "sam";
        String password = "111";
        // Declare the JDBC objects.

        try
        {
            // Establish the connection.
            Class.forName(driver);
            con = DriverManager.getConnection(url, userName, password);
            // Create and execute an SQL statement that returns some data.
            Toast.makeText(getApplicationContext(),"Success", Toast.LENGTH_LONG).show();
        }
        catch(Exception ex)
        {
           Toast.makeText(getApplicationContext(), "aa" + ex.getMessage().toString() + "ss", Toast.LENGTH_LONG).show();
        }
    }
}

0 个答案:

没有答案