使用jsp从Android应用程序将数据插入MySQL数据库

时间:2015-04-04 11:50:50

标签: java android mysql jsp

我正在尝试将我的Android应用程序中的数据插入 MySQL 数据库。为此,我在Android文件中使用 AsyncTask类,并使用 jsp 文件与MySql通信。虽然应用程序运行成功,但没有数据插入数据库。

正如我搜索到的那样,有类似的问题,但是他们使用 php JSON 这样做我不擅长。

非常感谢任何帮助。

我的文件如下:

indoor_patient.java:

import java.util.Calendar;
import com.project.gdp.ehealth.R;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TimePicker;

public class indoor_patient extends Activity{

public EditText sr_no, patient_name, consultant_name, ref_dr, department,
        rel_name, rel_no, arr_date, arr_time;
Button submit;
ImageButton datepicker, timepicker;
Calendar cal;
int day, month, year;
int hour, min;

static final int TIME_DIALOG_ID = 1;

static final int DATE_DIALOG_ID = 0;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.indoor_patient_tbl);

    sr_no = (EditText) findViewById(R.id.etSrNo);
    patient_name = (EditText) findViewById(R.id.etPatientName);
    consultant_name = (EditText) findViewById(R.id.etConsultantDr);
    ref_dr = (EditText) findViewById(R.id.etRefDr);
    department = (EditText) findViewById(R.id.etDepartment);
    rel_name = (EditText) findViewById(R.id.etRelativeName);
    rel_no = (EditText) findViewById(R.id.etRelContact);
    arr_date = (EditText) findViewById(R.id.etArrDate);
    arr_time = (EditText) findViewById(R.id.etArrTime);

    submit = (Button) findViewById(R.id.btnIndoorSubmit);

    cal = Calendar.getInstance();

    day = cal.get(Calendar.DAY_OF_MONTH);
    month = cal.get(Calendar.MONTH);
    year = cal.get(Calendar.YEAR);
    datepicker = (ImageButton) findViewById(R.id.imgBtnDate);

    datepicker.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    hour = cal.get(Calendar.HOUR_OF_DAY);
    min = cal.get(Calendar.MINUTE);
    timepicker = (ImageButton) findViewById(R.id.imgBtnTime);

    timepicker.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            showDialog(TIME_DIALOG_ID);
        }
    });

    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String webUrl = "http://10.0.3.2/EHealth_web/indoorPatient.jsp?sr_no="
                    + sr_no.getText().toString()
                    + ""
                    + patient_name.getText().toString()
                    + ""
                    + consultant_name.getText().toString()
                    + ""
                    + ref_dr.getText().toString()
                    + ""
                    + department.getText().toString()
                    + ""
                    + rel_name.getText().toString()
                    + ""
                    + rel_no.getText().toString()
                    + ""
                    + arr_date.getText().toString()
                    + ""
                    + arr_time.getText().toString() + "&Submit=submit";
            new dbwork().execute(webUrl);
        }
    });
}

@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, datePickerListener, year, month,
                day);
    case TIME_DIALOG_ID:    
        return new TimePickerDialog(this, timePickerListener, hour, min,
                false);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        arr_date.setText(selectedDay + "/" + (selectedMonth + 1) + "/"
                + selectedYear);
    }
};

private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        int hour;
        String am_pm;
        if (hourOfDay > 12) {
            hour = hourOfDay - 12;
            am_pm = "PM";
        } else {
            hour = hourOfDay;
            am_pm = "AM";
        }
        arr_time.setText(hour + ":" + min + " " + am_pm);
    }
};
}

dbwork.java:

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;    
import android.os.AsyncTask;

public class dbwork extends AsyncTask<String, String, String> {
    indoor_patient ins=new indoor_patient();

    protected String doInBackground(String... params) {
        String webUrl = params[0];
        StringBuffer strbuffer=new StringBuffer("");
        String line="";

        try {

           URL url=new URL(webUrl);

            HttpURLConnection hurl;
            hurl=(HttpURLConnection)url.openConnection();

            BufferedInputStream bis;
            bis=new BufferedInputStream(hurl.getInputStream());

            InputStream is;
            is=bis;

            InputStreamReader isr;
            isr=new InputStreamReader(is);

            BufferedReader br;
            br=new BufferedReader(isr);

            while ((line = br.readLine()) != null) {
                strbuffer.append(line);
            }
            bis.close();
            is.close();
            isr.close();
            br.close();

            hurl.disconnect();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return strbuffer.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    }
    }

indoorPatient.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <%@page import="java.sql.*;"%>
    <%
            String s1 = request.getParameter("sr_no");
            String s2 = request.getParameter("patient_name");
            String s3 = request.getParameter("consultant_name");
            String s4 = request.getParameter("ref_dr");
            String s5 = request.getParameter("department");
            String s6 = request.getParameter("rel_name");
            String s7 = request.getParameter("rel_no");
            String s8 = request.getParameter("arr_date");
            String s9 = request.getParameter("arr_time");

            try {
        Class.forName("com.mysql.jdbc.Driver");
        out.println("Success"); 
            }
            catch(Exception ex)
            {
            out.println(ex);
            }
            try
            {
        Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/eHealth", "root", "root");

        PreparedStatement stmt = con.prepareStatement("INSERT INTO `indoor_patient`(`sr_no`, `Patient name`, `Consultant Dr`, `Ref. Dr`, `Department`, `Relative Name`, `Relative Contact`, `Arr. Date`, `Arr. Time`) VALUES(?,?,?,?,?,?,?,?,?);");
        stmt.setString(1,s1);
        stmt.setString(2,s2);
        stmt.setString(3,s3);
        stmt.setString(4,s4);
        stmt.setString(5,s5);
        stmt.setString(6,s6);
        stmt.setString(7,s7);
        stmt.setString(8,s8);
        stmt.setString(9,s9);   
        stmt.executeUpdate();
    %>
    <%
        } catch (Exception e) {
        out.println(e);
            }
    %>
</body>
</html>

0 个答案:

没有答案