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

言尽于此,完结

无论是一个初级的 coder,高级的程序员,还是顶级的系统架构师,应该都有深刻的领会到设计模式的重要性。

  • 第一,设计模式能让专业人之间交流方便,如下:

程序员A:这里我用了XXX设计模式

程序员B:那我大致了解你程序的设计思路了

  • 第二,易维护

项目经理:今天客户有这样一个需求…

程序员:明白了,这里我使用了XXX设计模式,所以改起来很快

  • 第三,设计模式是编程经验的总结

程序员A:B,你怎么想到要这样去构建你的代码

程序员B:在我学习了XXX设计模式之后,好像自然而然就感觉这样写能避免一些问题

  • 第四,学习设计模式并不是必须的

程序员A:B,你这段代码使用的是XXX设计模式对吗?

程序员B:不好意思,我没有学习过设计模式,但是我的经验告诉我是这样写的

image

从设计思想解读开源框架,一步一步到Spring、Spring5、SpringMVC、MyBatis等源码解读,我都已收集整理全套,篇幅有限,这块只是详细的解说了23种设计模式,整理的文件如下图一览无余!

image

搜集费时费力,能看到此处的都是真爱!

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

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

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”>

</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:id=“@+id/address”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:text=“地址” />

5、创建StudentAdapter

在这里插入图片描述

package com.example.application01;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.TextView;

import com.example.application01.entity.Student;

import java.util.ArrayList;

public class StudentAdapter extends BaseAdapter {

ArrayList studatalist = new ArrayList<>();

private Context mContext;

public StudentAdapter(ArrayList sData, Context mContext) {

this.sData = sData;

this.mContext = mContext;

}

@Override

public int getCount() {

return sData.size();

}

@Override

public Object getItem(int position) {

return null;

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

convertView = LayoutInflater.from(mContext).inflate(R.layout.student_list,null);

TextView txt_id = (TextView) convertView.findViewById(R.id.id);

TextView txt_name = (TextView) convertView.findViewById(R.id.name);

TextView txt_age = (TextView) convertView.findViewById(R.id.age);

TextView txt_address = (TextView) convertView.findViewById(R.id.address);

txt_id.setText(sData.get(position).getId()+“”);

txt_name.setText(sData.get(position).getName());

txt_age.setText(sData.get(position).getAge()+“”);

txt_address.setText(sData.get(position).getAddress());

return convertView;

}

}

6、在Student_List当中实现点击搜索,并显示对应是学生列表

(1)修改activity_student_list.xnl当中的按钮

添加onClick

在这里插入图片描述

(2)在Student_List当中完善对应的方法

在这里插入图片描述

package com.example.application01;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.View;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.Toast;

import com.example.application01.entity.Student;

import com.example.application01.utils.PostUtil;

import org.json.JSONArray;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.util.ArrayList;

public class Student_List extends AppCompatActivity {

EditText editTextname = null;

ArrayList studatalist = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_student__list);

editTextname = findViewById(R.id.stuname);

}

public void findALLStudent(View view){

new Thread() {

public void run() {

if(studatalist != null){

studatalist.clear();

}

String data=“”;

try {

data = “name=”+ URLEncoder.encode(editTextname.getText().toString(), “UTF-8”);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

String stuJson= PostUtil.Post(“studentServlet”,data);

try {

JSONArray jsonArray = new JSONArray(stuJson);

for(int i = 0;i < jsonArray.length();i++){

JSONObject jsonObject = (JSONObject) jsonArray.get(i);

Student student = new Student();

student.setId(jsonObject.getInt(“id”));

student.setName(jsonObject.getString(“name”));

student.setAge(jsonObject.getInt(“age”));

student.setAddress(jsonObject.getString(“address”));

studatalist.add(student);

}

} catch (org.json.JSONException e) {

e.printStackTrace();

}

/*

*/

hand.sendEmptyMessage(1);

}

}.start();

}

Handler hand=new Handler(){

@Override

public void handleMessage(Message msg) {

if(msg.what == 1)

{

ListView stuList = (ListView) findViewById(R.id.stulist);

StudentAdapter adapter = new StudentAdapter( studatalist, Student_List.this);

stuList.setAdapter(adapter);

//startActivity(new Intent( getApplicationContext(), StuInfoActivity.class ) );

}

else

{

Toast.makeText(getApplicationContext(),“查询错误”,Toast.LENGTH_LONG).show();

}

}

};

}

三、Web端代码


1、创建Student实体类直接Android的复制即可

在这里插入图片描述

package 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;

}

}

2、创建对应的StudentDao查询

在这里插入图片描述

在这里插入图片描述

package dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import entity.Student;

public class StudentDao {

public ArrayList getStuByName(String name)

{

ArrayList stuList=new ArrayList();

String sql=“select * from student where name like '%”+name+“%'”;

Connection con=JDBCUtils.getConn();

try {

PreparedStatement pst=con.prepareStatement(sql);

ResultSet rs=pst.executeQuery();

while(rs.next())

{

Student stu=new Student();

stu.setId(rs.getInt(1));

stu.setName(rs.getString(2));

stu.setAge(rs.getInt(3));

stu.setAddress(rs.getString(4));

stuList.add(stu);

}

} catch (SQLException throwables) {

throwables.printStackTrace();

}finally {

JDBCUtils.close(con);

}

return stuList;

}

}

3、在web工程当中引入JSON相关的jar

在这里插入图片描述

4、创建StudentServlet

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

package servlet;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import dao.StudentDao;

import dao.UserDao;

import entity.Student;

public class StudentServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

面试准备+复习分享:

为了应付面试也刷了很多的面试题与资料,现在就分享给有需要的读者朋友,资料我只截取出来一部分哦

秋招|美团java一面二面HR面面经,分享攒攒人品

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

w_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NzU3MDM0,size_16,color_FFFFFF,t_70)

package servlet;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import dao.StudentDao;

import dao.UserDao;

import entity.Student;

public class StudentServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

面试准备+复习分享:

为了应付面试也刷了很多的面试题与资料,现在就分享给有需要的读者朋友,资料我只截取出来一部分哦

[外链图片转存中…(img-MGkoiVvq-1715807350450)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 26
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值