Home » Developer & Programmer » JDeveloper, Java & XML » oracle insert statement
oracle insert statement [message #131879] Tue, 09 August 2005 22:02 Go to next message
shuini
Messages: 5
Registered: August 2005
Location: philippines
Junior Member
hello. m new to oracle and java so i hope you guys can help me.

how do you insert into an oracle table with variables? this statement doesn't seem to work

stmt.executeUpdate("INSERT into T_LINKMANAGER(LMID,LMNAME,LMURL,LMFLAG,LMHITS,LMCATEG) VALUES(ct,aname,aurl,'false',0,acat)");

where ct is an int variable and aname,aurl and acat are strings passed from another setter.

all help will be much appreciated


Re: oracle insert statement [message #135281 is a reply to message #131879] Tue, 30 August 2005 14:59 Go to previous message
Art Metzer
Messages: 2480
Registered: December 2002
Senior Member
This code should help get you started.

First, in Oracle as SCOTT:
SQL> CREATE TABLE t_linkmanager (
  2      lmid        NUMBER
  3  ,   lmname      VARCHAR2(10)
  4  ,   lmurl       VARCHAR2(25)
  5  ,   lmflag      VARCHAR2(5)
  6  ,   lmhits      NUMBER
  7  ,   lmcateg     VARCHAR2(10)
  8  )
  9  /
 
Table created.
 
SQL>
Then, the Java class:
public class OrafaqJava131879
{
    public static void performInsert(java.sql.Connection c) throws Exception
    {
        java.sql.PreparedStatement pstm =
            c.prepareStatement("INSERT INTO t_linkmanager"
                             + "(lmid,lmname,lmurl,lmflag,lmhits,lmcateg)"
                             + "VALUES (?,?,?,?,?,?)");

        pstm.setInt(1,12345);
        pstm.setString(2,"Hello");
        pstm.setString(3,"http://www.orafaq.com");
        pstm.setString(4,"false");
        pstm.setInt(5,0);
        pstm.setString(6,"ACTIVE");
        pstm.executeUpdate();
    }
    public static void main(String[] args) throws Exception
    {
        java.sql.Connection conn = null;
        java.sql.DriverManager.registerDriver
            (new oracle.jdbc.driver.OracleDriver());
        conn = java.sql.DriverManager.getConnection
            ("jdbc:oracle:thin:@aria-dev:1521:ora920","scott","tiger");
        conn.setAutoCommit(false);
        performInsert(conn);
        conn.commit();
        conn.close();
    }
}
After running the Java class, back in Oracle as SCOTT:
SQL> SELECT lmid
  2  ,      lmname
  3  ,      lmurl
  4  ,      lmflag
  5  ,      lmhits
  6  ,      lmcateg
  7  FROM   t_linkmanager
  8  /
 
      LMID LMNAME     LMURL                     LMFLA     LMHITS LMCATEG
---------- ---------- ------------------------- ----- ---------- ----------
     12345 Hello      http://www.orafaq.com     false          0 ACTIVE
 
SQL>
Previous Topic: Date Problem
Next Topic: access Oracle database using javascript
Goto Forum:
  


Current Time: Mon Nov 25 10:28:47 CST 2024