Using Java to call MySQL database, the encapsulation and invocation of data stored procedure;

Source: Internet
Author: User

1. Encapsulation of stored procedures: Right-click Function--New function--select process (process is no return value) or select function (function has return value)

2. Here we select the process-click Next after entering

The mode has in (input parameters), out (outward output parameters), INOUT, where we usually use in and out, if nothing is written the system defaults to in.

After the function is established, we need to encapsulate the command to execute in the function, for example:

Note: To add a semicolon after writing a statement, add the length after the parameter varchar type.

Instance:

 PackageCom.inba.maya.cunchu;ImportJava.util.*;ImportJava.sql.*; Public classMycunchu { Public Static voidMain (string[] args)throwsexception{Class.forName ("Com.mysql.jdbc.Driver"); Connection Conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/mydb?characterencoding=gbk", "Root", "" "); //Note: When calling the database function, the key word is the call function name must be added (), whether the function is not with parameters, with parameters must write parameters;//called without arguments.//callablestatement Cs=conn.preparecall ("Call TCC ()"); //ResultSet rs=cs.executequery (); //called with parameters.//callablestatement Cs=conn.preparecall ("Call TTA (?,?)"); //Enter two parameters//cs.setstring (1, "k006"); //Cs.setint (2, 90); //ResultSet rs=cs.executequery (); //Output ParametersCallableStatement Cs=conn.preparecall ("Call TDD (?,?)"); //in this case, because I write parameters in the database, the first parameter defined is in, the second argument is out, so the first one is to SetStringCs.setstring (1, "k001"); //because the second one is out, so use Registeroutparameter (position, java.sql.Types. Data type)Cs.registeroutparameter (2, Java.sql.Types.INTEGER); ResultSet RS=Cs.executequery (); //Be sure to complete the query above when you get the parameters        intN=cs.getint (2);  while(Rs.next ()) {System.out.println (n); System.out.println (Rs.getstring (1) +rs.getstring (2) +rs.getint (5) +rs.getdouble (3));    } conn.close (); }}

Call the MySQL database in Java, the encapsulation and invocation of the data stored procedure;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.