Java calls database stored procedures

Source: Internet
Author: User

1. The Prime Minister creates a simple stored procedure for the database: (Here I am the stored procedure for creating an Oracle database)

Create or Replace procedure stu_upd (Nname Varchar2,npassword varchar2) is
Begin
Update student set name=nname where Password=npassword;
End Pl_pro;

Create or Replace procedure Stu_del (NID number) is
Begin
Delete from student where Id=nid;
End Stu_del;


Create or Replace procedure Stu_ins (Nid number,nname Varchar2,npassword varchar2,nage varchar2,nemail varchar2) is
Begin
INSERT into student (Id,name,password,age,email) values (nid,nname,npassword,nage,nemail);
End Stu_ins;

2. Establish a connection to the database through a Java program:

 Packagejdbc;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.util.Properties;ImportOrg.apache.commons.dbcp.BasicDataSource; Public classBasedao {Private StaticBasicdatasource ds=NULL; //private static Connection conn=null;    Private StaticString URL; Private StaticString User; Private StaticString password; Private StaticString drivername; Static{                Try {            //1. Read the configuration file conf.properties, using Java.util.Properties to readProperties pp=NewProperties (); //2. Read and parse the configuration file content through the file stream, the local database with MySQL, so the configuration file MySQL configuration release, other database configuration commentsPp.load (NewFileInputStream ("Src/jdbc.properties")); DriverName=pp.getproperty ("Jdbc.driverclassname");//Get driver nameUrl=pp.getproperty ("Jdbc.url");//gets the URL of the databaseUser=pp.getproperty ("Jdbc.username");//User namePassword=pp.getproperty ("Jdbc.password");//Password            intMaxactive=integer.parseint (Pp.getproperty ("jdbc.maxactive"));//get maximum number of connections            intMaxwait=integer.parseint (Pp.getproperty ("jdbc.maxwait"));//Get maximum wait time//3. Create a connection poolds=NewBasicdatasource (); Ds.setdriverclassname (drivername);//Setting the driver nameDs.seturl (URL);//Set the database addressDs.setusername (user);//Set user nameDs.setpassword (password);//Set PasswordDs.setmaxactive (maxactive);//set maximum number of connectionsDs.setmaxwait (maxwait);//set maximum wait time}Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }        //first load Basicdatasource for database connection     Public StaticConnection getconnection () {Try{System.out.println ("Database connection succeeded"); returnds.getconnection (); } Catch(SQLException e) {//TODO auto-generated Catch blockSYSTEM.OUT.PRINTLN ("Connection database Failed"); return NULL; }            }    //the second is the database connection through the traditional DriverManager     Public StaticConnection GetConnection2 () {Try{class.forname (drivername); Connection Conn=drivermanager.getconnection (Url,user,password); returnConn; } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }                return NULL; }             Public Static voidClose (Connection conn) {if(conn!=NULL){                                                                                   Try{conn.close (); } Catch(Exception e)                                                                          {E.printstacktrace ();                                                                                           } }    }}



3. Once the database connection is established, the stored procedure operations database is called:
 Packagemain;Importjava.sql.CallableStatement;Importjava.sql.Connection;Importjava.sql.SQLException;Importjdbc. Basedao; Public classTest {/**     * @paramargs *@throwsSQLException *@throwsclassnotfoundexception*/     Public Static voidMain (string[] args)throwsSQLException, ClassNotFoundException {//TODO auto-generated Method Stub//to load a database connectionConnection conn=Basedao.getconnection2 (); //create an object for an Oracle stored procedure, call a stored procedure//callablestatement C=conn.preparecall ("{Call Pl_pro (?,?)}"); //c.setstring (1, "Zhang San"); //c.setstring (2, "123456"); //callablestatement C=conn.preparecall ("{Call AAA (?)}"); //C.setlong (1, 2); //create an object for an Oracle stored procedure, call a stored procedureCallableStatement C=conn.preparecall ("{call BBB (?,?,?,?,?)}"); //pass parameters to a stored procedure at onceC.setlong (1, 5); C.setstring (2, "John Doe"); C.setstring (3, "123456"); C.setstring (4, "30"); C.setstring (5, "[Email protected]");    C.execute (); }}

Java calls database stored procedures

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.