Step 1: Add the MySQL JDBC Driver
First, you need to add the MySQL JDBC driver to your Java build path. You can download the driver from [MySQL official website](https://dev.mysql.com/downloads/connector/j/). Once downloaded, copy the JAR file to your Java build path.
Step 2: Create a database connection
In the JSP page, you need to use the DriverManager
class to create a connection to the MySQL database. You can use the following code sample to create a connection:
import java.sql.*; public class DatabaseConnection { public static void main(String[] args) { // JDBC driver name and database URL String JDBC_DRIVER = "com.mysql.jdbc.Driver"; String DB_URL = "jdbc:mysql://localhost:3306/database_name"; // Database credentials String USER = "username"; String PASS = "password"; try { // Register JDBC driver Class.forName(JDBC_DRIVER); // Open a connection Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); // Execute a query Statement stmt = conn.createStatement(); String sql = "SELECT * FROM table_name"; ResultSet rs = stmt.executeQuery(sql); // Process the results while (rs.next()) { // Retrieve column values int id = rs.getInt("id"); String name = rs.getString("name"); // Display the results System.out.println("ID: " + id + ", Name: " + name); } // Close the result set, statement and connection rs.close(); stmt.close(); conn.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } }
Step 3: Execute the SQL query
Once you have established a connection to the database, you can use Statement
or PreparedStatement
class to execute SQL queries. The following code example demonstrates how to execute a query:
// Create a statement Statement stmt = conn.createStatement(); // Execute a query String sql = "SELECT * FROM table_name"; ResultSet rs = stmt.executeQuery(sql); // Process the results while (rs.next()) { // Retrieve column values int id = rs.getInt("id"); String name = rs.getString("name"); // Display the results System.out.println("ID: " + id + ", Name: " + name); } // Close the result set and statement rs.close(); stmt.close();
Step 4: Process the query results
After executing the query, you can use the ResultSet
object to Process query results. ResultSet
The object contains all the rows in the query result set. You can use the next()
method to move to the next row and the getXXX()
method to retrieve column values.
Step 5: Close the database connection
After processing the query results, you should close the database connection. You can close the connection using the Connection.close()
method.
Complete code example
The following is a complete code example that demonstrates how to use JSP to connect to a MySQL database:
<%@ page import="java.sql.*" %> <% // JDBC driver name and database URL String JDBC_DRIVER = "com.mysql.jdbc.Driver"; String DB_URL = "jdbc:mysql://localhost:3306/database_name"; // Database credentials String USER = "username"; String PASS = "password"; // Register JDBC driver Class.forName(JDBC_DRIVER); // Open a connection Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); // Execute a query Statement stmt = conn.createStatement(); String sql = "SELECT * FROM table_name"; ResultSet rs = stmt.executeQuery(sql); // Process the results while (rs.next()) { // Retrieve column values int id = rs.getInt("id"); String name = rs.getString("name"); // Display the results out.println("ID: " + id + ", Name: " + name + "<br>"); } // Close the result set, statement and connection rs.close(); stmt.close(); conn.close(); %>
Note:
The above is the detailed content of Detailed explanation of the steps to connect JSP to MySQL database. For more information, please follow other related articles on the PHP Chinese website!