android成功增进数据库
bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package Bean;
public class causebean {
public String getCoursenumber() {
return coursenumber;
}
public void setCoursenumber(String coursenumber) {
this .coursenumber = coursenumber;
}
public String getCoursename() {
return coursename;
}
public void setCoursename(String coursename) {
this .coursename = coursename;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this .teacher = teacher;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this .place = place;
}
private String coursenumber;
private String coursename;
private String teacher;
private String place;
}
|
mainactivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
package com.hui.application3;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import Bean.causebean;
import JDBCUtils.utils;
import android.os.AsyncTask;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
private EditText etCourseNumber;
private EditText etCourseName;
private EditText etTeacher;
private EditText etPlace;
private Button btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etCourseNumber = findViewById(R.id.etCourseNumber);
etCourseName = findViewById(R.id.etCourseName);
etTeacher = findViewById(R.id.etTeacher);
etPlace = findViewById(R.id.etPlace);
btnAdd = findViewById(R.id.btnAdd);
btnAdd.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
addCourseToDatabase();
}
});
}
private void addCourseToDatabase() {
String courseNumber = etCourseNumber.getText().toString().trim();
String courseName = etCourseName.getText().toString().trim();
String teacher = etTeacher.getText().toString().trim();
String place = etPlace.getText().toString().trim();
if (courseNumber.isEmpty() || courseName.isEmpty() || teacher.isEmpty() || place.isEmpty()) {
Toast.makeText( this , "请填写完整的课程信息" , Toast.LENGTH_SHORT).show();
return ;
}
causebean bean = new causebean();
bean.setCoursenumber(courseNumber);
bean.setCoursename(courseName);
bean.setTeacher(teacher);
bean.setPlace(place);
SaveCourseTask task = new SaveCourseTask();
task.execute(bean);
}
private class SaveCourseTask extends AsyncTask<causebean, Void, Boolean> {
@Override
protected Boolean doInBackground(causebean... params) {
// causebean bean = params[0];
// Connection connection = null;
// PreparedStatement statement = null;
//
// try {
// // 获取数据库连接
// connection = utils.getConnection();
//
// // 创建 SQL 插入语句
// String sql = "INSERT INTO qz3 (coursenumber, coursename, teacher, place) VALUES (?, ?, ?, ?)";
//
// // 创建 PreparedStatement 对象
// statement = connection.prepareStatement(sql);
// statement.setString(1, bean.getCoursenumber());
// statement.setString(2, bean.getCoursename());
// statement.setString(3, bean.getTeacher());
// statement.setString(4, bean.getPlace());
//
// // 执行插入操作
// statement.executeUpdate();
//
// // 提交事务
// connection.commit();
//
// return true;
//
// } catch (SQLException e) {
// e.printStackTrace();
// return false;
// } finally {
// // 关闭 PreparedStatement 和 Connection
// if (statement != null) {
// try {
// statement.close();
// } catch (SQLException e) {
// e.printStackTrace();
// }
// }
// if (connection != null) {
// try {
// connection.close();
// } catch (SQLException e) {
// e.printStackTrace();
// }
// }
// }
causebean bean = params[ 0 ];
try {
// 获取数据库连接
Connection connection = utils.getConnection();
if (connection != null ) {
// 创建 SQL 插入语句
String sql = "INSERT INTO qz3 (coursenumber, coursename, teacher, place) VALUES (?, ?, ?, ?)" ;
// 创建 PreparedStatement 对象
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString( 1 , bean.getCoursenumber());
statement.setString( 2 , bean.getCoursename());
statement.setString( 3 , bean.getTeacher());
statement.setString( 4 , bean.getPlace());
// 执行插入操作
statement.executeUpdate();
// 提交事务
connection.commit();
// 关闭 PreparedStatement 和 Connection
statement.close();
connection.close();
return true ;
} else {
return false ;
}
} catch (Exception e) {
e.printStackTrace();
return false ;
}
}
@Override
protected void onPostExecute(Boolean success) {
if (success) {
// 数据保存成功
Toast.makeText(MainActivity. this , "课程添加成功" , Toast.LENGTH_SHORT).show();
etCourseNumber.setText( "" );
etCourseName.setText( "" );
etTeacher.setText( "" );
etPlace.setText( "" );
} else {
// 数据保存失败
Toast.makeText(MainActivity. this , "课程添加失败" , Toast.LENGTH_SHORT).show();
}
}
}
}
|
utils
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package JDBCUtils;
import android.annotation.SuppressLint;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class utils {
public static String url = "jdbc:mysql://192.168.17.1:3306/mydb2?useUnicode=true&characterEncoding=utf-8" ;
public static final String username = "root" ;
public static final String password = "Zcxy1029" ;
public static ThreadLocal<Connection> tl = new ThreadLocal<>();
public static Connection getConnection() throws SQLException, ClassNotFoundException {
Connection conn = tl.get();
//需要加上这个conn.isClosed()条件,你每次添加一条就关闭了数据库连接,所以需要判断如果数据库关闭了,要重新连接
if (conn == null || conn.isClosed()) {
Class.forName( "com.mysql.jdbc.Driver" );
conn = DriverManager.getConnection(url, username, password);
tl.set(conn);
conn.setAutoCommit( false );
} else {
}
return conn;
}
}
|
xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?xml version= "1.0" encoding= "utf-8" ?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id= "@+id/main"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
tools:context= ".MainActivity" >
<LinearLayout
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical"
android:padding= "16dp" >
<EditText
android:id= "@+id/etCourseNumber"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:hint= "课程编号" />
<EditText
android:id= "@+id/etCourseName"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:hint= "课程名称" />
<EditText
android:id= "@+id/etTeacher"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:hint= "教师姓名" />
<EditText
android:id= "@+id/etPlace"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:hint= "上课地点" />
<Button
android:id= "@+id/btnAdd"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center"
android:text= "添加课程" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
|