[JAVA100 example]058, calling stored procedures

Source: Internet
Author: User

Import java.sql.*;

/**


* <P>TITLE:JDBC Connection Database </p>


* <p>description: This example demonstrates how to use JDBC to connect to an Oracle database and to demonstrate adding data and querying data. </p>


* <p>copyright:copyright (c) 2003</p>


* <p>Filename:JDBCSTMConn.java</p>


* @version 1.0


*/


public class jdbcstmconn{


private static String url= "";


private static String username= "";


private static String password= "";


/**


*<br> Method Description: Obtain data connection


*<br> input Parameters:


*<br> return Type: Connection Connection object


*/


public Connection Conn () {


try {


//load JDBC driver


class.forname ("Oracle.jdbc.driver.OracleDriver");


//CREATE DATABASE connection


Connection con = drivermanager.getconnection (URL, username, password);


return con;


}catch (classnotfoundexception cnf) {


System.out.println ("Driver not find:" +cnf);


return null;


}catch (SQLException sqle) {


System.out.println ("Can´t Connection db:" +sqle);


return null;


} catch (Exception e) {


System.out.println ("Failed to load Jdbc/odbc driver.");


return null;


   }


  }


/**


*<br> Method Description: Invoke the stored procedure to view the results of the data


*<br> input parameters: Connection Con database connection


*<br> input parameter: String SQL SQL statement to execute


*<br> return type:


*/


public void execute (Connection con) {


CallableStatement toesup = null;


try {


Con.setautocommit (FALSE);


//Call stored procedure


toesup = Con.preparecall ("{Call P_test (?)}");


//Pass parameters to stored procedure


Toesup.setint (1, 6);


//Execute stored procedures


Toesup.executequery ();


Statement stmt = Con.createstatement ();


ResultSet rs = (ResultSet) stmt.executequery ("SELECT * from TEST");


while (Rs.next ()) {


String ID = rs.getstring (1);


String NAME = rs.getstring (2);


System.out.println (id+ "" +name);


    }


Rs.close ();


} catch (SQLException e) {


System.out.println (e);


try{


Toesup.close ();


Con.close ();


}catch (Exception es) {System.out.println (es);}


  }


  }


/**


*<br> Method Description: Example Demo


*<br> input Parameters:


*<br> return type:


*/


public void Demo () {


try{


jdbcstmconn oc = new Jdbcstmconn ();


Connection conn = Oc.conn ();


Oc.execute (conn);


Conn.close ();


}catch (SQLException se) {


System.out.println (SE);


}catch (Exception e) {


System.out.println (e);


   }


 


  }


/**


*<br> Method Description: Main method


*<br> input Parameters:


*<br> return type:


*/


public static void Main (string[] arg) {


if (arg.length!=3) {


System.out.println ("Use:java jdbcstmconn URL username password");


return;


   }


Jdbcstmconn oc = new Jdbcstmconn ();


oc.url = arg[0];


Oc.username=arg[1];


oc.password=arg[2];


Oc.demo ();


  }


}

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.