赞
踩
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) { Syste
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。