This tutorial will teach you how to create a a stored procedure in JDBC. In the example which will help you to create a stored procedure step by step using java code simple way.
The Simple way to create the stored procedure.you can use the following code snippet to create a parameterized stored procedure.
String str = "CREATE PROCEDURE authors(p_auth_id varchar(15), OUT p_auth_fname varchar(20), OUT p_auth_lname varchar(20))\n" + "BEGIN\n" + " SELECT auth_fname, auth_lname INTO p_auth_fname, p_auth_lname FROM authors where auth_id = p_auth_id;\n" + "END;";
In the above code snippet we created the stored procedure name authors. it created that accept auth_id as parameter and retrieve the corresponding author information from the database.the retrieved information stored in the OUT parameters.
IN : argument can pass to a stored procedure.
OUT : return value of a stored procedure.
the following source code will explain how to create a stored procedure jdbc.
import java.sql.*;
public class Jdbcc {
Connection con;
PreparedStatement pst;
public Jdbcc()
{
Connect();
}
public void Connect()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/abclibrary", "root","");
String str = "CREATE PROCEDURE authors(p_auth_id varchar(15), OUT p_auth_fname varchar(20), OUT p_auth_lname varchar(20))\n" +
"BEGIN\n" +
" SELECT auth_fname, auth_lname INTO p_auth_fname, p_auth_lname FROM authors where auth_id = p_auth_id;\n" +
"END;";
Statement stmt = con.createStatement();
int k = stmt.executeUpdate(str);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
Jdbcc jk = new Jdbcc();
}
}
What Is the Tesla Pi Phone? Imagine if Tesla, the company that makes famous…
Inventory Management POS systems are now an essential part of modern businesses such as bookshops,…
If you're just beginning to learn Java GUI programming creating an Water System Calculator is a fantastic project for…
GitHub is a powerful tool used by teams and developers around the globe. This guide is…
It's like having a super-smart buddy that is always there to help you write stories,…
The UK is known for its rich history, diverse culture, and most of all its…