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

一、完善学生信息显示

1、修改和删除的按钮在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="10dp"    >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:orientation="horizontal">        <TextView            android:id="@+id/id"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_weight="1"            android:text="id" />        <TextView            android:id="@+id/name"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_weight="1"            android:text="姓名" />        <TextView            android:id="@+id/age"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_weight="1"            android:text="年龄" />        <TextView            android:id="@+id/address"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_weight="1"            android:text="地址" />        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="10dp"            android:layout_weight="1"            android:layout_gravity="right"            android:orientation="horizontal">            <Button                android:id="@+id/button"                android:layout_width="70dp"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:layout_gravity="right"                android:layout_weight="1"                android:textSize="15sp"                android:text="修改" />            <Button                android:id="@+id/button4"                android:layout_width="70dp"                android:layout_height="wrap_content"                android:layout_gravity="right"                android:layout_weight="1"                android:textSize="15sp"                android:text="删除" />        LinearLayout>    LinearLayout>LinearLayout>

2、添加表头的xml,创建list_header.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="10dp"    >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:orientation="horizontal">        <TextView            android:id="@+id/id"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_weight="1"            android:text="id" />        <TextView            android:id="@+id/name"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_weight="1"            android:text="姓名" />        <TextView            android:id="@+id/age"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_weight="1"            android:text="年龄" />        <TextView            android:id="@+id/address"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_weight="1"            android:text="地址" />        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="10dp"            android:layout_weight="1"            android:layout_gravity="right"            android:orientation="horizontal">            <TextView                android:id="@+id/button"                android:layout_width="70dp"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:layout_gravity="right"                android:layout_weight="1"                android:textSize="15sp"                android:text="修改" />            <TextView                android:id="@+id/button4"                android:layout_width="70dp"                android:layout_height="wrap_content"                android:layout_gravity="right"                android:layout_weight="1"                android:textSize="15sp"                android:text="删除" />        LinearLayout>    LinearLayout>LinearLayout>

3、在adapter当中动态加载表头

  final LayoutInflater inflater = LayoutInflater.from(getApplicationContext());                View headView = inflater.inflate(R.layout.list_header, null);                if(stuList.getHeaderViewsCount()==0)                {                         stuList.addHeaderView(headView);                }

二、实现修改学生信息的操作

(一)安卓端

1、创建显示学生信息的页面



<?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=".StudentUpdate">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        tools:layout_editor_absoluteX="219dp"        tools:layout_editor_absoluteY="207dp"        android:padding="50dp"        >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:id="@+id/textView"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:textSize="15sp"                android:text="ID:" />            <TextView                android:id="@+id/id"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:ems="10"                />        LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:textSize="15sp"                android:text="昵称:" />            <EditText                android:id="@+id/name"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:ems="10"                android:inputType="textPersonName"                />        LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:id="@+id/textView2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:textSize="15sp"                android:text="年龄:"                />            <EditText                android:id="@+id/age"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:ems="10"                               />        LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:textSize="15sp"                android:text="地址:"                />            <EditText                android:id="@+id/address"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:ems="10"                android:inputType="phone"                />        LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">        LinearLayout>        <Button            android:layout_marginTop="50dp"            android:id="@+id/button2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="提交"            android:onClick="updatestudent"            />        <Button            android:id="@+id/button3"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="重置" />    LinearLayout>androidx.constraintlayout.widget.ConstraintLayout>

2、在student_list.xml页面上的按钮上添加onClick

3、完善Student_List当中的update方法

  public void update(View view){             LinearLayout linearLayout =  (LinearLayout)(view.getParent().getParent());        TextView idEd = linearLayout.findViewById(R.id.id);        TextView nameEd =  linearLayout.findViewById(R.id.name);        TextView ageEd = linearLayout.findViewById(R.id.age);        TextView addressEd = linearLayout.findViewById(R.id.address);        Intent intent=new Intent();        intent.putExtra("id", idEd.getText().toString());        intent.putExtra("name", nameEd.getText().toString());        intent.putExtra("age", ageEd.getText().toString());        intent.putExtra("address", addressEd.getText().toString());        intent.setClass(this, StudentUpdate.class);        startActivity(intent );        Toast.makeText(getApplicationContext(),"修改",Toast.LENGTH_LONG).show();    }

4、StudentUpdate当中获取对应Intent 对象当中的值并放入到页面当中的修改框当中

 @Override    protected void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);        setContentView(R.layout.activity_student_update);        Intent intent = getIntent();        String id =    intent.getStringExtra("id");        String name =  intent.getStringExtra("name");        String age = intent.getStringExtra("age");        String address = intent.getStringExtra("address");        TextView idtv = findViewById(R.id.id);        EditText nameet = findViewById(R.id.name);        EditText ageet = findViewById(R.id.age);        EditText addresset = findViewById(R.id.address);        idtv.setText(id);        nameet.setText(name);        ageet.setText(age);        addresset.setText(address);    }

