Android Studio 实现实现学生信息的查询 -源代码 三(Servlet + 连接MySql数据库) (JSON通信)

Posted 蓝盒子bluebox

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio 实现实现学生信息的查询 -源代码 三(Servlet + 连接MySql数据库) (JSON通信)相关的知识,希望对你有一定的参考价值。

Android Studio 实现登录注册-源代码 (连接MySql数据库)
Android Studio 实现登录注册-源代码 二(Servlet + 连接MySql数据库)
[Android Studio 实现登录注册-源代码 三(Servlet + 连接MySql数据库)实现学生信息的查询 (JSON通信)]
Android Studio 实现实现学生信息的增删改查 -源代码 四(Servlet + 连接MySql数据库)

一、在数据库当中创建学生信息表


SQL语句:

/*
Navicat mysql Data Transfer

Source Server         : localhost_3306
Source Server Version : 50562
Source Host           : localhost:3306
Source Database       : test

Target Server Type    : MYSQL
Target Server Version : 50562
File Encoding         : 65001

Date: 2021-05-17 16:14:29
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `age` int(11) NOT NULL,
  `address` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', 'andy', '21', '北京');
INSERT INTO `student` VALUES ('2', 'aaa', '22', '上海');
INSERT INTO `student` VALUES ('3', 'asdc', '34', '大连');
INSERT INTO `student` VALUES ('4', '张三', '20', '北京');

-- ----------------------------
-- Table structure for `users`
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `uid` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `age` int(255) NOT NULL,
  `phone` longblob NOT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES ('2', '123', 'HBV环保局', '123', '33', 0x3133333333333333333333);
INSERT INTO `users` VALUES ('3', '1233', '反复的', '1233', '12', 0x3132333333333333333333);
INSERT INTO `users` VALUES ('4', '1244', '第三代', '1244', '12', 0x3133333333333333333333);
INSERT INTO `users` VALUES ('5', '1255', 'SAS', '1255', '33', 0x3133333333333333333333);
INSERT INTO `users` VALUES ('6', '122', '但是ADS', '122', '12', 0x3132323232323232323232);
INSERT INTO `users` VALUES ('7', 'admin123', 'admin', '123', '45', 0x3132323232323232323232);
INSERT INTO `users` VALUES ('8', 'admin123', 'admin', '123', '45', 0x3132323232323232323232);
INSERT INTO `users` VALUES ('9', 'admin123', 'admin', '123', '45', 0x3132323232323232323232);
INSERT INTO `users` VALUES ('10', 'admin1232', 'admin', '123', '45', 0x3132323232323232323232);
INSERT INTO `users` VALUES ('11', 'admin222', '222', '2222', '222', 0x3232323232323232323232);
INSERT INTO `users` VALUES ('12', 'admin123', '123', '1212', '22', 0x32313231323232323232);
INSERT INTO `users` VALUES ('13', 'admin', 'admin', '2121', '33', 0x3333333333333333333333);
INSERT INTO `users` VALUES ('14', '123121212', '12321212', '123', '12', 0x3132333333333333333333);

二、android端代码

1、引入JSON相关的驱动包

切换工程目录

添加jar

切换回Android工程

2、创建Student的实体类


package com.example.application01.entity;

public class Student {

    private int id;
    private String name;
    private int age;
    private String address;

    public Student() {
    }

    public Student(int id, String name, int age, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }



}

3、创建跳转显示学生信息的页面



点击Finish

完善页面信息布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Student_List">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="76dp"
        tools:layout_editor_absoluteY="98dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20dp"

            android:orientation="horizontal">


            <EditText
                android:id="@+id/stuname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="textPersonName"
                />

            <Button
                android:id="@+id/buttonStu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="查询" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:orientation="horizontal">

            <ListView
                android:id="@+id/stulist"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">


            </ListView>

        </LinearLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

4、修改MainActivity当中的登录成功方法,设置其跳转到上述创建的页面当中


全部代码

package com.example.application01;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.application01.dao.UserDao;
import com.example.application01.utils.PostUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void reg(View view){

        startActivity(new Intent(getApplicationContext(),RegisterActivity.class));

    }


    public void login(View view){

        EditText EditTextname = (EditText)findViewById(R.id.name);
        EditText EditTextpassword = (EditText)findViewById(R.id.password);

        new Thread(){
            @Override
            public void run() {

                String data="";
                try {
                    data = "name="+ URLEncoder.encode(EditTextname.getText().toString(), "UTF-8")+
                            "&password="+ URLEncoder.encode(EditTextpassword.getText().toString(), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                String request = PostUtil.Post("LoginServlet",data);

                int msg = 0;
                if(request.equals("成功")){
                    msg = 1;
                }

                hand1.sendEmptyMessage(msg);


            }
        }.start();


    }
    final Handler hand1 = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {

            if(msg.what == 1)
            {
                //Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_LONG).show();
                startActivity(new Intent(getApplicationContext(),Student_List.class));
            }
            else
            {
                Toast.makeText(getApplicationContext(),"登录失败",Toast.LENGTH_LONG).show();
            }
        }
    };

}

5、创建一个XML页面用于显示ListView内部的东西

student_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"

    >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="id" />

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="姓名" />

        <TextView
            android:id="@+id/age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="年龄" />

        <TextView
            android:idAndroid Studio 实现实现学生信息的增删改查 -源代码 四(Servlet + 连接MySql数据库)

Android Studio 实现实现学生信息的查询 -源代码 三(Servlet + 连接MySql数据库) (JSON通信)

Android Studio 实现登录注册-源代码 (连接MySql数据库)

Android Studio 实现登录注册-源代码 二(Servlet + 连接MySql数据库)

Android Studio 实现登录注册-源代码 二(Servlet + 连接MySql数据库)

Android Studio实现简单的图书馆订座系统