Friday, November 8, 2019

How To Role Callable Contention Inward Coffee To Telephone Phone Stored Procedure? Jdbc Example

The CallableStatement of JDBC API is used to telephone phone a stored physical care for from Java Program. Calling a stored physical care for follows the same pattern every bit creating PreparedStatment as well as than executing it. You showtime require to practice a database connectedness past times supplying all the relevant details e.g. database URL, which comprise JDBC protocol as well as hostname, username, as well as password. Make certain your JDBC URL is acceptable past times JDBC driver y'all are using to connect to the database. Every database vendor uses unlike JDBC URL as well as they have got unlike driver JAR which must hold upwardly inwards your classpath earlier y'all tin run the stored physical care for from Java Program.

Once y'all are done amongst initial setup, y'all tin obtain CallableStatement from Connection past times calling prepareCall(String SQL) method, where SQL must hold upwardly inwards the format required past times your database vendor e.g. Microsoft SQL Server requires curly braces e.g.
{call Books.BookDetails_Get(?)}.

This stored proc requires an INPUT parameter which must hold upwardly laid past times calling setXXX() method on the CallableStatement object earlier y'all tin execute the query.

Once this is done, simply telephone phone the executeQuery() method of CallableStatement as well as it volition render the ResultSet contains all the rows returned past times this stored proc.

Just loop through ResultSet as well as extract all the rows. You have got successfully run the stored physical care for from Java Program using CallableStatement.

Sounds slow right, good it is indeed slow ane time y'all have got done it once. But, if y'all have got simply starting amongst Java as well as JDBC, sometime it may experience overwhelming because of setting upwardly a database, including JDBD driver into classpath as well as other issues which surface acre connecting to a database. In that case, I advise y'all to showtime become through a comprehensive Java course of report like The Complete Java MasterClass - Covers Java 11, instead of learning bits as well as pieces. Trust me, y'all volition larn a lot inwards a real brusk duration of time.



Steps to telephone phone a stored physical care for from Java


1) Create a database connection.

Connection con = null; try {    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");    String url = "jdbc:sqlserver://localhost:42588;";    con = DriverManager.getConnection(url, "username", "pw"); } grab (Exception e) {    e.printStackTrace(); }


2) Create a SQL String
You require to practice SQL using a String variable to telephone phone the stored procedure, for example, CallableStatement e.g. {call Books.BookDetails_Get(?)}. This is database dependent, for Oracle the format is unlike it starts amongst BEGIN as well as ends amongst ENDS instead of curly braces e.g.

String SQL = "{call Books.BookDetails_Get(?)}" // for Microsoft SQL Server String Oracle = "BEGIN BOOKDETAILS_GET(?); END;";


3) Create CallableStatement Object
You tin practice a CallableStatement past times calling Connection.prepareCall(SQL) method, transcend the SQL created inwards the previous step.

CallableStatement cs = con.prepareCall(SQL);


4)  Provide Input Parameters
You tin laid the input parameter past times calling diverse setXXX() method depending upon the information type of query parameters on the CallableStatement object, similar to PreparedStatment e.g.

cs.setString(1, "982928");

Btw, if y'all are non familiar amongst PreparedStatement as well as thence y'all tin too come across my post, difference betwixt PreparedStatement as well as CallableStatement to larn more.



5)  Call Stored Procedure
You tin execute a stored physical care for on the database past times calling executeQuery() method of CallableStatement class, every bit shown below:

ResultSet rs = cs.executeQuery();

This volition render a ResultSet object which contains rows returned past times your stored procedure.


6) Extract Rows from ResultSet
You tin acquire the information from the ResultSet past times Iterating over ResultSet as well as impress the results or practice Java objects, every bit shown below:

while(rs.next()){   System.out.println(rs.getString(1)); }

This volition impress the showtime column of every row. You should too unopen the ResultSet object ane time y'all are done amongst it.


These were the steps y'all require to follow to utilization the CallableStatement inwards JDBC to execute a stored physical care for on the database side as well as have information on your Java program. If y'all desire to larn to a greater extent than nearly JDBC or desire to start from scratch, I recommend Data Access layer roughly stored procedures, which is a dandy design, as well as thence y'all should have got a skilful agreement of CallableStatement.

They are the ones which are used to run the stored physical care for from Java programs. By encapsulating your information access logic as well as SQL on a stored procedure, allow y'all to modify them easily on SQL editor without making whatever modify on Java side, which agency y'all tin implement novel functionalities without edifice as well as deploying a novel JAR file on Java side.

Further Learning
read)
  • Difference betwixt Type 1 as well as Type iv JDBC drivers (answers)
  • Top 10 JDBC Interview Question for Java programmers (read)
  • 6 Essential JDBC Performance tips for Java Programmers (tips)
  • How to connect to MySQL database from Java Program? (tutorial)
  • JDBC Batch Insert as well as Update illustration using PreparedStatement (tutorial)
  • Difference betwixt java.sql.Date, java.sql.Timestamp, as well as java.util.Date inwards JDBC? (answer)

  • Thanks for reading this tutorial thence far. If y'all similar this JDBC tutorial as well as thence delight percentage amongst your friends as well as colleagues. If y'all have got whatever questions or feedback as well as thence delight drib a note.

    No comments:

    Post a Comment