要在Android Studio中从MySQL数据库展示项目中的数据,你需要遵循以下步骤:
1. 添加MySQL Connector/J依赖项到你的项目的build.gradle文件中:
```groovy
dependencies {
implementation 'mysql:mysql-connector-java:8.0.23'
}
```
2. 在AndroidManifest.xml文件中添加INTERNET权限:
```xml
<uses-permission android:name="android.permission.INTERNET" />
```
3. 创建一个用于连接MySQL数据库的类,例如`DatabaseHelper.java`:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class DatabaseHelper {
private static final String URL = "jdbc:mysql://your_server_ip:3306/your_database_name";
private static final String USERNAME = "your_username";
private static final String PASSWORD = "your_password"; public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return connection;
}
}
```
4. 创建一个用于执行SQL查询并返回结果集的类,例如`DataModel.java`:
```java
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;public class DataModel {
private int id;
private String name; public DataModel(int id, String name) {
this.id = id;
this.name = name;
} // Getter and Setter methods for id and name
}
```
5. 在你的主Activity中,使用`DatabaseHelper`类连接到MySQL数据库,并执行查询以获取数据:
```java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayAdapter<DataModel> adapter;
private List<DataModel> dataList; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); listView = findViewById(R.id.listView);
dataList = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList);
listView.setAdapter(adapter); connectToDatabase();
} private void connectToDatabase() {
String query = "SELECT * FROM your_table_name";
try {
Connection connection = DatabaseHelper.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query); while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
DataModel dataModel = new DataModel(id, name);
dataList.add(dataModel);
} resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
6. 最后,确保你的`activity_main.xml`布局文件中包含一个`ListView`:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> <ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" /></LinearLayout>
```
现在,当你运行应用程序时,它将连接到MySQL数据库并从表中获取数据,然后在`ListView`中显示这些数据。
标签:java,name,sql,private,Studio,MySQL,import,Android,id From: https://blog.51cto.com/M82A1/8341474