Home > Java > Java Tutorial > body text

Entry-level tutorial: Connecting to a MySQL database using JSP

王林
Release: 2024-02-01 09:41:05
Original
709 people have browsed it

Entry-level tutorial: Connecting to a MySQL database using JSP

Basic Tutorial on JSP Connecting to MySQL Database

1. Introduction to JDBC

JDBC (Java Database Connectivity) is the standard interface for Java language to access databases. It provides a unified method for interacting with databases. JDBC contains a set of interfaces and classes for connecting to databases, executing queries, updating data, and obtaining results.

2. Import the JDBC driver

Before using JDBC to connect to the database, you need to import the JDBC driver first. The JDBC driver is a JAR file that contains the implementation of the JDBC interface and classes. You can download the JDBC driver from the database manufacturer's website.

3. Establish a database connection

To establish a database connection, you can use the getConnection() method of the DriverManager class. This method requires three parameters: database URL, username and password.

// 加载JDBC驱动
Class.forName("com.mysql.jdbc.Driver");

// 建立数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
Copy after login

4. Execute the query

To execute the query, you can use the executeQuery() method of the Statement class. This method requires a SQL query statement as a parameter.

// 创建Statement对象
Statement stmt = conn.createStatement();

// 执行查询
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
Copy after login

5. Get the query results

To get the query results, you can use the next() method and getXXX() method of the ResultSet class. The next() method moves the cursor to the next line, and the getXXX() method can obtain the value of the specified column.

// 循环遍历结果集
while (rs.next()) {
  // 获取指定列的值
  int id = rs.getInt("id");
  String name = rs.getString("name");
  int age = rs.getInt("age");

  // 打印结果
  System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
}
Copy after login

6. Update data

To update data, you can use the executeUpdate() method of the Statement class. This method requires a SQL update statement as a parameter.

// 创建Statement对象
Statement stmt = conn.createStatement();

// 执行更新
int rowCount = stmt.executeUpdate("UPDATE users SET name='John Doe' WHERE id=1");

// 打印受影响的行数
System.out.println("受影响的行数:" + rowCount);
Copy after login

7. Close the database connection

After using the database connection, you need to close the connection. The connection can be closed using the close() method of the Connection class.

// 关闭数据库连接
conn.close();
Copy after login

8. Complete example

The following is a complete JSP example of connecting to a MySQL database:

<%@ page import="java.sql.*" %>

<%
  // 加载JDBC驱动
  Class.forName("com.mysql.jdbc.Driver");

  // 建立数据库连接
  Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");

  // 执行查询
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery("SELECT * FROM users");

  // 循环遍历结果集
  while (rs.next()) {
    // 获取指定列的值
    int id = rs.getInt("id");
    String name = rs.getString("name");
    int age = rs.getInt("age");

    // 打印结果
    out.println("ID: " + id + ", Name: " + name + ", Age: " + age + "
"); } // 关闭数据库连接 conn.close(); %>
Copy after login

This example will connect to a server named " test" MySQL database and query the table named "users". The query results will be displayed on the web page in the form of an HTML table.

The above is the detailed content of Entry-level tutorial: Connecting to a MySQL database using JSP. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!