connect android to mysql using jsp

2019-05-21 19:06发布

问题:

i am trying to connect with mysql using jsp below is the code of both the files android and jsp please have look on it its neither working nor giving me any error what would be the problem?

android code:

 package com.example.prj;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
Button submit;
TextView tv;
EditText et1,et2;
String user,pass;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    submit=(Button)findViewById(R.id.button1);
    tv=(TextView)findViewById(R.id.textView1);
    et1=(EditText)findViewById(R.id.editText1);
    et2=(EditText)findViewById(R.id.editText2);
    //user=et1.getText().toString();
    //pass=et2.getText().toString();

    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try{
                ArrayList<BasicNameValuePair> namevaluepair=new ArrayList<BasicNameValuePair>();
                namevaluepair.add(new BasicNameValuePair("user", user=et1.getText().toString()));
                namevaluepair.add(new BasicNameValuePair("pass", pass=et2.getText().toString()));
                HttpClient client=new DefaultHttpClient();
                HttpPost post=new HttpPost("http://10.0.2.2:8080/login.jsp");
                post.setEntity(new UrlEncodedFormEntity(namevaluepair));
                HttpResponse response=client.execute(post);
                HttpEntity entity=response.getEntity();
                InputStream in=entity.getContent();
                try{
                    BufferedReader bf=new BufferedReader(new InputStreamReader(in));
                    StringBuilder sb=new StringBuilder();
                    String line=null;
                    while((line=bf.readLine())!=null){

                        sb.append(line);

                    }
                    String result=sb.toString();
                    if(result.equalsIgnoreCase("vinod")){
                        Intent i=new Intent(MainActivity.this,loggedin.class);

                        i.putExtra("key",user);
                        startActivity(i);
                    }else{
                        tv.setText("invalid");
                    }


                }catch(Exception e){
                    e.printStackTrace();
                }
            }catch(Exception e){
                Log.e("log_tag", "Error in http"+e.toString());
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

JSP code:

<%@page import="java.io.*"%>
<%@page import="java.sql.*"%>
<%!
String username;
String password; 
Statement stmt=null;
ResultSet rs=null;
Connection con=null;
%>
<%
String driver="com.example.mysql.Driver";
Class.forName(driver).newInstance();

try{
 String url="jdbc:mysql://localhost/log";
  con = DriverManager.getConnection(url,"root","");
 stmt=con.createStatement();

}catch(Exception e){
e.printStackTrace(); 

}
if(!con.isClosed()){
username=request.getParameter("user");
password=request.getParameter("pass");
rs=stmt.executeQuery("select * from login");
while(rs.next()){
if(rs.getString("user")==username && rs.getString("pass")==password){
 out.println("vinod");
 System.out.println("vinod kakad");
}else{

 out.println("vinod");
 System.out.println("vinod kakad else part");
}
}
  else{
  out.println("lost");
  System.out.println("vinod kakad else lost part");
  } 


  }

 %>

回答1:

I am using the following code which connects to my MySQL database from JSP page on a Tomcat server

<%! String db;
    String user;
    String pw;
%>
<%
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql:///"+db, user, pw);
        Statement stmt = conn.createStatement();
        String sql = ""; //Your SQL here
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
    %><hr>
    <%= rs.getString("<columnName")%>
    <p>
        <%
            }
        //Cleanup, just to be nice :)
        rs.close();
        stmt.close();
        conn.close();
        %>

and this jsp code connects to my database, but I don't know yet how to pass it on to Android