3.12

所花时间:4小时

代码量:136

博客篇:1

了解到的知识点

实现数据库的连接,完成数据的插入

其中DBHelpOpen是连接数据库的类

package com.example.myapplication;

import static com.example.myapplication.R.*;
import static com.example.myapplication.R.id.button3;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.security.AccessController;
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 TextView result;
    private Button button3;
    private Button bt_insert;
    private EditText editText1;
    private EditText editText2;
    private EditText editText3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_main);
        result = findViewById(id.result);
        button3 = findViewById(id.button3);
        bt_insert = findViewById(id.bt_insert);
        editText1 = findViewById(id.ed1_insert);
        editText2 = findViewById(id.ed2_insert);
        editText3 = findViewById(id.ed3_insert);
        button3.setOnClickListener(new MyOnClickListener());
    }

    class MyOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            result.setText("yes");
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        //button3.setOnClickListener(this);
        bt_insert.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
   //     switch (v.getId()){
 //           case R.id.bt_insert:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Connection conn = null;
                        conn = (Connection) DBHelpOpen.getConn();
                        Statement stmt;
                        String place;
                        String classname;
                        String name;
                         place = editText1.getText().toString();
                         classname = editText2.getText().toString();
                         name = editText3.getText().toString();
                         if(!name.equals("王建民")||!place.equals("基教")){
                             return;
                         }
                         String sql2 = "select * from teacher4";
                        String sql1 = "insert into teacher4(place,classname,name) values('" + place + "','"+ classname + "','" + name + "')";
                        try {
                            stmt= conn.createStatement();
                            ResultSet rs = stmt.executeQuery(sql2);
                            boolean y = false;
                            while (rs.next() && !y) {
                                if (rs.getString(2).equals(classname)) {
                                    y = true;
                                }
                            }
                            if(y==true){
                                return;
                            }
                            stmt.executeUpdate(sql1);
                            stmt.close();
                            conn.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
      //          break;
      //  }
    }
}

  

posted @ 2024-03-12 18:58  umiQa  阅读(3)  评论(0编辑  收藏  举报