是的,您可以直接连接到远程数据库。我不确定它的安全性。
尝试这个:
下载MySQL JDBC
在您的 Android 项目中添加 .jar 文件并将其添加为库。
创建一个扩展 AsyncTask 的类并在那里执行该方法。
public class connectToServer extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... args) {
try {
connect();
} catch (Exception e) {
Log.e("Error", e.toString());
}
return null;
}
@Override
protected void onPostExecute(String file_url)
{
}
}
public void connect(){
Log.e("Android", "SQL Connection start");
Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String ip = "[your host server]";
String dbName = "[your dbName]";
String user = "[your userName]";
String password = "[your password]";
String connString = "jdbc:mysql://" + ip + "/" + dbName +
"";
conn = DriverManager.getConnection(connString, user, password);
Log.e("Connection", "Open");
Statement statement = conn.createStatement();
ResultSet set = statement.executeQuery("Select * from [table]");
statement.setQueryTimeout(0);
while(set.next())
{
Log.e("Data:", set.getString("[column_name]"));
}
}catch(Exception e)
{
Log.e("Error connection", e.toString());
}
}//end method