0
if (Baglan() == false) { return false; }
    try{
        String generatedColumns[] = {"ID","TAHSILATNO"};            
        String sSql = "Declare nId number := 0;" + 
                "        nKey number := 0;" + 
                " BEGIN" + 
                "   Select Nvl(Max(Id),0) + 1 INTO nId From bvktahsilatno;" +
                "   INSERT INTO bvktahsilatno (id, yili, tahsilatno ) VALUES( nId, 2018, 53);" + 
                "   EXCEPTION " + 
                "     WHEN DUP_VAL_ON_INDEX THEN" + 
                "          Select Nvl(Max(Id),0) + 1 INTO nId From bvktahsilatno;" + 
                "          Select Nvl(Max(tahsilatno),0) + 1 INTO nKey From bvktahsilatno WHERE Yili = 2018;" +
                "          INSERT INTO bvktahsilatno (id, yili, tahsilatno ) VALUES( nId, 2018, nKey);" + 
                "END;";

        //VtStatement = VtConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        //VtStatement.execute(pSql);
        //PreparedStatement  VtStatement = VtConnection.prepareStatement( sSql, Statement.RETURN_GENERATED_KEYS );
        //PreparedStatement  VtStatement = VtConnection.prepareStatement( sSql, generatedColumns );

        PreparedStatement  VtStatement = VtConnection.prepareStatement( sSql, Statement.RETURN_GENERATED_KEYS);
        System.out.println("oracle cumle : "  + sSql);          
        int nAdet = VtStatement.executeUpdate();
        System.out.println("Hatayok/Adet : " + nAdet);

        ResultSet rs = VtStatement.getGeneratedKeys();

        int id=0;
        while (rs.next()) {
             id = rs.getInt(1);

        }           
        System.out.println("Oracle Inserted ID -" + id); // display inserted record

        return true;
    } catch (SQLException e) {                      
        return false;
    }

I want to ask a question about retrieving created keys on Oracle Db after insert query. My code is as above but generated keys values are not showed on the terminal. How can i get generated column values without creating sequence ? Many form pages say that it is possible as using sequence for id or other column. Also id is primary key, tahsilatno is unique index. I can overcome this problem? Thank you for your helping from now

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Gökhan Aldanmaz
  • 113
  • 1
  • 14
  • Possible duplicate of [How to return rows from a declare/begin/end block in Oracle?](https://stackoverflow.com/questions/3371408/how-to-return-rows-from-a-declare-begin-end-block-in-oracle) – talex Oct 02 '18 at 09:49
  • Actually,Problem is not on query, i want to get result id and tahsilat no values after insert operation on result set or another way. – Gökhan Aldanmaz Oct 02 '18 at 10:03
  • Generated keys retrieval doesn't work when you execute an anonymous block of PL/SQL. You'll need to explicitly return it from you PL/SQL code. – Mark Rotteveel Oct 02 '18 at 11:14

0 Answers0