SQL: inserting new rows using ResultSet

Hi,
I have the following question - how does one properly insert rows into a database table, using an updatable ResultSet? There doesn't seem to be much to it, but the following code/result has me puzzled:
1. My MS SQL table:
create table dogs
age integer
2. Java code
public class TestSQL {
public static void main (String args[]) {
try{
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String url = "jdbc:odbc:bolit";
String usr = "bolit";
String pwd = "bolit";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, usr, pwd);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery("Select * from dogs");
rs.moveToInsertRow();
rs.updateInt("age",10);
rs.insertRow();
} catch(Exception e) {
System.out.println("TestSQL.main: "+e);
3. Resulting exception:
TestSQL.main: java.lang.NullPointerException
I would have maybe expected some SQL exception, but this I don't understand...
I'm in advance very grateful for any assistance.
By the way, how can one format code in posts?

to format posts, see:
http://forum.java.sun.com/faq.jsp#messageformat
I'd try putting in some printouts to see what's null:Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, usr, pwd);
//PRINT OUT "con" HERE
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
//PRINT OUT "stmt" HERE
rs = stmt.executeQuery("Select * from dogs");
//PRINT OUT "rs" HERE
rs.moveToInsertRow(); My guess is that your url is incorrect, so con is null, but I'm not a betting man, so I won't put any money on it ;-).

