首页 > 其他分享 >3.15

3.15

时间:2024-06-16 19:55:03浏览次数:27  
标签:layout 3.15 pst import android id conn

所花时间:4h

代码量:430

博客量:1

了解的知识点:1.Android连接Mysql数据库教程以及增删改查_android 访问 mysql 增删查改 源码-CSDN博客

更新数据

第一步,修改activity_main.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:orientation="vertical"

    tools:context=".MainActivity">

<Button

    android:id="@+id/bt_send"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:text="查询数据表"/>

    <TextView

        android:id="@+id/tv_response"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textStyle="bold" />

    <Button

        android:id="@+id/bt_delete"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="删除数据"/>

    <EditText

        android:id="@+id/ed_insert"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:hint="请输入要插入的数据"

        android:maxLines="2"/>

    <Button

        android:id="@+id/bt_insert"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="插入数据"/>

    <EditText

        android:id="@+id/ed_update"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:hint="请输入更新的内容"

        android:maxLines="2"/>

<Button

    android:id="@+id/bt_update"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:text="更新数据"/>

</LinearLayout>

 

第二步,修改MainActivity.java

package com.example.mysqlconnectiontest;

 

import android.annotation.SuppressLint;

import android.os.Handler;

import android.os.Message;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button button,button_delete,button_insert,button_update;

    private TextView textView;

    private static final int TEST_USER_SELECT = 1;

    int i =0,d=0,z=0;

    private EditText editText,editText_update;

    @SuppressLint("HandlerLeak")

    private Handler handler = new Handler(){

        @Override

        public void handleMessage(Message msg) {

            String user;

            switch (msg.what){

                case TEST_USER_SELECT:

                    Test test = (Test) msg.obj;

                    user = test.getUser();

                    System.out.println("***********");

                    System.out.println("***********");

                    System.out.println("user:"+user);

                    textView.setText(user);

                    break;

            }

        }

    };

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.bt_send);

        textView = (TextView) findViewById(R.id.tv_response);

        button_delete = (Button) findViewById(R.id.bt_delete);

        button_insert = (Button) findViewById(R.id.bt_insert);

        button_update = (Button) findViewById(R.id.bt_update);

        editText_update = (EditText) findViewById(R.id.ed_update);

    }

 

    @Override

    protected void onStart() {

        super.onStart();

        button.setOnClickListener(this);

        button_delete.setOnClickListener(this);

        button_insert.setOnClickListener(this);

        button_update.setOnClickListener(this);

    }

 

 

    @Override

    public void onClick(View view) {

        switch (view.getId()){

            case R.id.bt_send:

                //执行查询操作

                //通多点击buttoni自增长查询对应id的name

                if (i<=3){

                    i++;

                }else {

                    i=1;

                }

                //连接数据库进行操作需要在主线程操作

                new Thread(new Runnable() {

                    @Override

                    public void run() {

                        Connection conn = null;

                        conn =(Connection) DBOpenHelper.getConn();

                        String sql = "select name from test_one where id='"+i+"'";

                        Statement st;

                        try {

                            st = (Statement) conn.createStatement();

                            ResultSet rs = st.executeQuery(sql);

                            while (rs.next()){

                                //因为查出来的数据试剂盒的形式,所以我们新建一个javabean存储

                                Test test = new Test();

                                test.setUser(rs.getString(1));

                                Message msg = new Message();

                                msg.what =TEST_USER_SELECT;

                                msg.obj = test;

                                handler.sendMessage(msg);

                            }

                            st.close();

                            conn.close();

                        } catch (SQLException e) {

                            e.printStackTrace();

                        }

                    }

                }).start();

                break;

            case R.id.bt_delete:

                //new一个线程执行删除数据库数据

                d++;

                new Thread(new Runnable() {

                    @Override

                    public void run() {

                        Connection conn = null;

                        int u = 0;

                        conn =(Connection) DBOpenHelper.getConn();

                        String sql = "delete from test_one where id='"+d+"'";

                        PreparedStatement pst;

                        try {

                                pst = (PreparedStatement) conn.prepareStatement(sql);

                                u = pst.executeUpdate();

                            pst.close();

                            conn.close();

                        } catch (SQLException e) {

                            e.printStackTrace();

                        }

                    }

                }).start();

                break;

            case R.id.bt_insert:

                //执行插入操作

                new Thread(new Runnable() {

                    @Override

                    public void run() {

                        Connection conn = null;

                        int u = 0;

                        conn =(Connection) DBOpenHelper.getConn();

                        String sql = "insert into test_one (name) values(?)";

                        PreparedStatement pst;

                        try {

                            pst = (PreparedStatement) conn.prepareStatement(sql);

                            //将输入的edit框的值获取并插入到数据库中

                            pst.setString(1,editText.getText().toString());

                            u = pst.executeUpdate();

                            pst.close();

                            conn.close();

                        } catch (SQLException e) {

                            e.printStackTrace();

                        }

                    }

                }).start();

                break;

            case R.id.bt_update:

                //new一个线程执行删除数据库数据

                z++;

                new Thread(new Runnable() {

                    @Override

                    public void run() {

                        Connection conn = null;

                        int u = 0;

                        conn =(Connection) DBOpenHelper.getConn();

                        String sql = "update test_one set name='"+editText_update.getText().toString()+"' where id='"+z+"'";

                        PreparedStatement pst;

                        try {

                            pst = (PreparedStatement) conn.prepareStatement(sql);

                            u = pst.executeUpdate();

                            pst.close();

                            conn.close();

                        } catch (SQLException e) {

                            e.printStackTrace();

                        }

                    }

                }).start();

                break;

                default:

        }

    }

