How to call a stored procedure with JDBC

Source: Internet
Author: User

 PackageCom.huawei.interview.lym;Importjava.sql.CallableStatement;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.sql.Types; Public classJdbctest {/**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method StubConnection cn=NULL; CallableStatement cstmt=NULL; Try {           //It's better not to do this, because the driver's name is written dead in the program.Class.forName ("Com.mysql.jdbc.Driver"); //in the actual project, the DataSource data is applied here, if the framework is used ,//This data source does not need to be created by our code, we only need datasource ds = Context.lookup ()//cn = Ds.getconnection (); cn= Drivermanager.getconnection ("Jdbc:mysql:///test", "root", "root"); Cstmt= Cn.preparecall ("{Call Insert_student (?,?,?)}"); Cstmt.registeroutparameter (3, Types.integer); Cstmt.setstring (1, "Wangwu"); Cstmt.setint (2, 25);           Cstmt.execute (); //get the first few, different databases are not the same, it is recommended not to writeSystem.out.println (cstmt.getstring (3)); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }       finally       {            /*try{cstmt.close ();} catch (Exception e) {} try{cn.close ();} catch (Exception e) {}*/           Try {              if(Cstmt! =NULL) Cstmt.close (); if(CN! =NULL) Cn.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }       }    }

  

How to call a stored procedure with JDBC

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.