安卓写一个显示所有mysql的user表的管理界面,示例完整代码
时间: 2024-02-20 18:56:47 浏览: 57
好的,下面是一个简单的示例代码,用于在Android应用中显示MySQL数据库中的user表的数据,包括用户名和密码。
首先,我们需要在build.gradle文件中添加MySQL JDBC驱动依赖:
```
dependencies {
implementation 'mysql:mysql-connector-java:8.0.23'
}
```
接下来,我们需要创建一个User类来表示user表中的数据:
```java
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
```
然后,我们需要创建一个UserAdapter类来将user数据显示在RecyclerView中:
```java
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
private List<User> userList;
public UserAdapter(List<User> userList) {
this.userList = userList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
User user = userList.get(position);
holder.usernameText.setText(user.getUsername());
holder.passwordText.setText(user.getPassword());
}
@Override
public int getItemCount() {
return userList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView usernameText;
TextView passwordText;
public ViewHolder(@NonNull View itemView) {
super(itemView);
usernameText = itemView.findViewById(R.id.username_text);
passwordText = itemView.findViewById(R.id.password_text);
}
}
}
```
其中,user_item是显示一个user的布局文件。
接下来,我们需要在MainActivity中连接MySQL数据库,并查询user表中的数据:
```java
public class MainActivity extends AppCompatActivity {
private List<User> userList = new ArrayList<>();
private UserAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new UserAdapter(userList);
recyclerView.setAdapter(adapter);
new Thread(new Runnable() {
@Override
public void run() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM user");
while (rs.next()) {
String username = rs.getString("username");
String password = rs.getString("password");
User user = new User(username, password);
userList.add(user);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
} catch (Exception 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();
}
}
}
}).start();
}
}
```
其中,test是数据库名,root是MySQL数据库的用户名,password是MySQL数据库的密码。
最后,我们需要在activity_main布局文件中添加一个RecyclerView来显示user数据:
```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
```
这样,我们就完成了一个简单的在Android应用中显示MySQL数据库中的user表的数据的示例。
阅读全文