Home > Java > Java Tutorial > body text

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

王林
Release: 2024-04-17 09:12:02
Original
609 people have browsed it

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");
    Copy after login
  2. Get connection:

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

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

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

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

    rs.close();
    stmt.close();
    conn.close();
    Copy after login

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();
        }
    }
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!