Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Workwithjava

377 views

Published on

oracle foreign key primary key constraints performance tuning MTS IOT 9i block size backup rman corrupted column drop rename recovery controlfile backup clone architecture database archives export dump dmp duplicate rows extents segments fragmentation hot cold blobs migration tablespace locally managed redo undo new features rollback ora-1555 shrink free space user password link TNS tnsnames.ora listener java shutdown sequence

  • Be the first to comment

  • Be the first to like this

Workwithjava

  1. 1. How can I work with Java in the Database? Administration TipsHow can I work with Java in the Database?You can start by using Java Stored Procedures. Here, I’ll show you how you can make Javado the absolute classic job of updating the salary field for a specified employee in the EMPtable. It’s a useless thing to do in real life: Java is a complex language, and makes doingvery simple things (like ‘update emp set sal=…’) incredibly hard work –and you’d be a mugto do it in real life. But Java’s complexities also mean that doing very complex things aremuch easier than trying to do it with PL/SQL or SQL alone.I’m not taking you down the very-complex-made-easy path (partly because I think we’llboth get lost!). But at least this simple example will give you an idea of how to start toget Java working for you.First, copy this script exactly into a text editor (notepad or vi, for example), and save it ina file called Test.java (and yes, there’s a capital “T” and a lower case “j” there, and bothare significant). Save it in your Oracle_Home directory (it doesn’t have to be saved there,but it makes things a bit easier later on).import java.sql.*;public class Test { public static void updateSal(int empid, int salary) { try { Connection connection = DriverManager.getConnection("jdbc:oracle:kprb:"); Statement statement = connection.createStatement(); int norows = statement.executeUpdate("UPDATE emp SET sal = "+salary+"WHERE empno = "+empid); connection.commit(); } catch (SQLException e) { System.out.println(e); } }}A word to the wise: that statement is case sensitive like you wouldn’t believe! When Iinadvertently tried it with its line 7 reading “…CreateStatement();” the second stageproduced errors complaining that “CreateStatement” was not a method it was aware of.Quite right too: it knows about ‘createStatement’ with the lower case “c”! If you can’tmanage to type all that in exactly, therefore, you’ll find a working, tested version on the“Scripts” page of this website.Second, in your Oracle_Home directory (wherever that might be) type the followingcommand:loadjava –u scott/tiger@SID –r Test.javaThe “SID” there should obviously be replaced with the actual name of the Instance inwhich you wish to run this Java Stored Procedure. That must relate to an entry in yourtnsnames.ora file.Copyright © Howard Rogers 2001 1/11/2001 Page 1 of 2
  2. 2. How can I work with Java in the Database? Administration TipsIf you haven’t got tnsnames.ora configured, then you can replace it with the usualhost:port:sid connection string (where host is the name of your machine, and port Is theport on which the Listener is listening for connections to the Instance named by SID), butyou also need to add in a new parameter, like this:loadjava –thin –u scott/tiger@host:port:sid –r Test.javaNote also the precise capitalisation of the ‘Test.java’ name at the end of either of thesevariants of the command.What you are doing at this point is loading the text file we produced earlier into thedatabase as a Java Stored Procedure.Third, you connect to the database as Scott, and create a procedure that calls the newJava Stored Procedure we’ve just created. You do that because JSPs can’t be calleddirectly, but only via PL/SQL. In other words:connect scott/tiger@SIDcreate or replace procedure updatesal (“empid” in number, “salary”in number)as language javaname ‘Test.updateSal(int,int)’;/Again, capitalisation is important in that last line.Fourth and finally, it’s time to actually run the procedure. In SQL Plus, type:exec updatesal(7934,1000)Notice how this works –you call the PL/SQL procedure, supplying the relevant parameters(in this case, employee number and new salary figure). The PL/SQL procedure then callsthe Java Stored Procedure we earlier loaded into the database, passing it the suppliedparameter values in the process, and it is the Java code which then does the actualupdating and commiting of the requested update.If you follow all that with a normal select * from emp; you should see that employee7934 now has a $1000 salary.And that’s how you do it, more or less. Create Java procedures in a text editor, createJava Stored Procedures within the database by loading the code with the ‘loadjava’application, and then create a PL/SQL procedure which actually calls the Java procedure.Hopefully, that will give you enough to play around with, and see what else you can do!Copyright © Howard Rogers 2001 1/11/2001 Page 2 of 2

×