Home > Database > Mysql Tutorial > body text

java mysql stored procedure

WBOY
Release: 2023-05-18 17:23:37
Original
491 people have browsed it

Java MySQL Stored Procedure

MySQL is a popular relational database management system that provides many stored procedure functions. Using stored procedures reduces the complexity of building and managing applications. In this article, we will discuss ways to write and execute MySQL stored procedures using Java.

Introduction to stored procedures

A stored procedure is a series of SQL statements stored in an RDBMS. Stored procedures can be called based on predefined parameters, which can be used in the application to pass to the stored procedure and perform the required operations. MySQL stored procedures are currently better, especially for use in applications.

Define a MySQL stored procedure

Defining a MySQL stored procedure is very simple, just use the CREATE PROCEDURE statement in the database. Here is an example:

DELIMITER //
CREATE PROCEDURE test_sp(IN num1 INT, IN num2 INT, OUT sum INT)
BEGIN
SET sum = num1 num2;
END //
DELIMITER ;

In the above code, we created a stored procedure named "test_sp" and defined two input parameters num1 and num2, and an output parameter sum. Inside the stored procedure body, we perform a simple addition operation and store the result in sum.

Execute MySQL stored procedures

Using a Java program to execute MySQL stored procedures requires the following steps:

  1. Connect to the MySQL database through JDBC.
  2. Create a CallableStatement object, which will use the name of the MySQL stored procedure and its parameters.
  3. Set the value of the input parameter.
  4. Register output parameters and obtain the value of the output parameters after executing the stored procedure.
  5. Execute the stored procedure and close the connection.

Here is a Java program that uses the above steps to execute the above example stored procedure:

try {

// Connect to MySQL database
Connection conn = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/mydatabase", "username", "password");

// Create CallableStatement for test_sp stored procedure
CallableStatement cs = conn.prepareCall("{CALL test_sp (?, ?, ?)}");

// Set input parameter values
cs.setInt(1, 5);
cs.setInt(2, 10);

// Register output parameter
cs.registerOutParameter(3, Types.INTEGER);

// Execute stored procedure
cs.execute();

// Retrieve output parameter value
int sum = cs.getInt(3);

// Close connection
cs.close();
conn.close();

System.out.println("Sum of 5 and 10 is " + sum);
Copy after login

} catch (SQLException e) {

e.printStackTrace();
Copy after login

}

In the above code, we first connect to the MySQL database. We then create a CallableStatement that calls our stored procedure and sets the input parameters. Next, we register the type of the sum output parameter and get its value after executing the stored procedure. Finally, we close the connection and print the result.

Conclusion

MySQL stored procedures provide us with many advantages. They can reduce application complexity, improve application performance, and improve data security. Using MySQL stored procedures in Java applications can use the JDBC API, and we can easily write and execute stored procedures.

In short, if we want to learn how to use MySQL stored procedures, we need to understand their basic knowledge and the steps to write and execute stored procedures. These steps can be implemented through a Java program, using the JDBC API.

The above is the detailed content of java mysql stored procedure. 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