The Artima Developer Community
Sponsored Link

Java Answers Forum
PB in db access, URGENT!!!

2 replies on 1 page. Most recent reply: Apr 24, 2004 3:30 AM by Nawal Nafisse

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Nawal Nafisse

Posts: 2
Nickname: nawal
Registered: Apr, 2004

PB in db access, URGENT!!! Posted: Apr 12, 2004 9:30 AM
Reply to this message Reply
Advertisement
Hello every body,

I am a new member in this forum.
I have just one question.

I am using oracle8.i and i insert data in this db. The pb is that in a table, i use a trigger which controle automatically primary key of this table.After doing an insertion in this table, i want to have the value of Primary key.

For more details, this is the code below :

public IDENTIFICATION(FicheSociete fic){

setCode_sicovam(fic.getIdentification().getCodeSicovam());
setCode_isin(fic.getIdentification().getCodeISIN());
setCode_fininfo(fic.getIdentification().getCodeFininfo());
setMnemonique(fic.getIdentification().getMnemonique());
setCode_place_cotation(fic.getIdentification().getCodePlaceCotation());
setLibelle30(fic.getIdentification().getLibelle30());
setLibelle18(fic.getIdentification().getLibelle18());
}

public long insertIdentification(FicheSociete fic){


int result=-1;

IDENTIFICATION id;

java.sql.Connection con = null;
java.sql.PreparedStatement cstmt = null;
try {
con=importdbinfo.getOraConnection();

if (con != null)
{

String sql="INSERT INTO IDENTIFICATION (ID,CODE_SICOVAM, CODE_ISIN, CODE_FININFO, MNEMONIQUE, CODE_PLACE_COTATION, LIBELLE30, LIBELLE18) "
+" VALUES(?,?,?,?,?,?,?,?)";
System.err.println(sql);
cstmt =con.prepareStatement(sql);

cstmt.setLong(1,0);
cstmt.setString(2,fic.getIdentification().getCodeSicovam());
cstmt.setString(3,fic.getIdentification().getCodeISIN());
cstmt.setString(4,fic.getIdentification().getCodeFininfo());
cstmt.setString(5,fic.getIdentification().getMnemonique());
cstmt.setString(6,fic.getIdentification().getCodePlaceCotation());
cstmt.setString(7,fic.getIdentification().getLibelle30());
cstmt.setString(8,fic.getIdentification().getLibelle18());

result =cstmt.executeUpdate();
con.commit();


}


else
System.err.println("Can't get database connection ");
}
catch(SQLException e) {
System.err.println(e);
}
catch(Exception e) {
System.err.println(e);
}
finally {
try { if(cstmt != null) {cstmt.close(); } } catch(SQLException e) {System.err.println(e);}
try { if(con != null) {con.close(); } } catch(SQLException e) {System.err.println(e);}
}

id = new IDENTIFICATION(fic);
long identification_id = id.getId();

return identification_id;


}
--------
the pb is the result of identification_id is always "0". I want to have the real value of identification_id which is conform to the code_sicovam, ....

I am building a constructor which takes those values and i am trying to extract the value by using .get_id().

If this is logic or not? If there are any another way, please assign it to me. Cest urgent!!!!!!!!!


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: PB in db access, URGENT!!! Posted: Apr 12, 2004 7:36 PM
Reply to this message Reply
I am not 100% sure what you are looking for by reading your post. My best guess is that you are inserting a record in identification table. Its primary key "id" is generated by trigger. after inserting the record you want to have the newly generated primary key for that table. If I guesed correctly then here is the solution. I am copying code for insert() method that inserts an email in Email table. The email_id is its primary key and it is generated by a trigger. After insert we get email_id in the program. This is a working code. You can reuse to work for your case.

  public void insert(Connection connection) throws SQLException {
 
    String query = "begin " +
        "insert into email " +
        "(email_id, person_id, email_address, comments, inactive_date)" +
        "values" +
        "(null, ?, ?, ?, ?) returning email_id into ? ; " +
        " end;";
 
    // Prepare a statement
    CallableStatement statement = connection.prepareCall(query);
    statement.setLong(1, this.personId);
    statement.setString(2, this.emailAddress);
    statement.setString(3, this.comments);
    statement.setDate(4, this.inactiveDate);
    statement.registerOutParameter(5, Types.NUMERIC);
 
    int rowsUpdated = statement.executeUpdate();
 
    if (rowsUpdated != 1) {
      // Give customized message as well as wrap the SQLException as its cause
      SQLException e = new SQLException("Insert failed. Query:" + query);
      e.initCause(statement.getWarnings());
 
      // Close the statement
      statement.close();
 
      // Now throw the exception
      throw e;
    }
    else {
      // Let us get the newly generated email_id
      this.emailId = statement.getLong(5);
    }
 
    // Close the statement
    statement.close();
 
    // We don;t close connection here. The caller is responsible for closing
    // the connection
  }

Nawal Nafisse

Posts: 2
Nickname: nawal
Registered: Apr, 2004

Re: PB in db access, URGENT!!! Posted: Apr 24, 2004 3:30 AM
Reply to this message Reply
Thakns very much.

This is the solution

A+

Nawal

Flat View: This topic has 2 replies on 1 page
Topic: How to simulate Previous Topic   Next Topic Topic: Changing Directory structure on the server

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use