I am new to Android i want to store login and signup details in data base i'm following javatpoints tutorial but on that I'm getting errors now.i followed many tutorials then also same problem only if any one is free help me please.i want to know how to connect database and how to store and how to retrive from data base please help me stack over flow.......
public class MainActivity extends AppCompatActivity {
EditText password,userName;
Button login,resister;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
password=(EditText) findViewById(R.id.editText2);
userName=(EditText) findViewById(R.id.editText1);
login=(Button) findViewById(R.id.button1);
resister=(Button) findViewById(R.id.button2);
//progess_msz.setVisibility(View.GONE);
progressBar=(ProgressBar) findViewById(R.id.progressBar1);
progressBar.setVisibility(View.GONE);
resister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,RegisterUserActivity.class);
startActivity(intent);
}
});
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
String s1=userName.getText().toString();
String s2=password.getText().toString();
new ExecuteTask().execute(s1,s2);
}
});
}
class ExecuteTask extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... params) {
String res=PostData(params);
return res;
}
@Override
protected void onPostExecute(String result) {
progressBar.setVisibility(View.GONE);
//progess_msz.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), result, 3000).show();
}
}
public String PostData(String[] valuse) {
String s="";
try
{
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost("http://192.168.1.11/javatpoints/Login.php");
List<NameValuePair> list=new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("name", valuse[0]));
list.add(new BasicNameValuePair("pass",valuse[1]));
httpPost.setEntity(new UrlEncodedFormEntity(list));
HttpResponse httpResponse= httpClient.execute(httpPost);
HttpEntity httpEntity=httpResponse.getEntity();
s= readResponse(httpResponse);
}
catch(Exception exception) {}
return s;
}
public String readResponse(HttpResponse res) {
InputStream is=null;
String return_text="";
try {
is=res.getEntity().getContent();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(is));
String line="";
StringBuffer sb=new StringBuffer();
while ((line=bufferedReader.readLine())!=null)
{
sb.append(line);
}
return_text=sb.toString();
} catch (Exception e)
{
}
return return_text;
}
}
RegistreuserActivity.java
public class RegisterUserActivity extends Activity {
EditText userName,passwprd;
Button resister,login;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_user);
userName=(EditText) findViewById(R.id.editText1);;
passwprd=(EditText) findViewById(R.id.editText2);
resister=(Button) findViewById(R.id.button1);
progressBar=(ProgressBar) findViewById(R.id.progressBar1);
progressBar.setVisibility(View.GONE);
resister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
String s1=userName.getText().toString();
String s2=passwprd.getText().toString();
new ExecuteTask().execute(s1,s2);
}
});
}
class ExecuteTask extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... params) {
PostData(params);
return null;
}
@Override
protected void onPostExecute(String result) {
progressBar.setVisibility(View.GONE);
}
}
public void PostData(String[] valuse) {
try
{
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(
"http://192.168.1.11/javatpoints/registre.php");
List<NameValuePair> list=new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("name", valuse[0]));
list.add(new BasicNameValuePair("pass",valuse[1]));
httpPost.setEntity(new UrlEncodedFormEntity(list));
httpClient.execute(httpPost);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Login.java
public class Login extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
ObjectOutputStream out=new ObjectOutputStream(response.getOutputStream());
String n=request.getParameter("name");
String p=request.getParameter("pass");
System.out.println(n);
System.out.println(p);
if(validate(n, p)){
out.writeObject("success");
}
else{
out.writeObject("Sorry username or password error");
}
out.close();
}
public static boolean validate(String name,String pass){
boolean status=false;
try{
Class.forName("oracle.jdbc.driver.OracleDriver") Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system", oracle");
PreparedStatement ps=con.prepareStatement( "select * from javatpoint_user where name=? and password=?");
ps.setString(1,name);
ps.setString(2,pass);
ResultSet rs=ps.executeQuery();
status=rs.next();
}catch(Exception e){System.out.println(e);}
return status;
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
httpPostServlet
public class httpPostServlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String recived_data="";
String s1=request.getParameter("name");
String s2=request.getParameter("pass");
System.out.println(s1);
System.out.println(s2);
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement ps=con.prepareStatement(
"insert into javatpoint_user(name,password) values(?,?)");
ps.setString(1, s1);
ps.setString(2,s2);
ps.executeUpdate();
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Any help should be appreciated