3.19

所花时间:4小时

代码量:177

博客篇:1

 

大作业主界面程序

package com.example.studyapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText ed_id;
    private Button bt_login;
    private Button bt_register;
    private Button bt_teacher;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed_id = findViewById(R.id.ed_id);
        bt_login = findViewById(R.id.bt_login);
        bt_register = findViewById(R.id.bt_register);
        bt_teacher = findViewById(R.id.bt_teacher);
        bt_login.setOnClickListener(this);
        bt_register.setOnClickListener(this);
        bt_teacher.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.bt_login) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String id = ed_id.getText().toString();
                    Dao d= new Dao();
                    if(d.haveStudent(id)){
                        Students s =new Students();
                        try {
                            s=d.gets(id);
                        } catch (SQLException e) {
                            throw new RuntimeException(e);
                        }
                        SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("id",s.getId());
                        editor.putString("name",s.getName());
                        editor.putString("phnumber",s.getPhnumber());
                        editor.putString("unit",s.getUnit());
                        editor.commit();
                        startActivity(new Intent(MainActivity.this,student.class));
                    }
                    else {
                        Looper.prepare();
                        Toast.makeText(MainActivity.this,"不存在",Toast.LENGTH_SHORT).show();
                        Looper.loop();
                    }
                }
            }).start();
        }
        else if(v.getId() == R.id.bt_register){
            Log.d("aaa", "onClick: ");
            startActivity(new Intent(this, regist.class));
        }
        else if(v.getId() == R.id.bt_teacher){
            startActivity(new Intent(this,teacher.class));
        }
    }
}

 

大作业注册界面程序:

package com.example.studyapplication;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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.sql.SQLException;

public class regist extends AppCompatActivity implements View.OnClickListener {

    private EditText ed_id;
    private EditText ed_name;
    private EditText ed_phnumber;
    private EditText ed_unit;
    private Button bt_register;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        ed_id = findViewById(R.id.ed_id);
        ed_name = findViewById(R.id.ed_name);
        ed_phnumber = findViewById(R.id.ed_phnumber);
        ed_unit = findViewById(R.id.ed_unit);
        bt_register = findViewById(R.id.bt_register);
        bt_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Dao d = new Dao();
                Students s = new Students();
                s.setId(ed_id.getText().toString());
                s.setName(ed_name.getText().toString());
                s.setPhnumber(ed_phnumber.getText().toString());
                s.setUnit(ed_unit.getText().toString());
                if(s.getId()==null||s.getName()==null||s.getUnit()==null||s.getPhnumber()==null||s.getId().length()<8||s.getPhnumber().length()<11){
                    Looper.prepare();
                    Toast.makeText(regist.this,"no",Toast.LENGTH_SHORT).show();
                    Looper.loop();
                    return;
                }
                d.addStudent(s);
                finish();
            }
        }).start();
    }
}

  

数据库添加的方法

public void addStudent(Students s)  {
        conn = DBHelpOpen.getConn();
        String sql = "insert into student(id, name,phnumber,unit) values('" +s.getId()+ "','" +s.getName()+"','" +s.getPhnumber()+ "','" +s.getUnit()+"')";
        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(sql);
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

  

判断登录方法

public boolean haveStudent(String id){

        conn = DBHelpOpen.getConn();
        String sql = "select * from student";
        boolean y = false;
        try {
            stmt= conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next() && !y) {
                if (rs.getString(1).equals(id)) {
                    y = true;
                }
            }
            Log.d("aaa", String.valueOf(y));
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return y;
    }

  

 

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