首页 > 其他分享 >4.13

4.13

时间:2024-06-05 22:11:51浏览次数:11  
标签:4.13 String id bean import android public

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>

标签:4.13,String,id,bean,import,android,public
From: https://www.cnblogs.com/Christmas77/p/18234009

相关文章

  • 24.2.13 ~ 4.13 Codeforces Round 925 & 926 & 934 & 939 (Div.3 / Div.2 * 3)
    925Div.3Solve:A~G(7/7)Rank:95Rating:\(0+706=706\)(\(1400+206=1606\))发挥评价:Normal+本场没什么有价值题目。926Div.2Solve:A~DF(5/6)Rank:72Rating:\(706+575=1281\)(\(1606+225=1831\))发挥评价:Good本场没有什么失误。CF1929E*2300(me*2300)选......
  • 【康复训练1】2.24.4.13美团春招
    前言写在前面,由于很长一段时间没有敲代码了,上周写了华为的题目,debug半天也没有debug出一道来,属实狠狠的打击到我了,因此特开此专栏,以便开启老年选手康复之路!!!第一题-塔子哥的好子矩阵前3题,手速题水题#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongcon......
  • 2024.4.13
    <?xmlversion="1.0"encoding="utf-8"?><ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.androi......
  • 4.13
    ValueError:xandymusthavesamefirstdimension,buthaveshapes(525,)and(501,) 这个错误提示意味着x轴数据和y轴数据的长度不相等。在这个例子中,x轴数据的长度是1599,而y轴数据的长度是1908。这个错误通常发生在以下情况:1.没有正确地指定x轴和y轴数据。在使用Matp......
  • 2024.4.13 模拟赛总结
    坑点总结:1.关于数据顺序模拟赛T1题面清明节,又称祭祖节,在每年4月4日至6日之间,是祭祀、祭祖和扫墓的节日。小明的爸爸妈妈决定清明假期带着他回老家扫墓。小明的爸爸一共要开车行驶1000千米才能到家,现在沿途有N个旅馆,为了安全起见,每天晚上都不开车,住在旅馆里(晚上不可以......
  • 4.13 闲话
    TopCoder13696Morphling前置知识:泰勒展开,符号化方法,无标号无根树计数我们考虑我们目前序列为\(a\),然后从每个\(i\toa_i\)连边,得到一颗基环树。我们考虑一次\((x,y)\)的影响,原本连向\(x\)的边连向了\(y\),原本连向\(y\)的边连向了\(x\),而\(x,y\)连向的边互换了,我......
  • 4.13 ACM-ICPC算法 字符串之后缀自动机
    4.13ACM-ICPC算法:字符串之后缀自动机在竞赛编程,尤其是ACM-ICPC竞赛中,字符串算法占据了极其重要的位置。其中,后缀自动机(SuffixAutomaton,简称SAM)以其强大的功能和高效的性能,成为了解决字符串问题的利器。本文旨在介绍后缀自动机的基本概念、构建方法以及在算法竞赛中的应......
  • 使用单机部署为副本集(开启oplog.rs)-4.4.13
    环境:OS:Centos7db:4.4.131.下载相应的版本https://www.mongodb.com/download-center/community我这里下载的是mongodb-linux-x86_64-rhel70-4.4.13.tgz 2.创建安装目录[root@testservices]#mkdir-p/usr/local/services[root@testservices]#mkdir-p/home/middle/mong......
  • 算法训练day36 1005.134.135.
    算法训练day361005.134.135.1005.K次取反后最大化的数组和题目1005.K次取反后最大化的数组和-力扣(LeetCode)题解代码随想录(programmercarl.com)将数字按绝对值大小排序优先将绝对值最大的负数取反剩余步骤将最小非负数取反注意数组大小顺序,以及处理剩余......
  • Linux Kernel 4.13 RC6发布:正式版9月3日发布
    美国当地时间上周末,大神LinusTorvalds发布了Linux Kernel4.13内核的又一候选版本。上周发布的RC5版本更新幅度也要比上上周的RC4要小,LinusTorvalds表示本周发布的RC6版本属于常规更新,在过去一周的开发过程中并没有出现任何意外。RC6版本主要对网络、声音和InfiniBand驱动,以及......