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