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(); }
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:
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(); }
CallableStatement
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(); }
ResultSetMetaData
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(); }
public class Student { private String name; private int age; // getter and setter methods }
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(); }
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(); }
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!