Home > Java > Java Tutorial > body text

Database technology and database management technology in Java

WBOY
Release: 2023-06-08 00:01:04
Original
986 people have browsed it

Java is a cross-platform, object-oriented programming language that is widely used in the development and implementation of various applications. In the development process of Java applications, database technology and database management technology are also very important. This article will explore database technology and database management technology in Java, including the basic concepts of databases, database connections, methods of operating databases, data storage and management, etc.

1. The basic concept of database

A database refers to a collection of data that is organized and stored together according to certain rules and can be used by multiple users. Its function is to provide services for enterprises or Organizations provide functions for data storage and management. Commonly used database management systems include MySQL, Oracle, SQL Server, etc.

In Java, access and operations to the database require the use of Java Database Connectivity (JDBC) technology, which is a Java API for executing SQL statements and consists of a set of protocols between Java applications and databases. Composed of components and interfaces.

2. Implementation of database connection

In Java, to access the database, you need to establish a connection with the database first. In JDBC, use the static method getConnection() method of the DriverManager class to obtain the connection to the database. The getConnection() method needs to pass three parameters: a URL, username and password.

For example, create a MySQL database named "test", create a table named "student", and set their username and password to root and 123456 respectively, you can use the following code to create the same Database connection:

Connection conn = null;
try {
    Class.forName("com.mysql.cj.jdbc.Driver");
    String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC";
    String user = "root";
    String password = "123456";
    conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
    e.printStackTrace();
}
Copy after login

The Class.forName() method is used to load the MySQL JDBC driver.

3. How to operate the database

JDBC provides some interfaces and classes to operate the database, mainly including the following aspects:

  1. Statement Both Statement and PreparedStatement interfaces are used to execute SQL statements. The difference is that PreparedStatement can prevent SQL injection attacks.
For example, to query all data in a table, you can use the following code:

Statement stmt = null;
ResultSet rs = null;
try {
    stmt = conn.createStatement();
    String sql = "SELECT * FROM student";
    rs = stmt.executeQuery(sql);
    while(rs.next()) {
        System.out.println(rs.getString("name") + " " + rs.getInt("age"));
    }
} catch (Exception e) {
    e.printStackTrace();
}
Copy after login

CallableStatement

  1. CallableStatement interface is used to call stored procedures and functions. It is a sub-interface of Statement.
For example, to call a stored procedure named "getStudentNum", you can use the following code:

CallableStatement cstmt = null;
try {
    String sql = "{call getStudentNum(?)}";
    cstmt = conn.prepareCall(sql);
    cstmt.registerOutParameter(1, Types.INTEGER);
    cstmt.execute();
    System.out.println(cstmt.getInt(1));
} catch (Exception e) {
    e.printStackTrace();
}
Copy after login

Among them, the registerOutParameter() method is used to register output parameters.

ResultSetMetaData

  1. The ResultSetMetaData interface can obtain metadata information of query results.
For example, to obtain the field name and type of a table, you can use the following code:

ResultSetMetaData rsmd = null;
try {
    stmt = conn.createStatement();
    String sql = "SELECT * FROM student";
    rs = stmt.executeQuery(sql);
    rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    for (int i = 1; i <= columnCount; i++) {
        System.out.println(rsmd.getColumnName(i) + " " + rsmd.getColumnTypeName(i));
    }
} catch (Exception e) {
    e.printStackTrace();
}
Copy after login

4. Data storage and management

In Java, data storage and management can This is achieved by accessing the database. JavaBeans are usually used to encapsulate the data, and then the data is written to or read from the database through JDBC.

For example, create a JavaBean class named Student:

public class Student {
    private String name;
    private int age;
    // getter and setter methods
}
Copy after login

Next, you can use PreparedStatement to write data to the database.

PreparedStatement pstmt = null;
try {
    String sql = "INSERT INTO student(name, age) VALUES(?, ?)";
    pstmt = conn.prepareStatement(sql);
    pstmt.setString(1, "Tom");
    pstmt.setInt(2, 20);
    pstmt.executeUpdate();
} catch (Exception e) {
    e.printStackTrace();
}
Copy after login

At the same time, you can also use Statement or PreparedStatement to read data from the database.

For example, to read the information of a student named "Tom" you can use the following code:

PreparedStatement pstmt = null;
ResultSet rs = null;
try {
    String sql = "SELECT * FROM student WHERE name=?";
    pstmt = conn.prepareStatement(sql);
    pstmt.setString(1, "Tom");
    rs = pstmt.executeQuery();
    if(rs.next()) {
        Student student = new Student();
        student.setName(rs.getString("name"));
        student.setAge(rs.getInt("age"));
        System.out.println(student.getName() + " " + student.getAge());
    }
} catch (Exception e) {
    e.printStackTrace();
}
Copy after login
Summary

Database technology and database management technology in Java are Java An integral part of application development. Through JDBC technology, the connection with various relational databases is realized, and a rich set of classes and interfaces are provided to operate the database, process the result set, and read and write data. In actual development, we need to choose appropriate solutions and optimization methods according to specific situations to improve the performance and maintainability of the program.

The above is the detailed content of Database technology and database management technology in Java. 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!