Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Record to Oracle Table from JBuilder Code

Status
Not open for further replies.

MARTINRODDY

Programmer
Apr 3, 2001
30
US
I have just started using JBuilder - I use QueryDataSet to do queries from an Oracle table. How do I insert rows onto the table?? I am not using a data aware control, the data is coming from a JTable.
 
I'm assuming you already have the Oracle JDBC drivers.

If you have an application server, you can create an entity bean through the wizard: new->enterprise->EJB Entity Bean Modeler. Fill in the connection information, and everything else should be a cakewalk. No manual tweaks are necessary once you're through the wizard.

Then, to add a row, get yourself an instance of the home interface and perform a create:

<code>
import javax.naming.*;
import javax.rmi.PortableRemoteObject;

public insert()
{
//The values we want to put in the DB
String myField1 = &quot;Wee&quot;;
String myField2 = &quot;Hooray&quot;;

//Get an instance of the home interface
Context ctx = new InitialContext();
Object ref = ctx.lookup(&quot;MyClassJNDIName&quot;);
MyHome home = (MyHome)(PortableRemoteObject.narrow(ref, MyHome.class));

//Insert the row
home.create(myField1, myField2);
}

</code>


If you don't have an appserver, you'll have to use regular JDBC, with a simple query like &quot;insert into myTable values(myField1, myField2);&quot;. I haven't done this myself, only in VB with ADODB objects which are very simple to use, so if this is the case someone else will have to help you out.
 
I've tried regular JDBC now. The code to insert is as follows:

import oracle.jdbc.*;
import java.sql.*;

//Which driver to use for connection. I'm assuming an Oracle DB here.
String driverClassName = &quot;oracle.jdbc.OracleDriver&quot;;

//The connection URL.
// Oracle Client Conn. Host Port SID
String url = &quot;jdbc:eek:racle:thin:mad:mydbhost.com:1521:mySID&quot;;

Class.forname(driverClassName);
Connection conn = DriverManager.getConnection(url, &quot;scott&quot;, &quot;tiger&quot;);
Statement statement = conn.getStatement;
statement.executeQuery(&quot;insert into MyTable values('Wee', 'Hooray')&quot;);

Hope it helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top