标签:layout,3.15,pst,import,android,id,conn
From: https://www.cnblogs.com/po3a/p/18251151

相关文章

  • TinyVue 3.15.0 正式发布,推出全新的 Charts 图表组件底座,功能更强、图表更丰富!
    你好,我是Kagol。我们非常高兴地宣布,2024年4月8日,TinyVue发布了v3.15.0......
  • KubeSphere 社区双周报|2024.03.15-03.29
    KubeSphere社区双周报主要整理展示新增的贡献者名单和证书、新增的讲师证书以及两周内提交过commit的贡献者,并对近期重要的PR进行解析,同时还包含了线上/线下活动和布道推广等一系列社区动态。本次双周报涵盖时间为:2024.03.15-03.29。贡献者名单新晋KubeSpherecontribut......
  • 2024.3.15软件工程日报
    学习时间:2h代码量100packagecom.example.text_five;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.EditText;importandroid.widget.Toast;importandroidx.appcompat.app.AppCompatActivity;importcom.example.t......
  • 3.15
     第九天所花时间1h代码量50行博客量1篇学到的知识按钮图片的插入     <?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"......
  • 3.15毕设
    前面的HelloWorld,只做了一个查询的Demo,这里尝试另外四种常见的操作。  前面所写的增删改查是存在问题的。主要问题就是冗余代码过多,模板化代码过多。例如,开发一个serDao,可能是下面这样: 引入Mapper,UserDao就基本上用不到了,因为框架已经帮我们做好了 ......
  • 3.15pht做题笔记
    3.15pht做题笔记C考虑先枚举学生\(j\),再枚举问题\(x\),接着枚举该问题回答相同的同学\(i\)根据鸽巢原理,每个同学有效枚举的次数肯定不会超过\(O(nk)\),所以总复杂度是\(O(n^2k)\)D先想确定\(k\)之后怎么做,从\(1\)到\(n\)枚举\(a_1\)的位置,每次只会交换两组......
  • q1-投资理财-2024.3.15
    q1-投资理财-2024.3.15​ 兴趣使然,在20岁接触到了股票,虽然没怎么赚钱并且一直都在赔钱,不过在家没有别的盈利能力,股票和期货成为搞钱的内容,期货我想碰的是鸡蛋期货,一般都是12月可能有小幅度上涨,整体一直下跌到2月份,有时候234月都是下跌的,一直到5月份会到底然后上涨到7月8月份,有的......
  • 日记 2024.3.15:2024 年 syzx 春季训练 1
    日记2024.3.15:2024年syzx春季训练1A找出在\(1,2\)周围一圈的点,挑出最远点\(u,v\)(找不到说明\(d_{1,2}=1\)),判一下\(d_{u,v}\)与\(d_{u,2}\)的关系以区分\(\pm1\)。这样比较好看。B普通冒泡\(n(n-1)/2\)次,这题\(n^2\),说明每做一次操作可以浪费一次操作。......
  • 2024.3.9 - 3.15
    SatLGR-176(Div.2)A.区间和问题,一眼盯真:前缀和。B.bfs,顺便记一下转移方向。C.最小化最大值,二分答案,用点DS实时维护逆序对即可,笔者用了线段树。D.区间DP,预处理一下\(a_i^{a_j}\)的值,然后记\(f_{l,r,0/1}\)表示到达了\([l,r]\)区间,并且最后一步是取了头部/尾部到达该......
  • centos7 k8s 三节点 全二进制部署 1.23.15
    主机名IP地址Pod网段Service网段master192.168.1.60172.16.0.0/1210.96.0.0/16node01192.168.1.70172.16.0.0/1210.96.0.0/16node02192.168.1.80172.16.0.0/1210.96.0.0/16[root@master~]#cat/etc/redhat-releaseCentOSLinuxrelease7.9.2......