Android Studio 实现实现学生信息的增删改查 -源代码 四(Servlet + 连接MySql数据库)(2)

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注Android)
img

正文

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

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

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

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

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”>

<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=“重置” />

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

}

最后

都说三年是程序员的一个坎,能否晋升或者提高自己的核心竞争力,这几年就十分关键。

技术发展的这么快,从哪些方面开始学习,才能达到高级工程师水平,最后进阶到Android架构师/技术专家?我总结了这 5大块;

我搜集整理过这几年阿里,以及腾讯,字节跳动,华为,小米等公司的面试题,把面试的要求和技术点梳理成一份大而全的“ Android架构师”面试 Xmind(实际上比预期多花了不少精力),包含知识脉络 + 分支细节。

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

2021年虽然路途坎坷,都在说Android要没落,但是,不要慌,做自己的计划,学自己的习,竞争无处不在,每个行业都是如此。相信自己,没有做不到的,只有想不到的。祝大家2021年万事大吉。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注Android)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

序员的一个坎,能否晋升或者提高自己的核心竞争力,这几年就十分关键。

技术发展的这么快,从哪些方面开始学习,才能达到高级工程师水平,最后进阶到Android架构师/技术专家?我总结了这 5大块;

我搜集整理过这几年阿里,以及腾讯,字节跳动,华为,小米等公司的面试题,把面试的要求和技术点梳理成一份大而全的“ Android架构师”面试 Xmind(实际上比预期多花了不少精力),包含知识脉络 + 分支细节。

[外链图片转存中…(img-Vule3nWS-1713437957435)]

[外链图片转存中…(img-2uBqHzdJ-1713437957435)]

[外链图片转存中…(img-FXXFXHgY-1713437957435)]

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

2021年虽然路途坎坷,都在说Android要没落,但是,不要慌,做自己的计划,学自己的习,竞争无处不在,每个行业都是如此。相信自己,没有做不到的,只有想不到的。祝大家2021年万事大吉。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-EHp44YRn-1713437957436)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 12
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我了解了。针对你的问题,我可以给你一些基本的步骤和思路,帮助你实现 Android Studio 连接 MySQL 数据库实现增删改查功能。 1. 配置 MySQL 数据库 首先,你需要在本地或者远程服务器上搭建一个 MySQL 数据库,并创建一张表用于存储数据。这一步需要你具备一定的数据库管理知识,如果你不熟悉,请参考相关的教程或者请专业人士帮忙。 2. 导入 MySQL 驱动 将 MySQL 驱动包(JDBC)导入到 Android Studio 项目中。你可以在 Maven 中心库中找到对应版本的 MySQL 驱动,或者手动下载 jar 包导入到项目中。在项目的 build.gradle 文件中添加以下代码: ``` dependencies { // ... implementation 'mysql:mysql-connector-java:8.0.23' } ``` 3. 编写数据库连接代码 在你的 Android Studio 项目中创建一个数据库连接类,用于连接 MySQL 数据库并执行 SQL 语句。代码示例: ```java import java.sql.*; public class MySQLConnection { // 数据库连接信息 private static final String DRIVER = "com.mysql.cj.jdbc.Driver"; private static final String URL = "jdbc:mysql://localhost:3306/mydatabase"; private static final String USERNAME = "root"; private static final String PASSWORD = "password"; // 获取数据库连接 public static Connection getConnection() { Connection connection = null; try { Class.forName(DRIVER); connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } return connection; } // 关闭数据库连接 public static void closeConnection(Connection connection, Statement statement, ResultSet resultSet) { try { if (resultSet != null) { resultSet.close(); } if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } ``` 4. 实现增删改查功能 在你的 Android Studio 项目中编写增删改查的代码,使用上面创建的数据库连接类来执行 SQL 语句。代码示例: ```java import java.sql.*; public class MySQLUtils { // 插入数据 public static void insert(String name, String email) { Connection connection = null; PreparedStatement statement = null; try { connection = MySQLConnection.getConnection(); String sql = "INSERT INTO user(name, email) VALUES (?, ?)"; statement = connection.prepareStatement(sql); statement.setString(1, name); statement.setString(2, email); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { MySQLConnection.closeConnection(connection, statement, null); } } // 删除数据 public static void delete(int id) { Connection connection = null; PreparedStatement statement = null; try { connection = MySQLConnection.getConnection(); String sql = "DELETE FROM user WHERE id=?"; statement = connection.prepareStatement(sql); statement.setInt(1, id); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { MySQLConnection.closeConnection(connection, statement, null); } } // 更新数据 public static void update(int id, String name, String email) { Connection connection = null; PreparedStatement statement = null; try { connection = MySQLConnection.getConnection(); String sql = "UPDATE user SET name=?, email=? WHERE id=?"; statement = connection.prepareStatement(sql); statement.setString(1, name); statement.setString(2, email); statement.setInt(3, id); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { MySQLConnection.closeConnection(connection, statement, null); } } // 查询数据 public static void query() { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { connection = MySQLConnection.getConnection(); String sql = "SELECT * FROM user"; statement = connection.createStatement(); resultSet = statement.executeQuery(sql); while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); String email = resultSet.getString("email"); System.out.println("id: " + id + ", name: " + name + ", email: " + email); } } catch (SQLException e) { e.printStackTrace(); } finally { MySQLConnection.closeConnection(connection, statement, resultSet); } } } ``` 这样,你就可以在 Android Studio连接 MySQL 数据库实现增删改查功能了。当然,这只是一个简单的示例,实际情况可能会更加复杂,需要你根据具体的需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值