Android studio连接mysql,点击button按钮后对mysql里面的数据进行增删查改的具体代码
时间: 2024-02-24 14:57:41 浏览: 82
android对mysql进行增删改查
在Android Studio连接MySQL数据库的方式有很多,这里提供一种基于 JDBC 连接 MySQL 数据库的方法。以下是一个示例代码,实现了点击按钮后对 MySQL 数据库中的数据进行增删查改操作:
```java
public class MainActivity extends AppCompatActivity {
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306/test";
private static final String USER = "root";
private static final String PASS = "password";
private Connection conn = null;
private Statement stmt = null;
private EditText editName;
private EditText editAge;
private Button btnAdd;
private Button btnDelete;
private Button btnUpdate;
private Button btnQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editName = findViewById(R.id.edit_name);
editAge = findViewById(R.id.edit_age);
btnAdd = findViewById(R.id.btn_add);
btnDelete = findViewById(R.id.btn_delete);
btnUpdate = findViewById(R.id.btn_update);
btnQuery = findViewById(R.id.btn_query);
// 注册 JDBC 驱动
try {
Class.forName(JDBC_DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// 打开连接
try {
conn = DriverManager.getConnection(DB_URL, USER, PASS);
} catch (SQLException e) {
e.printStackTrace();
}
// 按钮点击事件
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editName.getText().toString();
int age = Integer.parseInt(editAge.getText().toString());
addData(name, age);
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editName.getText().toString();
deleteData(name);
}
});
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editName.getText().toString();
int age = Integer.parseInt(editAge.getText().toString());
updateData(name, age);
}
});
btnQuery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
queryData();
}
});
}
// 添加数据
private void addData(String name, int age) {
try {
stmt = conn.createStatement();
String sql = "INSERT INTO student (name, age) VALUES ('" + name + "', " + age + ")";
stmt.executeUpdate(sql);
Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除数据
private void deleteData(String name) {
try {
stmt = conn.createStatement();
String sql = "DELETE FROM student WHERE name = '" + name + "'";
stmt.executeUpdate(sql);
Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 更新数据
private void updateData(String name, int age) {
try {
stmt = conn.createStatement();
String sql = "UPDATE student SET age = " + age + " WHERE name = '" + name + "'";
stmt.executeUpdate(sql);
Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查询数据
private void queryData() {
try {
stmt = conn.createStatement();
String sql = "SELECT * FROM student";
ResultSet rs = stmt.executeQuery(sql);
StringBuilder sb = new StringBuilder();
while (rs.next()) {
String name = rs.getString("name");
int age = rs.getInt("age");
sb.append("姓名: ").append(name).append(", 年龄: ").append(age).append("\n");
}
Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 关闭连接
@Override
protected void onDestroy() {
super.onDestroy();
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
以上示例代码中,我们实现了对名为 test 的 MySQL 数据库中的 student 表进行增删查改操作。你需要根据自己的实际情况修改代码中的数据库连接信息和表名。
阅读全文