The process of invoking Oracle in Java

Source: Internet
Author: User
Tags command line flush connect stmt stringbuffer
Oracle| Process Example 1:


/*


* This sample shows you to call a Pl/sql stored procedure using the SQL92


* syntax. Also the other sample Plsql.java.


*/





import java.sql.*;


import java.io.*;





class Plsqlexample


{


public static void main (String args [])


throws SQLException, IOException


{


//Load the driver


Drivermanager.registerdriver (New Oracle.jdbc.driver.OracleDriver ());





String url = "jdbc:oracle:oci8:@";


try {


String url1 = System.getproperty ("Jdbc_url");


if (url1!= null)


url = url1;


} catch (Exception e) {


//If There is any security exception ignore it


//and use the default


}





Connect to the database


Connection conn =


drivermanager.getconnection (URL, "Scott", "Tiger");





//Create a statement


Statement stmt = Conn.createstatement ();





//Create the stored function


Stmt.execute ("Create or Replace function raisesal (name CHAR, raise number) return number are begin return raise + 1 00000; end; ");





//Close the statement


Stmt.close ();





//Prepare to call the stored procedure raisesal.


//This sample uses the SQL92 syntax


callablestatement cstmt = Conn.preparecall ("{? = Call Raisesal (?,?)} ");





//Declare that the "the"? is a return value of type Int


cstmt.registeroutparameter (1, Types.integer);





//We want to raise LESLIE ' s salary by 20,000


Cstmt.setstring (2, "LESLIE"); The Name argument is the second?


Cstmt.setint (3, 20000); The raise argument is the third?





//Do the Raise


Cstmt.execute ();





//Get the new salary back


int new_salary = cstmt.getint (1);





System.out.println ("The New salary is:" + new_salary);





//Close the statement


Cstmt.close ();





//Close the connection


Conn.close ();


}


}





Example 2:





/*


* Created on 2004-10-12


*


* TODO To change the template for this generated file go to


* Window-preferences-java-code Style-code Templates


*/





/**


* @author Jackey


*


* TODO to change the template of this generated type comment go to


* Window-preferences-java-code Style-code Templates


*/


/*


* This sample can is used to check the JDBC installation.


* Just run it and provide the connect information. It would select


* "Hello World" from the database.


*/





//You need to import the java.sql package to use JDBC


import java.sql.*;





//We import java.io to is able to read from the command line





import java.io.*;





import oracle.jdbc.OracleTypes;





class Jdbccheckup


{


public static void Main (String args[])


throws SQLException, IOException


{


//Load the Oracle JDBC driver


Drivermanager.registerdriver (New Oracle.jdbc.driver.OracleDriver ());





//Prompt the user for connect information


System.out.println ("Please enter information to test connection to the database");


String User;


String password;


String database;





user = Readentry ("User:");


int slash_index = User.indexof ('/');


if (Slash_index!=-1)


{


password = user.substring (Slash_index + 1);


user = user.substring (0, Slash_index);


}


Else


Password = readentry ("Password:");


database = readentry ("Database (a Tnsname entry):");





System.out.print ("Connecting to the Database ...");


System.out.flush ();





System.out.println ("Connecting ...");


Connection conn = Drivermanager.getconnection


("jdbc:oracle:oci8:@" + database, user, password);


System.out.println ("connected.");





//Create a statement


Statement stmt = Conn.createstatement ();





//Does the SQL "Hello World" thing


ResultSet RSet = stmt.executequery ("Select ' Hello World ' Dual");





while (Rset.next ())


System.out.println (rset.getstring (1));


//Close the result set, the statement and connect


Rset.close ();


Stmt.close ();





System.out.println ("Your JDBC installation is correct.");


//








CallableStatement call = Conn.preparecall (' {call emp_dept_data. OPEN_CV (?,?)} ");


//Find out all of the SALES person


//Call.registeroutparameter (1, oracletypes.cursor);


Call.registeroutparameter (1, oracletypes.cursor);


Call.setint (2, 1);


Call.execute ();


ResultSet rs = (ResultSet) call.getobject (1);





//Dump the cursor


while (Rs.next ())


System.out.println (rs.getstring (1) + "T" + rs.getstring (2) + "T" + rs.getstring (3));





Rs.close ();


Call.close ();





Conn.close ();


}





//Utility function to read a line from standard input


static string Readentry (String prompt)


{


Try


{


StringBuffer buffer = new StringBuffer ();


System.out.print (prompt);


System.out.flush ();


int c = System.in.read ();


while (c!= ' \ n ' && c!=-1)


{


Buffer.append ((char) c);


c = System.in.read ();


}


return buffer.tostring (). Trim ();


}


catch (IOException e)


{


return "";


}


}


}

















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.