- 資料庫 :mysql 5.1.7
- JDBC: 同資料庫版本
最開始我用的資料庫mysql8.0.22+mysql-connector-java-8.0.22,什么方法都試過,就是Android連不上,但Navicat連接都是好好的,
懷疑是以前網站、微信小程式資料都是放在這個資料庫上,改了太多東西估計自己也不記得了,,然后我又裝了一個mysql,于是就OK了

一、配置mysql驅動(mysql-connector-java-5.1.7.jar)

把jar包放到libs下面,將其作為dependence
可以直接右鍵 ->add as lib
或者file->project structure->dependence,
ok以后as會自動進行同步,當然,如果要把jar包洗掉,記得要移除dependence

二、
首先確保:
1 應用層埠號是打開的(mysql是3306,但也可能配置時用的是3307、3308)
2 確保遠程連接是允許的
3 確保驅動位置正確

4 url確保要和服務器同步時區 serverTimezone=UTC
5 確保資料庫是開著的: net start 資料庫名
6 連接mysql要呼叫中斷,連接資料庫的邏輯最好不要寫在主執行緒里面,不然會影響主執行緒中其他代碼的執行效率,Android studio的架構上估計是強制連接資料庫要開辟另外一個執行緒,所以DriverManager.getConnection(url, user, password) 要寫在另一個執行緒里,
具體代碼:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
private static String driver = "com.mysql.jdbc.Driver";// MySql驅動
private static String url = "jdbc:mysql://1.116.78.161/庫名?useSSL=false&serverTimezone=UTC&characterEncoding=utf8";
private static String user = "**";// 資料庫用戶名
private static String password = "****";// 資料庫密碼
private TextView textView;
private TextView Contents;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();
}
/**
* 初始化組件
*/
private void Init(){
Contents=(TextView)findViewById(R.id.content);
textView=(TextView)findViewById(R.id.TextView);
ButtonClick buttonClick=new ButtonClick();
textView.setOnClickListener(buttonClick);
}
/**
* 向資料庫追加資料
* @param link
* @throws SQLException
*/
private void setData(Connection link) throws SQLException {
String sql = "insert into artical(id,articalName,artical) values(?,?,?)";
PreparedStatement pst=link.prepareStatement(sql);
pst.setObject(1,222);
pst.setObject(2,"aaa");
pst.setObject(3,"ffaaaf");
pst.execute();
}
/**
* 獲取資料庫中的資料
* @param link
* @return
* @throws SQLException
*/
private String getData(Connection link) throws SQLException{
String Aritical = null;
String commond = "select * from artical where id=2"; //找到id=2的行
PreparedStatement pst = link.prepareStatement(commond);
ResultSet rs = pst.executeQuery();
if (rs != null){
int column = rs.getMetaData().getColumnCount();
while (rs.next()){
String name = rs.getMetaData().getColumnName(3);
Aritical = rs.getString(name);
}
pst.close();
return Aritical;
}else {
return null;
}
}
private class ButtonClick implements View.OnClickListener{
@Override
public void onClick(View v) {
// Android的資料庫連接不能在主執行緒進行
new Thread(new Runnable() {
@Override
public void run() {
try{
Class.forName(driver);// 動態加載類
Connection link = DriverManager.getConnection(url, user, password);
Log.i("","SUCCESS");
//TO DO
//.............操作資料庫.............
String articals = getData(link);
Contents.setText(articals);
//The end
link.close();
} catch (Exception e) {
Log.e("","ERROR");
e.printStackTrace();
}
}
}).start();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/275025.html
標籤:其他
上一篇:使用Fragment
下一篇:鴻蒙WebView
