public class MainActivity extends AppCompatActivity {
private Button Btsearch;
private TextView Texttime;
private TextView Textdisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Btsearch = findViewById(R.id.search);
Texttime = findViewById(R.id.time);
Textdisplay = findViewById(R.id.text_display);
Btsearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="数据库操作DEMO"
android:textColor="@color/colorPrimaryDark"
android:textSize="30dp"
android:gravity="center"/>
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="2020-05-21"
android:textColor="@color/colorAccent"
android:textSize="13dp"
android:gravity="center"/>
<Button
android:id="@+id/search"
android:layout_width="120dp"
android:layout_height="100dp"
android:text="查询"
/>
<TextView
android:id="@+id/text_display"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/colorPrimaryDark"
android:text="Hello World!"
android:textColor="#BEE8B6"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:paddingTop="20dp"
/>
public void onClick(View v) {
Toast toast = Toast.makeText(getApplicationContext(), "查询按钮被点击", Toast.LENGTH_SHORT);
toast.show();
}
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://192.168.1.4/testdb","****","123456");
这是一个经典错误, Socket不能对外连接,错误不会被报出,调试的时候,能看到Exception, 一般是抛出 java.net.socketexception permission denied这个异常。
只要你的程序版想联网,就会抛出这个异常,最终联网失败。
原因是: 需要访权问到网络必须要有权限,
在AndroidManifest.xml中,需要进行如下配置:
<uses-permission android:name="android.permission.INTERNET" />
google不再允许网络请求(HTTP、Socket)等相关操作直接在Main Thread类中,所以和network有关的操作一般比较耗时,把这部分操作放到一个子线程里,然后用Handler消息机制与主线程通信.
new Thread(new Runnable() {
@Override
public void run() {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("ClassforName成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("ClassforName失败");
}
try {
java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://192.168.1.4/testdb","***","123456");
cn.close();
System.out.println("Connection连接数据库成功");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Connection连接数据库失败");
}
}
}).start();
Statement st =cn.createStatement();
ResultSet 是java中执行select后,返回的结果集类。
rs 就是结果集的变量。
这是利用来Java JDBC操作数据库源的写法。
st 实际是 Statement 对象
sql 是SQL语言,一百种用于操作数据库的语度言
st.executeQuery(sql);是执行这条知sql语言(例如: 从数据库中读取一些道信息)
ResultSet rs=st.executeQuery(sql);
ResultSet rs 存放的是从数据库中,返回来的数据结果。
String sql = "select B_Name from book";
ResultSet rs=st.executeQuery(sql);
while (rs.next()){
String mybook=rs.getString("B_Name");
System.out.println(mybook);
}
如果handler建立在线程里面,会报错
Can’t create handler inside thread that has not called Looper.prepare()
private Handler myhandler =new Handler(){
public void handleMessage(Message msg){
if(msg.what!=0){
Textdisplay.setText(text);
}
}
};
Integer i =0;
while (rs.next()){
String mybook=rs.getString("B_Name");
System.out.println(mybook);
i++;
if(i==1){
text=mybook;
}else {
text=text+mybook+'\n';
}
myhandler.sendEmptyMessage(1);
}
id.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
id=s.toString();
}
});
注意异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column ‘???’ in ‘field list’
可以用
System.out.print(sql);先将你的语句输出,这样就很容易发现有没有错误。
Btinsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"插入按钮点击",Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
@Override
public void run() {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection cn =DriverManager.getConnection("jdbc:mysql://192.168.1.4/testdb","***","123456");
System.out.println("插入数据库Connection连接数据库成功");
Statement st = cn.createStatement();
String sql ="insert into book values("+id+",'"+name+"','小明','北京出版社','2020-05-21')";
System.out.println(sql);
st.execute(sql);
System.out.println("插入数据库成功");
text="插入"+name+"数据库成功";
myhandler.sendEmptyMessage(2);
} catch (ClassNotFoundException | SQLException e) {
System.out.println("插入数据库失败");
e.printStackTrace();
}
}
}).start();
}
});
Btupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"更新按钮点击",Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
@Override
public void run() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection cn = DriverManager.getConnection("jdbc:mysql://192.168.1.4/testdb","***","123456");
System.out.println("更新数据库Connection连接数据库成功");
Statement st = cn.createStatement();
String sql = "update book set B_Author='"+pname+"' where B_Name='"+name+"'";
System.out.println(sql);
st.execute(sql);
System.out.println("更新成功");
text="更新"+name+"的作者为"+pname+"成功";
myhandler.sendEmptyMessage(3);
} catch (ClassNotFoundException | SQLException e) {
System.out.println("更新失败");
e.printStackTrace();
}
}
}).start();
}
});
Btupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"更新按钮点击",Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
@Override
public void run() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection cn = DriverManager.getConnection("jdbc:mysql://192.168.1.4/testdb","***","123456");
System.out.println("更新数据库Connection连接数据库成功");
Statement st = cn.createStatement();
String sql = "delete from book where set B_Author='"+name+"'";
System.out.println(sql);
st.execute(sql);
System.out.println("删除成功");
text="删除书名为"+name+"成功";
myhandler.sendEmptyMessage(3);
} catch (ClassNotFoundException | SQLException e) {
System.out.println("删除失败");
e.printStackTrace();
}
}
}).start();
}
});
package com.example.mysql_demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MainActivity extends AppCompatActivity {
private Button Btsearch,Btinsert,Btupdate,Btdelete;
private EditText editid,editname,editpname;
private TextView Texttime;
private TextView Textdisplay;
private String text,id,name,pname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Btsearch = findViewById(R.id.search);
Btinsert = findViewById(R.id.insert);
Btupdate =findViewById(R.id.update);
Btdelete = findViewById(R.id.delete);
editid = findViewById(R.id.isbn);
editname = findViewById(R.id.b_name);
editpname = findViewById(R.id.p_name);
Texttime = findViewById(R.id.time);
Textdisplay = findViewById(R.id.text_display);
//textview滑动条
Textdisplay.setMovementMethod(ScrollingMovementMethod.getInstance());
//查询按钮点击事件
Btsearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast = Toast.makeText(getApplicationContext(), "查询按钮被点击", Toast.LENGTH_SHORT);
toast.show();
text = "查询结果为:\n";
//建一个子线程 数据库操作属于网络操作,不能用UI线程
new Thread(new Runnable() {
@Override
public void run() {
try {
//第一步 寻找这个驱动,JVM加入内存
Class.forName("com.mysql.jdbc.Driver");
System.out.println("ClassforName成功");
//第二步 通过connection数据库建立链接对象
java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://192.168.1.4/testdb","***","123456");
System.out.println("Connection连接数据库成功");
//第三步 通过cn链接对象 创建一个statement对象,操作对象
Statement st =cn.createStatement();
//第四步 写一个SQL语句
String sql = "select B_Name from book";
//第五步 执行SQL语句,返回结果集到rs对象
//statement类的executeQuery()方法来下达select指令以查询数据库
ResultSet rs=st.executeQuery(sql);
System.out.println(rs);
//第六步 返回结果集得到字符串,某一列
// Integer i =0;
while (rs.next()){
String mybook=rs.getString("B_Name");
System.out.println(mybook);
// i++;
// if(i==1){
// text=mybook;
// }else {
text=text+mybook+'\n';
// }
myhandler.sendEmptyMessage(1);
}
//class 异常
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("ClassforName失败");
//Connection 异常
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Connection连接数据库失败");
}
}
}).start();
}
});
//插入按钮事件
Btinsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"插入按钮点击",Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
@Override
public void run() {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection cn =DriverManager.getConnection("jdbc:mysql://192.168.1.4/testdb","***","123456");
System.out.println("插入数据库Connection连接数据库成功");
Statement st = cn.createStatement();
String sql ="insert into book values("+id+",'"+name+"','小明','河南出版社','2020-05-21')";
System.out.println(sql);
st.execute(sql);
System.out.println("插入成功");
text="插入"+name+"数据库成功";
myhandler.sendEmptyMessage(2);
} catch (ClassNotFoundException | SQLException e) {
System.out.println("插入失败");
e.printStackTrace();
}
}
}).start();
}
});
//更新数据库
Btupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"更新按钮点击",Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
@Override
public void run() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection cn = DriverManager.getConnection("jdbc:mysql://192.168.1.4/testdb","***","123456");
System.out.println("更新数据库Connection连接数据库成功");
Statement st = cn.createStatement();
String sql = "update book set B_Author='"+pname+"' where B_Name='"+name+"'";
System.out.println(sql);
st.execute(sql);
System.out.println("更新成功");
text="更新"+name+"的作者为"+pname+"成功";
myhandler.sendEmptyMessage(3);
} catch (ClassNotFoundException | SQLException e) {
System.out.println("更新失败");
e.printStackTrace();
}
}
}).start();
}
});
//删除数据库
Btdelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"删除按钮点击",Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
@Override
public void run() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection cn = DriverManager.getConnection("jdbc:mysql://192.168.1.4/testdb","***","123456");
System.out.println("删除数据库Connection连接数据库成功");
Statement st = cn.createStatement();
String sql = "delete from book where B_Name='"+name+"'";
System.out.println(sql);
st.execute(sql);
System.out.println("删除成功");
text="删除书名为"+name+"成功";
myhandler.sendEmptyMessage(4);
} catch (ClassNotFoundException | SQLException e) {
System.out.println("删除失败");
e.printStackTrace();
}
}
}).start();
}
});
//id文本框输入事件
editid.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Textdisplay.setText("输入结束后的内容为 [" + s.toString()+" ] 即将显示在屏幕上");
id=s.toString();
}
});
//图书文本框输入事件
editname.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Textdisplay.setText("输入结束后的内容为 [" + s.toString()+" ] 即将显示在屏幕上");
name = s.toString();
}
});
//作者文本框输入事件
editpname.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Textdisplay.setText("输入结束后的内容为 [" + s.toString()+" ] 即将显示在屏幕上");
pname = s.toString();
}
});
}
private Handler myhandler =new Handler(){
public void handleMessage(Message msg){
if(msg.what==1){
Textdisplay.setText(text);
}if (msg.what == 2){
Textdisplay.setText(text);
}if (msg.what == 3){
Textdisplay.setText(text);
}if(msg.what == 4){
Textdisplay.setText(text);
}
}
};
}
<?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:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="数据库操作DEMO"
android:textColor="@color/colorPrimaryDark"
android:textSize="30dp"
android:gravity="center"/>
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="2020-05-21"
android:textColor="@color/colorAccent"
android:textSize="13dp"
android:gravity="center"/>
<EditText
android:id="@+id/isbn"
android:layout_width="200dp"
android:layout_height="40dp"
android:hint="ISBM"/>
<EditText
android:id="@+id/b_name"
android:layout_width="200dp"
android:layout_height="40dp"
android:hint="图书名字"/>
<EditText
android:id="@+id/p_name"
android:layout_width="200dp"
android:layout_height="40dp"
android:hint="作者名字"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/search"
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="查询"
/>
<Button
android:id="@+id/insert"
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="插入"
/>
<Button
android:id="@+id/update"
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="更新"
/>
<Button
android:id="@+id/delete"
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="删除"
/>
</LinearLayout>
<TextView
android:id="@+id/text_display"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/colorPrimaryDark"
android:text="Hello World!"
android:textColor="#BEE8B6"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:paddingTop="20dp"
android:scrollbars="vertical"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mysql_demo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
public class MainActivity extends AppCompatActivity implements View.OnClickListener
protected void onCreate(Bundle savedInstanceState) {
//第一种myclick实现 用 implements OnclickListener
//所有用this代替
Btsearch.setOnClickListener(this);
Btinsert.setOnClickListener(this);
Btupdate.setOnClickListener(this);
Btdelete.setOnClickListener(this);
//设置Tag标签为1,标识查询
//1--查询 2--插入 3--更新 4--删除
Btsearch.setTag(1);
Btinsert.setTag(2);
Btupdate.setTag(3);
Btdelete.setTag(4);
}
@Override
public void onClick(View v) {
//由于多个按钮作用不太一样,所以先识别Tag标签来区分每个按钮,由于Tag为object对象,所以需要强制装换Integer
//1--查询 2--插入 3--更新 4--删除
Integer tagid = (Integer) v.getTag();
if(tagid==1){
Toast toast = Toast.makeText(getApplicationContext(), "查询按钮被点击", Toast.LENGTH_SHORT);
toast.show();
text = "查询结果为:\n";
//建一个子线程 数据库操作属于网络操作,不能用UI线程
new Thread(new Runnable() {
@Override
public void run() {
try {
//第一步 寻找这个驱动,JVM加入内存
Class.forName("com.mysql.jdbc.Driver");
System.out.println("ClassforName成功");
//第二步 通过connection数据库建立链接对象
java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://192.168.1.4/testdb","***","123456");
System.out.println("Connection连接数据库成功");
//第三步 通过cn链接对象 创建一个statement对象,操作对象
Statement st =cn.createStatement();
//第四步 写一个SQL语句
String sql = "select B_Name from book";
//第五步 执行SQL语句,返回结果集到rs对象
//statement类的executeQuery()方法来下达select指令以查询数据库
ResultSet rs=st.executeQuery(sql);
System.out.println(rs);
//第六步 返回结果集得到字符串,某一列
while (rs.next()){
String mybook=rs.getString("B_Name");
System.out.println(mybook);
text=text+mybook+'\n';
myhandler.sendEmptyMessage(1);
}
//class 异常
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("ClassforName失败");
//Connection 异常
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Connection连接数据库失败");
}
}
}).start();
}if(tagid==2){
Toast.makeText(getApplicationContext(),"插入按钮点击",Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
@Override
public void run() {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection cn =DriverManager.getConnection("jdbc:mysql://192.168.1.4/testdb","***","123456");
System.out.println("插入数据库Connection连接数据库成功");
Statement st = cn.createStatement();
String sql ="insert into book values("+id+",'"+name+"','小明','河南出版社','2020-05-21')";
System.out.println(sql);
st.execute(sql);
System.out.println("插入成功");
text="插入"+name+"数据库成功";
myhandler.sendEmptyMessage(2);
} catch (ClassNotFoundException | SQLException e) {
System.out.println("插入失败");
e.printStackTrace();
}
}
}).start();
}
if(tagid==3){
Toast.makeText(getApplicationContext(),"更新按钮点击",Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
@Override
public void run() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection cn = DriverManager.getConnection("jdbc:mysql://192.168.1.4/testdb","***","123456");
System.out.println("更新数据库Connection连接数据库成功");
Statement st = cn.createStatement();
String sql = "update book set B_Author='"+pname+"' where B_Name='"+name+"'";
System.out.println(sql);
st.execute(sql);
System.out.println("更新成功");
text="更新"+name+"的作者为"+pname+"成功";
myhandler.sendEmptyMessage(3);
} catch (ClassNotFoundException | SQLException e) {
System.out.println("更新失败");
e.printStackTrace();
}
}
}).start();
}
if(tagid==4){
Toast.makeText(getApplicationContext(),"删除按钮点击",Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
@Override
public void run() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection cn = DriverManager.getConnection("jdbc:mysql://192.168.1.4/testdb","***","123456");
System.out.println("删除数据库Connection连接数据库成功");
Statement st = cn.createStatement();
String sql = "delete from book where B_Name='"+name+"'";
System.out.println(sql);
st.execute(sql);
System.out.println("删除成功");
text="删除书名为"+name+"成功";
myhandler.sendEmptyMessage(4);
} catch (ClassNotFoundException | SQLException e) {
System.out.println("删除失败");
e.printStackTrace();
}
}
}).start();
}
}
package com.example.mysql_demo;
import androidx.appcompat.app.AppCompatActivity;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,TextWatcher{
private Button Btsearch,Btinsert,Btupdate,Btdelete;
private EditText editid,editname,editpname;
private TextView Texttime;
private TextView Textdisplay;
private String text,id,name,pname;
java.sql.Connection cn= null;
Statement st= null;
ResultSet rs= null;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Textdisplay.setText("输入结束后的内容为 [" + s.toString()+" ] 即将显示在屏幕上");
//依靠getEditableText()判断是哪个文本框
if(s==editid.getEditableText()){
id=s.toString();
}
if(s==editpname.getEditableText()){
pname=s.toString();
}
if(s==editname.getEditableText()){
name=s.toString();
}
}
public void MysqlConnect(){
try {
//第一步 寻找这个驱动,JVM加入内存
Class.forName("com.mysql.jdbc.Driver");
System.out.println("ClassforName成功");
//第二步 通过connection数据库建立链接对象
cn = DriverManager.getConnection("jdbc:mysql://192.168.1.4/testdb","***","123456");
System.out.println("Connection连接数据库成功");
//第三步 通过cn链接对象 创建一个statement对象,操作对象
st =cn.createStatement();
//class 异常
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("ClassforName失败");
text="ClassforName失败";
// myhandler.sendEmptyMessage(1);
//Connection 异常
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Connection连接数据库失败");
text="Connection连接数据库失败";
// myhandler.sendEmptyMessage(1);
}
}
public void Mysqlupdate(String sql){
if (sql.indexOf("select")==0){
try {
MysqlConnect();
System.out.println("执行第111111111111111111111111111111111111111步");
rs=st.executeQuery(sql);
//class 异常
} catch (SQLException e) {
e.printStackTrace();
System.out.println("查询数据库失败");
// text="Connection连接数据库失败";
// myhandler.sendEmptyMessage(1);
}
}else{
try {
MysqlConnect();
System.out.println("执行第222222222222222222222222222222222222222222步");
st.execute(sql);
//class 异常
} catch (SQLException e) {
e.printStackTrace();
if(sql.indexOf("insert")==0){
System.out.println("插入数据库失败");
}
if(sql.indexOf("update")==0){
System.out.println("更新数据库失败");
}
if(sql.indexOf("delete")==0){
System.out.println("删除数据库失败");
}
// text="Connection连接数据库失败";
// myhandler.sendEmptyMessage(1);
}
}
}
@Override
public void onClick(View v) {
//由于多个按钮作用不太一样,所以先识别Tag标签来区分每个按钮,由于Tag为object对象,所以需要强制装换Integer
//1--查询 2--插入 3--更新 4--删除
Integer tagid = (Integer) v.getTag();
if(tagid==1){
Toast toast = Toast.makeText(getApplicationContext(), "查询按钮被点击", Toast.LENGTH_SHORT);
toast.show();
text = "查询结果为:\n";
//建一个子线程 数据库操作属于网络操作,不能用UI线程
new Thread(new Runnable() {
@Override
public void run() {
String sql = "select B_Name from book";
Mysqlupdate(sql);
try {
//第六步 返回结果集得到字符串,某一列
while (rs.next()) {
String mybook = rs.getString("B_Name");
System.out.println(mybook);
text = text + mybook + '\n';
myhandler.sendEmptyMessage(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}).start();
}
if(tagid==2){
Toast.makeText(getApplicationContext(),"插入按钮点击",Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
@Override
public void run() {
String sql ="insert into book values("+id+",'"+name+"','小明','河南出版社','2020-05-21')";
Mysqlupdate(sql);
text="插入"+name+"数据库成功";
myhandler.sendEmptyMessage(2);
}
}).start();
}
if(tagid==3){
Toast.makeText(getApplicationContext(),"更新按钮点击",Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
@Override
public void run() {
String sql = "update book set B_Author='"+pname+"' where B_Name='"+name+"'";
Mysqlupdate(sql);
text="更新"+name+"的作者为"+pname+"成功";
myhandler.sendEmptyMessage(3);
}
}).start();
}
if(tagid==4){
Toast.makeText(getApplicationContext(),"删除按钮点击",Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
@Override
public void run() {
String sql = "delete from book where B_Name='"+name+"'";
Mysqlupdate(sql);
text="删除书名为"+name+"成功";
myhandler.sendEmptyMessage(4);
}
}).start();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Btsearch = findViewById(R.id.search);
Btinsert = findViewById(R.id.insert);
Btupdate =findViewById(R.id.update);
Btdelete = findViewById(R.id.delete);
editid = findViewById(R.id.isbn);
editname = findViewById(R.id.b_name);
editpname = findViewById(R.id.p_name);
Texttime = findViewById(R.id.time);
Textdisplay = findViewById(R.id.text_display);
//textview滑动条
Textdisplay.setMovementMethod(ScrollingMovementMethod.getInstance());
//第一种myclick实现 用 implements OnclickListener
//所有用this代替
Btsearch.setOnClickListener(this);
Btinsert.setOnClickListener(this);
Btupdate.setOnClickListener(this);
Btdelete.setOnClickListener(this);
//设置Tag标签
//1--查询 2--插入 3--更新 4--删除
Btsearch.setTag(1);
Btinsert.setTag(2);
Btupdate.setTag(3);
Btdelete.setTag(4);
//所有文本 输入框用接口implements TextWatcher实现
editid.addTextChangedListener(this);
editpname.addTextChangedListener(this);
editname.addTextChangedListener(this);
}
private Handler myhandler =new Handler(){
public void handleMessage(Message msg){
if(msg.what!=0){
Textdisplay.setText(text);
}
}
};
}