运行测试


(二)web端

1、创建更新学生信息的dao

 public boolean update(Student stu){      String sql="update student set name=?,age=?,address=? where id=?";        Connection  con = JDBCUtils.getConn();        try {                 PreparedStatement pst=con.prepareStatement(sql);            pst.setString(1,stu.getName());            pst.setInt(2,stu.getAge());            pst.setString(3,stu.getAddress() );            pst.setInt(4,stu.getId()   );            int value = pst.executeUpdate();            if(value>0){                     return true;            }        } catch (SQLException throwables) {                 throwables.printStackTrace();        }finally {                 JDBCUtils.close(con);        }        return false;    }

2、创建对应更新学生信息的Servlet




完善StudentUpdateServlet

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {     System.out.println("-----------------");request.setCharacterEncoding("utf-8");response.setCharacterEncoding("UTF-8");int id = Integer.parseInt(request.getParameter("id"));String name = request.getParameter("name");int age =  Integer.parseInt(request.getParameter("age"));String address =  request.getParameter("address");Student student = new Student();student.setId(id);student.setName(name);student.setAge(age);student.setAddress(address);        String msg = "";        StudentDao studentDao = new StudentDao();                    boolean flag = studentDao.update(student);                if(flag){                 msg = "成功";        }else{             msg = "失败";        }           PrintWriter out = response.getWriter();out.println(msg);out.flush();out.close();}

(三)安卓端

1、在StudentUpdate当中创建updatestudent实现向studentUpdateServlet发送更新信息的请求

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.LayoutInflater;import android.view.View;import android.widget.EditText;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import com.example.application01.utils.PostUtil;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;public class StudentUpdate extends AppCompatActivity {         @Override    protected void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);        setContentView(R.layout.activity_student_update);        Intent intent = getIntent();        String id =    intent.getStringExtra("id");        String name =  intent.getStringExtra("name");        String age = intent.getStringExtra("age");        String address = intent.getStringExtra("address");        TextView idtv = findViewById(R.id.id);        EditText nameet = findViewById(R.id.name);        EditText ageet = findViewById(R.id.age);        EditText addresset = findViewById(R.id.address);        idtv.setText(id);        nameet.setText(name);        ageet.setText(age);        addresset.setText(address);    }    public void updatestudent(View view){             TextView idtv = findViewById(R.id.id);        EditText nameet = findViewById(R.id.name);        EditText ageet = findViewById(R.id.age);        EditText addresset = findViewById(R.id.address);        String id =  idtv.getText().toString();        String name =   nameet.getText().toString();        String age =  ageet.getText().toString();        String address = addresset.getText().toString();        new Thread(){                 @Override            public void run() {                     String data="";                try {                         data = "&id="+ URLEncoder.encode(id, "UTF-8")+                            "&name="+ URLEncoder.encode(name, "UTF-8")+                            "&age="+ URLEncoder.encode(age, "UTF-8")+                            "&address="+ URLEncoder.encode(address, "UTF-8");                } catch (UnsupportedEncodingException e) {                         e.printStackTrace();                }                String request = PostUtil.Post("studentUpdateServlet",data);                int msg = 0;                if(request.equals("成功")){                         msg = 1;                }                hand.sendEmptyMessage(msg);            }        }.start();    }    Handler hand=new Handler(){             @Override        public void handleMessage(Message msg) {                 if(msg.what == 1)            {                     startActivity(new Intent( getApplicationContext(),  Student_List.class ) );                //startActivity(new Intent( getApplicationContext(),  StuInfoActivity.class ) );            }            else            {                     Toast.makeText(getApplicationContext(),"更新失败",Toast.LENGTH_LONG).show();            }        }    };}

2、运行测试



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

更多相关文章

  1. Android(安卓)实现ListView 3D效果 - 1
  2. [android]利用动画实现view跑马灯效果
  3. android5.0以上实现录屏功能,并将录屏内容在相册中显示!(unity调用a
  4. 【Android(安卓)修炼手册】Gradle 篇 -- Android(安卓)Gradle Pl
  5. Android清除缓存功能实现
  6. Android的NDK开发(5)————Android(安卓)JNI层实现文件的read
  7. Android利用DownloadManager实现文件下载
  8. Android实现EditView获取焦点但不弹出软键盘
  9. 浅谈Java中Collections.sort对List排序的两种方法

随机推荐

  1. Android(安卓)源码下载
  2. Unity3D游戏引擎实现在Android中打开WebV
  3. ADK的安装
  4. Android(安卓)代码风格指南
  5. android屏幕适配详解
  6. Android(安卓)MediaPlayer
  7. MAC下Android的Eclipse开发环境的搭建
  8. Android(安卓)工具类 Toast Log.d()
  9. android中layout区别
  10. Android(安卓)EventBus 通信