Home  >  Article  >  Java  >  How does Java database connection connect to different types of databases?

How does Java database connection connect to different types of databases?

王林
王林Original
2024-04-17 09:12:02727browse

In Java, you can use JDBC to access different databases, including: loading the JDBC driver, obtaining the connection, creating a Statement/PreparedStatement, executing the query/updating the traversal results, releasing resources

How does Java database connection connect to different types of databases?

Java Database Connection: Connecting to different types of databases

In Java, you can connect to various types of databases through the JDBC (Java Database Connectivity) API. JDBC provides a common set of Java classes and interfaces that allow applications to interact with different database systems such as MySQL, Oracle, SQL Server, etc.

Connection steps

Connecting to a database usually involves the following steps:

  1. Loading the JDBC driver:

    Class.forName("com.mysql.cj.jdbc.Driver");
  2. Get connection:

    Connection conn = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/mydb", "user", "password");
  3. Create Statement/PreparedStatement:

    Statement stmt = conn.createStatement();
  4. Execute query/update:

    ResultSet rs = stmt.executeQuery("SELECT * FROM users");
  5. Traversal results:

    while (rs.next()) {
        // 获取结果集中的值
    }
  6. Release resources:

    rs.close();
    stmt.close();
    conn.close();

Practical case: Connecting to MySQL database

The following code example demonstrates how to connect to a MySQL database and execute a query:

import java.sql.*;

public class MySQLConnection {

    public static void main(String[] args) {
        Connection conn;

        try {
            // 加载 MySQL 驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            
            // 获取连接
            conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/mydb", "user", "password");
            
            // 创建 Statement
            Statement stmt = conn.createStatement();
            
            // 执行查询
            ResultSet rs = stmt.executeQuery("SELECT * FROM users");
            
            // 遍历结果
            while (rs.next()) {
                System.out.println(rs.getInt("id") + " " + rs.getString("name"));
            }
            
            // 释放资源
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Notes on connecting to different types of databases

  • Different database systems may use different JDBC drivers.
  • The connection URL may vary depending on the specific database system.
  • SQL syntax may vary depending on the database system used.

By following these steps and taking these considerations into account, you can easily connect to various types of databases and perform various operations using Java JDBC.

The above is the detailed content of How does Java database connection connect to different types of databases?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn