Java SQL 数据库操作指南264
简介Java SQL (Java Database Connectivity) 是 Java 编程语言中用于连接和操作关系型数据库的 API。它允许 Java 程序执行 SQL 语句,检索数据,更新数据库并执行其他数据库操作。
建立数据库连接要建立与数据库的连接,需要使用 DriverManager 类。下面的代码示例演示了如何使用 JDBC URL、用户名和密码建立与 MySQL 数据库的连接:
```java
import ;
import ;
public class DatabaseConnection {
public static void main(String[] args) {
String jdbcUrl = "jdbc:mysql://localhost:3306/mydb";
String username = "root";
String password = "password";
try {
Connection connection = (jdbcUrl, username, password);
("Successfully connected to the database.");
} catch (Exception e) {
();
}
}
}
```
执行 SQL 查询使用 Statement 或 PreparedStatement 对象执行 SQL 查询。Statement 对象用于执行简单的 SQL 查询,而 PreparedStatement 对象用于执行带参数的查询。
```java
import ;
import ;
import ;
import ;
import ;
public class SQLQuery {
public static void main(String[] args) {
String sql = "SELECT * FROM employees";
try (Connection connection = ("jdbc:mysql://localhost:3306/mydb", "root", "password");
Statement statement = ()) {
ResultSet rs = (sql);
while (()) {
(("ID: %d, Name: %s, Salary: %f", ("id"), ("name"), ("salary")));
}
} catch (SQLException e) {
();
}
}
}
```
更新数据库使用 Statement 或 PreparedStatement 对象执行更新操作,如插入、更新和删除。
```java
import ;
import ;
import ;
import ;
public class SQLUpdate {
public static void main(String[] args) {
String sql = "UPDATE employees SET salary = 100000 WHERE id = 1";
try (Connection connection = ("jdbc:mysql://localhost:3306/mydb", "root", "password");
Statement statement = ()) {
int rowCount = (sql);
(("%d row(s) updated", rowCount));
} catch (SQLException e) {
();
}
}
}
```
事务处理事务是一组数据库操作,它们要么全部提交要么全部回滚。事务可确保操作的原子性和一致性。
```java
import ;
import ;
import ;
import ;
public class Transaction {
public static void main(String[] args) {
String sql1 = "INSERT INTO employees (name, salary) VALUES ('John', 50000)";
String sql2 = "DELETE FROM employees WHERE id = 1";
try (Connection connection = ("jdbc:mysql://localhost:3306/mydb", "root", "password")) {
(false);
try (Statement statement = ()) {
(sql1);
(sql2);
();
("Transactions successfully committed");
} catch (SQLException e) {
();
();
}
} catch (SQLException e) {
();
}
}
}
```
连接池连接池用于管理数据库连接,以提高性能和减少创建和销毁连接的开销。
```java
import ;
import ;
public class ConnectionPool {
public static void main(String[] args) {
BasicDataSource dataSource = new BasicDataSource();
("jdbc:mysql://localhost:3306/mydb");
("root");
("password");
try (Connection connection = ()) {
// Use the connection
} catch (SQLException e) {
();
}
}
}
```
2024-10-23
下一篇:Java 初始化方法:全面指南
Python字符串查找与判断:从基础到高级的全方位指南
https://www.shuihudhg.cn/134118.html
C语言如何高效输出字符串“inc“?深度解析printf、puts及格式化输出
https://www.shuihudhg.cn/134117.html
PHP高效获取CSV文件行数:从小型文件到海量数据的最佳实践与性能优化
https://www.shuihudhg.cn/134116.html
C语言控制台图形输出:从入门到精通的ASCII艺术实践
https://www.shuihudhg.cn/134115.html
Python在Linux环境下的执行与自动化:从基础到高级实践
https://www.shuihudhg.cn/134114.html
热门文章
Java中数组赋值的全面指南
https://www.shuihudhg.cn/207.html
JavaScript 与 Java:二者有何异同?
https://www.shuihudhg.cn/6764.html
判断 Java 字符串中是否包含特定子字符串
https://www.shuihudhg.cn/3551.html
Java 字符串的切割:分而治之
https://www.shuihudhg.cn/6220.html
Java 输入代码:全面指南
https://www.shuihudhg.cn/1064.html