Similar Messages

  • Numbers 09, insert new row using tab

    can I use the tab function to enter a new row when i reach the end of a row?

    Hi Kangolman,
    You are not using Numbers '09. You are using Numbers 3 and that feature has gone. Numbers '09 (Numbers 2.3) may still be on your computer. Look in a folder called iWork '09 inside your Applications folder.
    Wayne Contello has written a User Tip for working with the old and new versions of iWork.
    https://discussions.apple.com/docs/DOC-6991
    The trick is to drag icons for both versions to your Dock. Right click or control click on each and Options > Keep in Dock. That puts you in charge of which version you launch.
    Instead of double clicking the document, run whichever version of Numbers/Pages/Keynote suits and use File > Open.
    Regards,
    Ian.

  • Insert new row to master - detail form using bindingsource

    Hi all,
    I have a form to analyse price of product, the form include master (textbox:slip ID,datetimepicker : date) and detail(datagridview:slipID,productID,price..)
    When I insert new row as below code, all inserted to DB but not show on form, I must close and reopen the form the new row will show, how i must to do to fix it 
    please help, thanks
    Select data when form load 
    ds = New DataSet
    damaster = New SqlDataAdapter("select soPTG,ngaybd,ngaykt,khoa = case when sttesign =0 then 'True' else 'False' end from tbl_PTGMaster", conn)
    damaster.Fill(ds, "tbl_ptgMaster")
    dadet = New SqlDataAdapter("select [SoPTG],[Mahang],[gianhap],[thuesuat],[laigop],[giacu],[giamoi] from tbl_PTGDetail", conn)
    dadet.Fill(ds, "tbl_ptgdetail")
    ds.Relations.Add("PTG_rel", ds.Tables("tbl_ptgmaster").Columns("SoPTG"), ds.Tables("tbl_ptgdetail").Columns("SoPTG")).ChildKeyConstraint.UpdateRule = Rule.Cascade
    BindingMaster.DataSource = ds
    BindingMaster.DataMember = "tbl_ptgmaster"
    BindingDetail.DataSource = BindingMaster
    BindingDetail.DataMember = "PTG_rel"
    gridchitiet.DataMember = "tbl_ptgdetail"
    txtptgnumb.DataBindings.Add("text", BindingMaster, "soPTG")
    dtstart.DataBindings.Add("Text", BindingMaster, "ngaybd")
    dtend.DataBindings.Add("Text", BindingMaster, "ngaykt")
    cmdlock.DataBinding.Add("Enabled", BindingMaster, "khoa", True, DataSourceUpdateMode.OnPropertyChanged)
    setStatus()
    SetHeader()
    Dim cmdinsert As New SqlCommand("insert into tbl_PTGMaster(SoPTG,ngaybd,ngaykt) values(@SoPTG,@ngaybd,@ngaykt)", conn)
    cmdinsert.Parameters.Add("@soPTG", SqlDbType.Char, 10).Value = txtptgnumb.Text
    cmdinsert.Parameters.Add("@ngaybd", SqlDbType.DateTime).Value = dtstart.Value
    cmdinsert.Parameters.Add("@ngaykt", SqlDbType.DateTime).Value = dtend.Value
    damaster.InsertCommand = cmdinsert
    damaster.FillSchema(ds, SchemaType.Source)
    cmdinsert = New SqlCommand("insert into tbl_PTGDetail(SoPTG,mahang,gianhap) values(@SoPTG,@mahang,@gianhap)", conn)
    cmdinsert.Parameters.Add("@SoPTG", SqlDbType.Char, 10).Value = txtptgnumb.Text
    cmdinsert.Parameters("@SoPTG").Direction = ParameterDirection.InputOutput
    cmdinsert.Parameters.Add("@mahang", SqlDbType.Char, 10, "mahang")
    cmdinsert.Parameters.Add("@gianhap", SqlDbType.Decimal, 8, "gianhap")
    dadet.InsertCommand = cmdinsert
    dadet.FillSchema(ds, SchemaType.Source)
    gridchitiet.FinishEditing()
    BindingMaster.EndEdit()
    damaster.Update(ds, "tbl_PTGMaster")
    ds.Tables("tbl_PTGMaster").AcceptChanges()
    BindingDetail.EndEdit()
    dadet.Update(ds, "tbl_PTGDetail")
    ds.Tables("tbl_PTGDetail").AcceptChanges()
    BindingMaster.ResetBindings(False)
    BindingDetail.ResetBindings(False)

    Hello,
    The base idea here is if the record is added successfully you would then get the new primary key then manually add the record to the underlying data source. Does not matter if we are talking a single table or a master-detail setup.
    The following shows the basics
    https://code.msdn.microsoft.com/Adding-new-records-into-bff5eaaf 
    All of the above is done without using a DataSet yet the same thing needs to happen with a DataSet, simply focus on the one table.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

  • ODI - SCD Type 2 - Insert new row error

    Hi All,
    For Dimension I have a surrogate key, a natural key, and a column with "overwrite on change", start_date, end_date, current_record_ind. When I run the interface with the default SCD Type 2 for SQl server, this runs fine. But when I change that one column from "overwrite on change" to "insert new row", it fails on the update step. What should I be looking for anf fixing.
    Thanks for your time and help.

    DB: SQL Server 2008.
    IKM = IKM MSSQL Slowly Changing Dimension
    Error Message: ODI-1228 - Incorrect syntax near the keyword from.
    Code: update T
    set
    from database.dbo.Dim_type as T,
    database.dbo.I$_Dim_type as S
    where T.Cd = S.Cd
    and T.Current_rec_ind = 1
    and IND_UPDATE = 'U'
    To overcome the issue, I have commented the update code in the Knowledge module and the insert works, but for this one it is ok, but I have requirements where one column needs to be overwritten and other column changes will require to add a new record. How to handle both?
    Thanks for your time.

  • Inserting multiple rows using a single Insert statement without using dual

    Hi all,
    i am trying to insert multiple rows using a single insert statement like the below one.
    The below one works fine..
    But is there any other change that can be done in the below one without using dual...
    insert all
    into ps_hd_samp (num1,num2) values (1,1)
    into ps_hd_samp (num1,num2) values (2,2)
    into ps_hd_samp (num1,num2) values (3,3)
    select 1 from dual;

    NiranjanSe wrote:
    Hi all,
    i am trying to insert multiple rows using a single insert statement like the below one.
    The below one works fine..
    But is there any other change that can be done in the below one without using dual...
    insert all
    into ps_hd_samp (num1,num2) values (1,1)
    into ps_hd_samp (num1,num2) values (2,2)
    into ps_hd_samp (num1,num2) values (3,3)
    select 1 from dual;
    SQL> create table ps_hd_samp (num1 number,num2 number);
    Table created.
    SQL> insert all
      2  into ps_hd_samp (num1,num2) values (1,1)
      3  into ps_hd_samp (num1,num2) values (2,2)
      4  into ps_hd_samp (num1,num2) values (3,3)
      5  select count(*)
      6  from ps_hd_samp;
    3 rows created.
    SQL> select * from ps_hd_samp;
          NUM1       NUM2
             1          1
             2          2
             3          3

  • Min and Max and insert new rows

    Hello,
    I am really a beginner in Excel (2007). I am not sure if this is the correct forum, anyway.
    I have grouped some rows in a sheet (rows 1, 2 and 3, columns A and B, table below), some of the columns have numeric values in that group and I have a row (inside that group) that shows the maximum and minimum values (row 1):
              A     B 
    +  1   43   12 <- MAX and MIN values for row 2 and 3 columns A and B
    +  2   34   12
    +  3   43   34
    I am using MAX and MIN functions: A1 = MAX($A2:$A$3), B1 = MIN($B$2:$B$3)
    The problem comes when I want to insert more rows after row 3, the formula keeps wrapping to A$2:$A$3 therefore I need to readjust the formula to: MAX($A2:$A$4) manually (for MIN is the same).
              A     B 
    +  1   43   12 <- Are the same, they should be
    "98 and  10"
    +  2   34   12
    +  3   43   34
    +  4   98   10 <- New row, but the maximum and minimum continues to be the same, I want to extend the formula automatically to the 4 row when I insert the new row.
    Question:
    How can I extend the function of the MAX and MIN formula automatically when I insert new rows?
    Please consider that I may insert rows at the beginning, in the middle or the end.
    Thanks,
    Enrique.
    Kikeman Electric Systems Engineer

    Hi Enrique,
    Thanks for posting in MSDN.
    Based on the description, you want to modify the formula for the A1 and B1 cell when you insert a new rows.
    Yes, we can use Worksheet.Change to dermin the change of data on the worksheet then we can modify the formula as we wanted. Here is a sample for your reference:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Me.Range("A1").Formula = "=MAX($A2:$A" & Me.UsedRange.Rows.Count & ")"
    Me.Range("B1").Formula = "=MIN($B2:$B" & Me.UsedRange.Rows.Count & ")"
    End Sub
    You can more detail about Excel VBA developing from link below:
    Getting Started with VBA in Excel 2010
    Welcome to the Excel 2013 developer reference
    Regards & Fei
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • How to insert new row in MIGO using badi.

    hi,
    Transaction code: MIGO. i'm using badi "MB_MIGO_BADI" and method "LINE_MODIFY", i want to insert N number of item lines when user entered any production order no. and press enter.
    Notes: production order has only one item line with qty N.
    regards,
    Shahid Rais

    Such a feature is not available, but you could add a key event handler which works its magic when it detects a press on the down arrow key, while the last row is selected. Then you can perform the code to insert a new row.

  • I can't insert new row in database when there is a secondary key.

    hi experts. I am using Jdeveloper since a while. I would like to know If there is a way to insert a new row with secondary key attribute in database. As example if we have a sessionEJBLocal data control can't we add new row by using single selection choice item for secondary key attributes? I tried to use single selection even jdbc with sql queries in a managed bean but nothing. Can you help me please?

    would this solve your problem
    http://kohlivikram.blogspot.com/2008/10/add-new-row-in-adf-table-on-button.html

  • How come Merge always Insert new row at the bottom?

    Hi, 
    I came across an issue when I was using Merge to update a table A in my database. I used a stored procedure which takes a table variable, say B, as a parameter. A and B has the same structure. I would like A always to be updated with B. Merge seems to be
    a perfect choice to do this task. 
    I created a datatable in code, and passed this datatable to the stored procedure. It works well. However, any rows in B that don't exist in A are always inserted at the bottom of table A. How can I possibly let the new row be inserted into the middle of
    the table?

    How can I update A to be exactly like B? If I use Merge to do insertion, I get
    Table A:
    Tom 23 Male
    Jerry 20 Male
    Linda 39 Female
    Sam 30 Male
    Yes, this is exactly the same as Table B. Tables are sets, that they are unordered objects. If you want to see data, you need to apply an ORDER BY clause.
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Inserting new row in Table in 11.1.2 from 11.1.1.3 not rendering table!!!!!

    Hey Guys, my status might say noob but i am well seasoned in ADF. We migrated from 11.1.1.3 to the new JDEV 11.1.2. We are doing a test run before we commit to it.
    I noticed the following:
    We have master detail forms that work flawlessly in 11.1.1.3 when we ran the app under 11.1.2 we noticed that any time you try to add a new row to a table the table does not render. The server logs show the action going through and the count goes up one but the table is not rendered anymore it disappears!!!!!!!!!!!!!!
    Now all of this was imported from the 11.1.1.3. all of our tables that have createInsert actions on them are doing this.
    If i drag the tables again and set them up from scratch it seems to work fine. Our issue is we have a good 300 of these tables in different panels and panel collections. we have to do all of that work all over again.
    No errors show in the log. IS THIS A BUG???????????????
    UPDATE: REFRESHING THE PAGE using the Browser Refresh Button Will show the new ROW....... THIS IS NOT GOOOD!!!
    Edited by: user8333408 on Jul 7, 2011 9:23 AM
    Edited by: user8333408 on Jul 7, 2011 9:24 AM
    Edited by: user8333408 on Jul 7, 2011 10:08 AM

    Thanks for the reply.
    The Project was in version 11.1.3 before I migrated. I had the issue above so I migrated to 11.1.1.4 then to 11.1.1.5 then to the New GREAT 11.1.2 to go by the matrix of support The issue is still there.
    Basic Page layout:
    SEARCH-MASTER-PANELTABBED LAYOUT (Hold all the CHILDREN RECORDS)
    PANEL TAB is made of ; SHOW DETAIL ITEM- PANEL COLLECTION- TABLE.
    the panel collection has a toolbar with 2 buttons in it.
    My issue varies:
    Hit create insert==> record count goes up one but the table does not render (it goes all blank even if records existed in it)
    Hit undo budo ==>table still not rendering. (click next on master record then click previous to come back, the table renders correctly.)
    other tables do the following:
    Hit Create insert == > table shows normally and I can enter new record.
    Hit undo ==> table count goes down but table does not render at all anymore.
    for this issue i noticed if i set cache results to false the table works after hitting undo
    IF I CREATE A NEW SHOW DETAIL ITEM and put exactly what the other tabs have in them and use the same layouts and buttons the new table works great!!!!!!!!!!!!???????????????????? WTF?!?!?!?!
    NO ERROR MESSAGES even at finest level are thrown.
    I HAD NO ISSUES IN THE PREVIOUS VERSIONS OF JDEV IS THIS A JSF 2.0 BUG????
    BY THE WAY JDEV 11.1.2 sometimes CRASHES or Hangs when viewing the Binding of Page when you click on Binding straight from the Design view. I have to open the Binding Page separate to view it. FYI
    Edited by: Nottallah on Jul 11, 2011 9:48 AM
    Edited by: Nottallah on Jul 11, 2011 9:51 AM

  • Insert new row in table

    hi,
    im using aqualogic dsp2.5 . i have a requirement wher ei have to insert a new row in the table through data services. i tried the method executeUpdate() but i got an error saying this function is not supported. is there any method to insert a new row in a table through data services.
    thank you in advance,
    rahav.

    http://e-docs.bea.com/aldsp/docs21/appdev/ejbclt.html#wp1083367
    queryPlansDataServices.customer.CUSTOMERDocument customerDocument= queryPlansDataServices.customer.CUSTOMERDocument.Factory.newInstance();
    queryPlansDataServices.customer.CUSTOMERDocument.CUSTOMER customer=customerDocument.addNewCUSTOMER();
    customer.setLASTNAME("KAY_99");
    customer.setFIRSTNAME("JOHN_99");
    customer.setCUSTOMERID("CUSTOMER_99");
    customer.setCITY("SAN JOSE");
    customer.setCUSTOMERSINCE(Calendar.getInstance());
    customer.setEMAILADDRESS("[email protected]");
    customer.setTELEPHONENUMBER(new BigInteger("4085708612"));
    customer.setZIPCODE(new BigInteger("95131"));
    customer.setSTATE("CA");
    customer.setSTREETADDRESS1("2413 N First St");
    customerDs.submit( customerDocument );

  • Insert new row into oracle databse through application module usng ADF Form

    I am using Jdeveloper 11.1.2. I am a beginner. I had tried in-built createInsert and commit method to add new row into oracle DB. For that, I had created ViewObject, VOImpl and AMImpl. Then, I created to jsf page. On first page, I dragged and dropped createInsert fn and VO(as ADF-readonly table) from DC Palette. and on second page, I dragged and dropped my VO(as ADF-Form) and commit (as Button) from DC palette. I created task-flow for it. when i click on createInsert button, i redirected to second page. where i filled all the required information on form. After filling, when i clicked commit button, i redirected to first page.(coz i created task flow to return first page after commit). New row is inserted into ADF-table, but when i checked my oracle DB, there was nothing added.
    Thus, I want to use custom method to insert row into OracleDB. So how can i get this? what are the steps required to do this? what programming codes required in Bean.java and AMImpl.java. Or is there any other steps to do this?
    please provide me detail steps and programming code for bean, amimpl or for VOImpl or EOImpl.
    please help me.

    User,
    To insert a row into the db you have to use a VO which is based on an EO (entity object). There is no java class (or impl) needed. You can all do this decoratively.
    All you have to think of is the PK of the new row. You can use Groovy for that (http://tompeez.wordpress.com/2011/09/02/using-groovy-expression-to-set-a-primary-key-with-a-sequence-number/)
    The rest is like you described it yourself. Drag the Vo onto the page, drop it as read only table, drag the createInsert onto the page as button and set up the navigation. On the second page drag the same VO onto the page and drop it as ADF Form. drag the commit operation onto the page as button and setup the navigation back to the first page.
    Timo

  • Can't insert new row in last position in row set

    I am trying to programmatically insert a new row into the last slot of a View Object's default row set.
    I normally can do this:
    Row newRow = vo.createRow();
    // set stuff on the row
    vo.last();
    vo.next();
    vo.insert(newRow);and, voila, the row is in the last position.
    But on this particualr view object, when I do the above, the row is inserted in the first slot.
    There doesn't seem to be anything unusual about the View object. it is a child Vo in the data model, but that shouldn't matter.
    When I debug with vo.getCurrentRowSlot(), even after performing last(); next();, vo.getCurrentRowSlot() returns 0, never 3.
    Has anyone else run into this behavior and if so, any resolution or explanation?
    FYI, I was able to use the following workaround:
    Row lastRow = vo.last();
    if (lastRow != null){
        int indx = vo.getRangeIndexOf(lastRow) + 1;
        vo.insertRowAtRangeIndex(indx, newRow);
    } else {
       vo.insertRow(newRow);
    }   

    would this solve your problem
    http://kohlivikram.blogspot.com/2008/10/add-new-row-in-adf-table-on-button.html

  • Split Oracle 11g Table Partition and Insert new rows

    Hi,
    I have a doubt that i would like to be clarified. I need to split a partition of a table with more than 800 Million records, but at the same time (during the split operation) i need to insert new records (not in the partition that is being splited). Is there any problem/risk doing this? Is the insert ETL process going to fail ?
    Thanks in advance
    Greetings.
    Nuno
    Edited by: user13512889 on 1/Set/2011 3:25

    Hi Enrique,
    Thanks for posting in MSDN.
    Based on the description, you want to modify the formula for the A1 and B1 cell when you insert a new rows.
    Yes, we can use Worksheet.Change to dermin the change of data on the worksheet then we can modify the formula as we wanted. Here is a sample for your reference:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Me.Range("A1").Formula = "=MAX($A2:$A" & Me.UsedRange.Rows.Count & ")"
    Me.Range("B1").Formula = "=MIN($B2:$B" & Me.UsedRange.Rows.Count & ")"
    End Sub
    You can more detail about Excel VBA developing from link below:
    Getting Started with VBA in Excel 2010
    Welcome to the Excel 2013 developer reference
    Regards & Fei
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • OBIEE Write Back - Insert New Row

    Hello,
    I am new to these forums and looking for some help with OBIEE's Write Back feature.
    I have Write Back working fine with respect to updating existing rows in a table (ie: the UPDATE tag), but was wondering how to force Write back to invoke the INSERT feature. I would like to be able to create new rows in a table to allow my end user to dynamically add and remove information. I have heard of this being possible (perhaps through some sort of temp. table) in other forum posts, but have yet to see how this is implemented. Also, is it at all possible to use Write Back to delete rows in a table? Any help here would be greatly appreciated. Thanks!
    -Mike
    Here is an exerpt from my working XML file:
    <WebMessage name="NEWSNOTESADMIN">
    <XML>
    <writeBack connectionPool="Oracle Data Warehouse Connection Pool">
    <insert>INSERT INTO WC_NEWS_NOTES (CONTENTS, CONTENT_DATE, IMPORTANCE_LVL) VALUES(@{c0},@c{4},@{c2})</insert>
    <update>UPDATE WC_NEWS_NOTES SET CONTENTS='@{c0}', CONTENT_DATE='@{c4}', IMPORTANCE_LVL=@{c2} WHERE CONTENT_ID='@{c1}' </update>
    </writeBack>
    </XML>
    </WebMessage>

    Well first and foremost, Oracle explicitly states that writeback isn't meant to be used to transform OBIEE into a data entry system. Check the other posts on this subject...
    On the "force insert": put the same statement you have for INSERT into the UPDATE tag. Related to the initial point I mentioned: you will have to worry about uniqueness yourself. OBIEE isn't an OLTP platform!
    Re. delete: I liked Stijns recent post, so I'm going to use that as well:
    http://www.justfuckinggoogleit.com/search.pl?query=obiee+delete+through+writeback
    Third hit. Venkats blog.

Maybe you are looking for