使用jsp将android连接到mysql [英] connect android to mysql using jsp

查看:69
本文介绍了使用jsp将android连接到mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jsp与mysql连接,下面是android和jsp文件的代码,请看看它既不工作也不给我任何错误,这是什么问题?

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代码:

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代码:

<%@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");
  } 


  }

 %>

推荐答案

我正在使用以下代码,该代码从Tomcat服务器上的JSP页面连接到我的MySQL数据库

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();
        %>

并且此jsp代码连接到我的数据库,但我尚不知道如何将其传递给Android

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

这篇关于使用jsp将android连接到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