java中如何连接数据库
连接数据库的基本步骤
在Java中连接数据库通常使用JDBC(Java Database Connectivity)技术。以下是连接数据库的基本步骤。
加载数据库驱动
使用Class.forName()方法加载数据库驱动。不同数据库的驱动类名不同,例如MySQL的驱动类名为com.mysql.jdbc.Driver。
Class.forName("com.mysql.jdbc.Driver");
建立数据库连接
通过DriverManager.getConnection()方法建立与数据库的连接。需要提供数据库URL、用户名和密码。
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "root";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
创建Statement对象
使用Connection对象的createStatement()方法创建Statement对象,用于执行SQL语句。
Statement statement = connection.createStatement();
执行SQL查询
通过Statement对象的executeQuery()方法执行SQL查询,返回ResultSet对象。
ResultSet resultSet = statement.executeQuery("SELECT * FROM mytable");
处理查询结果
遍历ResultSet对象获取查询结果。

while (resultSet.next()) {
String column1 = resultSet.getString("column1");
int column2 = resultSet.getInt("column2");
System.out.println(column1 + " " + column2);
}
关闭资源
使用完毕后关闭ResultSet、Statement和Connection对象,释放资源。
resultSet.close();
statement.close();
connection.close();
使用连接池提高性能
在高并发场景下,频繁创建和关闭数据库连接会影响性能。可以使用连接池技术,如HikariCP、Apache DBCP等。
HikariCP配置示例
添加HikariCP依赖后,配置连接池参数。
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/mydatabase");
config.setUsername("root");
config.setPassword("password");
config.setMaximumPoolSize(10);
HikariDataSource dataSource = new HikariDataSource(config);
Connection connection = dataSource.getConnection();
使用PreparedStatement防止SQL注入
PreparedStatement可以预编译SQL语句,提高性能并防止SQL注入。

String sql = "INSERT INTO mytable (column1, column2) VALUES (?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, "value1");
preparedStatement.setInt(2, 123);
preparedStatement.executeUpdate();
事务管理
通过Connection对象的setAutoCommit()、commit()和rollback()方法管理事务。
try {
connection.setAutoCommit(false);
// 执行多个SQL操作
connection.commit();
} catch (SQLException e) {
connection.rollback();
} finally {
connection.setAutoCommit(true);
}
使用try-with-resources自动关闭资源
Java 7及以上版本可以使用try-with-resources自动关闭资源,避免资源泄漏。
try (Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM mytable")) {
while (resultSet.next()) {
// 处理结果
}
} catch (SQLException e) {
e.printStackTrace();
}
常见数据库连接示例
MySQL连接示例
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "root";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
PostgreSQL连接示例
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/mydatabase";
String username = "postgres";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
Oracle连接示例
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String username = "scott";
String password = "tiger";
Connection connection = DriverManager.getConnection(url, username, password);
异常处理
数据库操作可能抛出SQLException,需要进行适当的异常处理。
try {
// 数据库操作
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
}






