2024.3.12 軟工日報

liuxuechao發表於2024-03-14

學習時間:下午四節課

程式碼量: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>