安卓连接MySQL数据库的方式有多种,以下是其中两种常用的方式:
dependencies {
implementation 'mysql:mysql-connector-java:8.0.26'
}
然后,在代码中使用以下示例代码连接MySQL数据库:
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 static final String DB_URL = "jdbc:mysql://<MySQL服务器地址>:<端口号>/<数据库名称>";
private static final String DB_USER = "<用户名>";
private static final String DB_PASSWORD = "<密码>";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
stmt = conn.createStatement();
String sql = "SELECT * FROM <表名>";
rs = stmt.executeQuery(sql);
while (rs.next()) {
String column1 = rs.getString("column1");
String column2 = rs.getString("column2");
// 处理查询结果
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
需要替换<MySQL服务器地址>
、<端口号>
、<数据库名称>
、<用户名>
和<密码>
为实际的数据库连接信息。
connect.php
,内容如下:<?php
$servername = "<MySQL服务器地址>";
$username = "<用户名>";
$password = "<密码>";
$dbname = "<数据库名称>";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$sql = "SELECT * FROM <表名>";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$column1 = $row["column1"];
$column2 = $row["column2"];
// 处理查询结果
}
} else {
echo "0 结果";
}
$conn->close();
?>
然后,在安卓应用中使用以下示例代码发送HTTP请求访问该PHP脚本并获取数据库查询结果:
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private static final String PHP_SCRIPT_URL = "<PHP脚本的URL>";
private TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = findViewById(R.id.result_text_view);
new FetchDataAsyncTask().execute();
}
private class FetchDataAsyncTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
StringBuilder result = new StringBuilder();
try {
URL url = new URL(PHP_SCRIPT_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}
@Override
protected void onPostExecute(String result) {
resultTextView.setText(result);
}
}
}
需要将<PHP脚本的URL>
替换为实际的PHP脚本的URL。
这两种连接MySQL数据库的方式各有优劣,开发者可以根据具体需求和场景选择适合的方式。对于腾讯云的相关产品和产品介绍,可以参考腾讯云官方文档:https://cloud.tencent.com/document/product
领取专属 10元无门槛券
手把手带您无忧上云