学习时间:下午四节课
代码量:200
package com.example.myapplication;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.129.97/classwork?useSSL=false&serverTimezone=UTC";
String USER = "root";
String PWD = "822611";
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("数据插入失败。");
}
}
}
} <?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> 标签:12,layout,String,软工,height,2024.3,android,id,match From: https://www.cnblogs.com/luoqingci/p/18073789