我正在開發一個 android studio 專案,并希望能夠從我正在編程的 pc 上的資料庫中訪問資料。我想使用 MySQL 資料庫,因為我以前使用過它們。我想知道是否可以從android studio中做到這一點。如果可能的話,我的代碼是什么?我查看了其他人的問題和答案,所有這些都為我產生了錯誤。提前致謝
我已經嘗試了以下方法,并且我的所有 ip、埠、用戶名和密碼資訊都是正確的,但我不斷收到以下錯誤:E/Error in connection: com.mysql.jdbc.Driver 宣告我的屬性后我的連接類中的代碼是如下
//declaring connection policy
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String ConnectionURL = null;
//starts actual connection to db
try
{
//establishing connection to Database
//all hard coded
Class.forName("com.mysql.jdbc.Driver");
ConnectionURL= "jdbc:jtds:sqlserver://" ip ":" port ";" "databasename=" database ";user=" uname ";password=" pass ";";
connection = DriverManager.getConnection(ConnectionURL);
}
//Catches any error in the data
catch (Exception e)
{
//executes if there is a error connecting to the database
Log.e("Error in connection", e.getMessage());
System.out.println("Error connecting to db");
}
//returns the connection to the main class
//it will then be sent to the DBmanager class and used to get the data out of the database
return connection;
}
}
uj5u.com熱心網友回復:
不鼓勵將應用程式直接連接到 mysql 服務器(以及其他 rdms),因為您需要這樣做才能通過直接訪問將資料庫打開到 Internet,這是一個致命的安全風險。
搜索
- REST API MySQL 安卓
你會發現很多教程
像這個簡短的https://phppot.com/php/php-mysql-rest-api-for-android/或這個https://www.javahelps.com/2018/12/access-mysql-from-android-通過.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446611.html
上一篇:檢查MenuItem是否為空
