Home > Java > javaTutorial > body text

What common classes and methods are used for Java database connections?

WBOY
Release: 2024-04-16 10:45:01
Original
418 people have browsed it

Java database connection requires the use of classes and methods in the java.sql package, including: DriverManager: manages the database driver and establishes a connection with the database. Connection: Represents the connection to the database. Statement: The object that executes the SQL statement. ResultSet: Object that stores query results.

What common classes and methods are used for Java database connections?

Common classes and methods for Java database connection

To connect to the database in Java, you need to use the java.sql package classes and methods in . The following introduces several common classes and methods:

Class:

  • DriverManager: Manage the database driver and establish a connection with the database .
  • Connection: represents the connection to the database.
  • Statement: The object that executes the SQL statement.
  • ResultSet: Object that stores query results.

Method:

  • DriverManager.getConnection(String url, String user, String password): Get and specify the database Connection.
  • Connection.createStatement(): Create a Statement object.
  • Statement.executeUpdate(String sql): Execute SQL statements that update the database, such as INSERT, UPDATE, and DELETE.
  • Statement.executeQuery(String sql): Execute the SQL statement that queries the database, and the results are saved in ResultSet.
  • ResultSet.next(): Move the cursor to the next line.
  • ResultSet.getString(String columnName): Get the string value of the specified column name.
  • ResultSet.getInt(String columnName): Get the integer value of the specified column name.

Practical case:

Connect to MySQL database and query a table:

import java.sql.*;

public class JdbcExample {

    public static void main(String[] args) {
        // 数据库连接信息
        String url = "jdbc:mysql://localhost:3306/database_name";
        String user = "username";
        String password = "password";

        try {
            // 获取连接
            Connection connection = DriverManager.getConnection(url, user, password);

            // 创建 Statement 对象
            Statement statement = connection.createStatement();

            // 执行查询
            ResultSet resultSet = statement.executeQuery("SELECT * FROM table_name");

            // 遍历结果
            while (resultSet.next()) {
                String name = resultSet.getString("column_name");
                int age = resultSet.getInt("column_name");
                System.out.println(name + " - " + age);
            }

            // 释放资源
            resultSet.close();
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

In the above code, we generated a database with MySQL The database connection executes a query statement and traverses the query results.

The above is the detailed content of What common classes and methods are used for Java database connections?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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