android访问mysql增删改查_Android Studio连接数据库实现增删改查
时间: 2023-08-05 21:44:21 浏览: 194
好的,我了解了。针对你的问题,我可以给你一些基本的步骤和思路,帮助你实现 Android Studio 连接 MySQL 数据库并实现增删改查功能。
1. 配置 MySQL 数据库
首先,你需要在本地或者远程服务器上搭建一个 MySQL 数据库,并创建一张表用于存储数据。这一步需要你具备一定的数据库管理知识,如果你不熟悉,请参考相关的教程或者请专业人士帮忙。
2. 导入 MySQL 驱动
将 MySQL 驱动包(JDBC)导入到 Android Studio 项目中。你可以在 Maven 中心库中找到对应版本的 MySQL 驱动,或者手动下载 jar 包导入到项目中。在项目的 build.gradle 文件中添加以下代码:
```
dependencies {
// ...
implementation 'mysql:mysql-connector-java:8.0.23'
}
```
3. 编写数据库连接代码
在你的 Android Studio 项目中创建一个数据库连接类,用于连接 MySQL 数据库并执行 SQL 语句。代码示例:
```java
import java.sql.*;
public class MySQLConnection {
// 数据库连接信息
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/mydatabase";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
// 获取数据库连接
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return connection;
}
// 关闭数据库连接
public static void closeConnection(Connection connection, Statement statement, ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
4. 实现增删改查功能
在你的 Android Studio 项目中编写增删改查的代码,使用上面创建的数据库连接类来执行 SQL 语句。代码示例:
```java
import java.sql.*;
public class MySQLUtils {
// 插入数据
public static void insert(String name, String email) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = MySQLConnection.getConnection();
String sql = "INSERT INTO user(name, email) VALUES (?, ?)";
statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, email);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
MySQLConnection.closeConnection(connection, statement, null);
}
}
// 删除数据
public static void delete(int id) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = MySQLConnection.getConnection();
String sql = "DELETE FROM user WHERE id=?";
statement = connection.prepareStatement(sql);
statement.setInt(1, id);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
MySQLConnection.closeConnection(connection, statement, null);
}
}
// 更新数据
public static void update(int id, String name, String email) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = MySQLConnection.getConnection();
String sql = "UPDATE user SET name=?, email=? WHERE id=?";
statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, email);
statement.setInt(3, id);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
MySQLConnection.closeConnection(connection, statement, null);
}
}
// 查询数据
public static void query() {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = MySQLConnection.getConnection();
String sql = "SELECT * FROM user";
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String email = resultSet.getString("email");
System.out.println("id: " + id + ", name: " + name + ", email: " + email);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
MySQLConnection.closeConnection(connection, statement, resultSet);
}
}
}
```
这样,你就可以在 Android Studio 中连接 MySQL 数据库并实现增删改查功能了。当然,这只是一个简单的示例,实际情况可能会更加复杂,需要你根据具体的需求进行调整。
阅读全文