CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2005
    Location
    マレーシア
    Posts
    155

    [RESOLVED] Get newly inserted row

    I`m using JDBC to connect to MySql. From my java code, I inserd new rows into the data table using INSERT statements. How can I get back the new inserted row? I`m leaving the fields to have their default values when inserting
    Ai nò mai speling sùks, bùt ai du it mainfuli
    http://www.geocities.jp/juuhassai/i_rouseiji.html

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Get newly inserted row

    Hello, you can use the last_insert_id() MySQL function (If you use an autoincrement columns as a PK) to get the PK value of the newly added record.
    Code:
    Select col1,col2 from tablename where id = last_insert_id()
    And read this data from java like this: (I'm not a java expert)

    Code:
    		CallableStatement sql=con.prepareCall("call InsertAndGetValues()");
    		
    		ResultSet result = sql.executeQuery();
    		
    		if(result.first())
    		{
    			int x = result.getInt(1);
    			String s = result.getString(2);
    		}
    InsertAndGetValues is the name of the stored procedure that you use to insert new recored and select its values as above.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured