赞
踩
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);
切换工程目录
添加jar
切换回Android工程
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; } }
点击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>
全部代码
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(); } } }; }
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="地址" /> </LinearLayout> </LinearLayout>
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<Student> studatalist = new ArrayList<>(); private Context mContext; public StudentAdapter(ArrayList<Student> 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; } }
添加onClick
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<Student> 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(); } } }; }
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; } }
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<Student> getStuByName(String name) { ArrayList<Student> stuList=new ArrayList<Student>(); 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; } }
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) throws ServletException, IOException { //获取请求名字 String name=request.getParameter("name"); //调用dao层 StudentDao studentDAO=new StudentDao(); ArrayList<Student> stuList=studentDAO.getStuByName(name); response.setContentType("application/x-json"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); JSONArray jsonArray = JSONArray.fromObject(stuList); out.print(jsonArray); out.flush(); out.close(); } }
先启动web端代码
运行Android端代码
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。