0

I am doing SqlDatabase connectivity through android without using webservice.

I have following two functions in my connection clasxs:

    public class gaSQLConnect 
{
    String url ="";
    Connection conn=null;
    Statement statement=null;
    ResultSet resultSet=null;
        public void setConnection(String DBName,String UserName,String Password)
        {
            try {
                Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
                url ="jdbc:jtds:sqlserver://10.0.2.2:1433;instanceName=14GRAFICALI\\MSSQLSERVER2008;DatabaseName="+DBName+";integratedSecurity=true;user="+UserName+";password="+Password;
                conn =DriverManager.getConnection(url);
                } catch (Exception e) {
                e.printStackTrace();
                }
        }
        public Boolean CheckUser(String uName,String password)
        {
            int recFound=0;

            try
            {
                statement=conn.createStatement();


                resultSet=statement.executeQuery("select * from UserMaster where username="+uName+" and password="+password+"");
                if(resultSet.last())
                {
                    if(resultSet.getRow()>0)
                    {
                        recFound=1;
                    }
                    else
                    {
                        recFound=0;
                    }
                }


            }
            catch (Exception e) {
                e.printStackTrace();
                recFound=0;
            }
            if(recFound==0)
            return false;
            else
                return true;
}
}

I am calling these functions through following code:

final gaSQLConnect con=new gaSQLConnect();

        btnLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                con.setConnection("AndroidDB", "sa", "ok");
                boolean isValidUser=con.CheckUser(etLoginID.getText().toString(), etPassword.getText().toString());
                if(isValidUser)
                {
                    Intent i= new Intent(getApplicationContext(),Messages.class);
                    startActivity(i);
                }

            }
        });

I debugged app and came to know that exception is on following line:

resultSet=statement.executeQuery("select * from UserMaster where username="+uName+" and password="+password+"");

Logcat:

09-11 11:14:05.896: E/AndroidRuntime(756): FATAL EXCEPTION: main
09-11 11:14:05.896: E/AndroidRuntime(756): java.lang.NullPointerException
09-11 11:14:05.896: E/AndroidRuntime(756):  at com.example.messagesql.gaSQLConnect.CheckUser(gaSQLConnect.java:49)
09-11 11:14:05.896: E/AndroidRuntime(756):  at com.example.messagesql.Login$1.onClick(Login.java:37)
09-11 11:14:05.896: E/AndroidRuntime(756):  at android.view.View.performClick(View.java:2485)
09-11 11:14:05.896: E/AndroidRuntime(756):  at android.view.View$PerformClick.run(View.java:9080)
09-11 11:14:05.896: E/AndroidRuntime(756):  at android.os.Handler.handleCallback(Handler.java:587)
09-11 11:14:05.896: E/AndroidRuntime(756):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 11:14:05.896: E/AndroidRuntime(756):  at android.os.Looper.loop(Looper.java:123)
09-11 11:14:05.896: E/AndroidRuntime(756):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-11 11:14:05.896: E/AndroidRuntime(756):  at java.lang.reflect.Method.invokeNative(Native Method)
09-11 11:14:05.896: E/AndroidRuntime(756):  at java.lang.reflect.Method.invoke(Method.java:507)
09-11 11:14:05.896: E/AndroidRuntime(756):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-11 11:14:05.896: E/AndroidRuntime(756):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-11 11:14:05.896: E/AndroidRuntime(756):  at dalvik.system.NativeStart.main(Native Method)
4
  • @prijupaul its inbuilt method to create statement, as we do con.open() in .net Commented Sep 11, 2013 at 5:55
  • what is "statement" stands for??? Commented Sep 11, 2013 at 5:56
  • @Shrimant. Just realized that from the docs. So had to delete my comment. Commented Sep 11, 2013 at 5:56
  • @PiyushGupta i posted full code Commented Sep 11, 2013 at 5:58

1 Answer 1

2

I think you missed the single quote.

The query should be:

("select * from UserMaster where username='"+uName+"' and password='"+password+" ");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.