今天 建民老师留的任务的 连接数据库 进行添加数据库操作
页面代码
<?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:background="@drawable/ic_launcher_background" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:id="@+id/et1" android:layout_marginTop="150dp" android:layout_width="match_parent" android:layout_height="75dp" android:gravity="center" android:text="添加" android:textColor="#50120D" android:textSize="25sp" /> <EditText android:id="@+id/et2" android:layout_marginTop="15dp" android:layout_width="match_parent" android:layout_height="50dp" android:hint="课程名称" android:inputType="textMultiLine" android:gravity="center_vertical" android:maxLines="1" /> <EditText android:id="@+id/et3" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="50dp" android:hint="任课教师" android:inputType="textMultiLine" android:gravity="center_vertical" android:maxLines="1" /> <EditText android:id="@+id/et4" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="50dp" android:hint="上课地点" android:inputType="textMultiLine" android:gravity="center_vertical" android:maxLines="1" /> <Button android:id="@+id/et5" android:layout_width="100dp" android:layout_height="40dp" android:layout_gravity="center" android:layout_marginTop="10dp" android:text="添加" android:textSize="15sp" /> </LinearLayout>
数据库连接代码 以及 添加数据操作 代码
package com.example.classwork; import android.os.AsyncTask; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class MysqlHelp { public static void insertData(String classq, String teacher, String didian) { new InsertDataTask().execute(classq, teacher, didian); } private static class InsertDataTask extends AsyncTask<String, Void, Boolean> { @Override protected Boolean doInBackground(String... params) { String classq = params[0]; String teacher = params[1]; String didian = params[2]; String CLS = "com.mysql.jdbc.Driver"; String URL = "jdbc:mysql://192.168.61.241/classwork?useSSL=false&serverTimezone=UTC"; String USER = "root"; String PWD = "123456"; try { Class.forName(CLS); Connection conn = DriverManager.getConnection(URL, USER, PWD); // 执行插入数据的逻辑 String query = "INSERT INTO s(classq, teacher, didian) VALUES (?, ?, ?)"; try (PreparedStatement pstmt = conn.prepareStatement(query)) { pstmt.setString(1, classq); pstmt.setString(2, teacher); pstmt.setString(3, didian); int affectedRows = pstmt.executeUpdate(); return affectedRows > 0; } } catch (Exception ex) { ex.printStackTrace(); return false; } } @Override protected void onPostExecute(Boolean success) { if (success) { System.out.println("数据插入成功!"); } else { System.out.println("数据插入失败。"); } } } }
进行点击操作 跳转页面 以及调用函数的 逻辑 代码
package com.example.classwork; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.example.classwork.MysqlHelp; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button myButtonlogin; private EditText classq,teacher,didian; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myButtonlogin = findViewById(R.id.et5); myButtonlogin.setOnClickListener(this); } public void onClick(View view) { classq = findViewById(R.id.et2); teacher = findViewById(R.id.et3); didian = findViewById(R.id.et4); String id = classq.getText().toString(); String name = teacher.getText().toString(); String phone = didian.getText().toString(); if(name.equals("王建民")||name.equals("刘立嘉")||name.equals("刘丹")||name.equals("杨子光")||name.equals("张云霞")||name.equals("武永亮")||name.equals("高飞")||name.equals("孙静")||name.equals("黄荣峰")) { if(phone.equals("一教")||phone.equals("二教")||phone.equals("三教")||phone.equals("基教")) { MysqlHelp.insertData(id, name, phone); } else { Toast.makeText(getApplicationContext(), "地点姓名不正确,请输入正确的上课地点", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getApplicationContext(), "老师姓名不正确,请输入正确的老师姓名", Toast.LENGTH_SHORT).show(); } } }
标签:didian,String,equals,三月,import,十二号,课堂,teacher,name From: https://www.cnblogs.com/youxiandechilun/p/18069612