java连接mysql数据库的代码并通过java语言实现增删改查
时间: 2023-11-13 15:11:11 浏览: 50
好的,以下是Java连接MySQL数据库的代码,并通过Java语言实现增删改查的示例:
首先,需要导入MySQL连接的Java驱动程序,可以在项目中添加如下依赖:
```xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
```
接下来,可以编写数据库连接的工具类:
```java
import java.sql.*;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf8";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
public static Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
其中,URL、USERNAME、PASSWORD分别对应MySQL数据库的连接地址、用户名和密码。getConnection()方法返回一个数据库连接,close()方法用于关闭数据库连接、Statement和ResultSet。
接下来,可以编写增删改查的示例代码:
```java
import java.sql.*;
public class Main {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
// 插入数据
stmt = conn.createStatement();
String sql = "INSERT INTO user(name, age) VALUES('Tom', 18)";
stmt.executeUpdate(sql);
// 更新数据
sql = "UPDATE user SET age = 20 WHERE name = 'Tom'";
stmt.executeUpdate(sql);
// 查询数据
sql = "SELECT * FROM user";
rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("id:" + id + ", name:" + name + ", age:" + age);
}
// 删除数据
sql = "DELETE FROM user WHERE name = 'Tom'";
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, rs);
}
}
}
```
其中,通过执行Statement的executeUpdate()方法可以实现插入、更新、删除操作,通过执行Statement的executeQuery()方法可以实现查询操作。最后,记得关闭数据库连接、Statement和ResultSet。
希望这个示例能够帮助你理解Java连接MySQL数据库并实现增删改查的过程。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)