Results 1 to 8 of 8
  1. #1
    java_no1 is offline Member
    Join Date
    May 2011
    Location
    Bengaluru
    Posts
    14
    Rep Power
    0

    Default How to work on database?

    Hello every one.
    Actually i started to work on database connection. I am using Microsoft Office Access.
    I have written code and i have done all the connection part also, that means adding that to ODBC.
    Even i am not getting any exception also. but data is not updating in tables.
    please help me out.
    Here is the coding
    import java.sql.*;

    public class DataBase
    {
    DataBase()
    {
    data();
    }

    public void data()
    {
    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("jdbc:odbc:nandan" );

    PreparedStatement ps=con.prepareStatement("insert into Nand values(?,?,?)");
    ps.setString(1,"Apple");
    System.out.println("Inserted column");
    ps.setString(2,"Ball");
    System.out.println("Inserted column");
    ps.executeUpdate();

    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }

    public static void main(String args[])
    {
    new DataBase();
    }
    }

    OUTPUT:
    inserted column
    inserted column



    But It is not inserting to the Table Nand.
    I have created the table properly also in MS Access.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,299
    Blog Entries
    7
    Rep Power
    24

    Default

    Java Code:
    			PreparedStatement ps=con.prepareStatement("insert into Nand values(?,?,?)");
    			ps.setString(1,"Apple");
    			System.out.println("Inserted column");
    			ps.setString(2,"Ball");
    			System.out.println("Inserted column");
    			System.out.println(ps.executeUpdate());
    You didn't supply a value for the third column; also print out the return value of the executeUpdate() method call and see what happens.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    java_no1 is offline Member
    Join Date
    May 2011
    Location
    Bengaluru
    Posts
    14
    Rep Power
    0

    Default

    I removed third column, now i am trying to do for only two columns but still same thing, Table is not getting updates. and the return value of the executeUpdate() method is 1.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,299
    Blog Entries
    7
    Rep Power
    24

    Default

    Quote Originally Posted by java_no1 View Post
    I removed third column, now i am trying to do for only two columns but still same thing, Table is not getting updates. and the return value of the executeUpdate() method is 1.
    A return value 1 indicates that one row was updated (inserted) by the DML statement; maybe the database didn't commit its changes to disk (I know nothing about MS database engines); try a con.commit() at the end.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,873
    Rep Power
    22

    Default

    Quote Originally Posted by JosAH View Post
    Java Code:
    			PreparedStatement ps=con.prepareStatement("insert into Nand values(?,?,?)");
    			ps.setString(1,"Apple");
    			System.out.println("Inserted column");
    			ps.setString(2,"Ball");
    			System.out.println("Inserted column");
    			System.out.println(ps.executeUpdate());
    You didn't supply a value for the third column; also print out the return value of the executeUpdate() method call and see what happens.

    kind regards,

    Jos
    Which should have thrown an exception...which is curious.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,299
    Blog Entries
    7
    Rep Power
    24

    Default

    Quote Originally Posted by Tolls View Post
    Which should have thrown an exception...which is curious.
    I'm afraid we didn't see the actual code (see reply #3). But then again, Microsoft doesn't surprise me anymore ... let's wait what the OP has to say ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,873
    Rep Power
    22

    Default

    I love that.
    "I have a problem in my code. Here is some code that isn't the code that has the problem. Can you tell me what's wrong?"

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,299
    Blog Entries
    7
    Rep Power
    24

    Default

    Quote Originally Posted by Tolls View Post
    I love that.
    "I have a problem in my code. Here is some code that isn't the code that has the problem. Can you tell me what's wrong?"
    Yep, most of the time there is a huge asymmetry between the quality of information peoply supply and the quality of information they want to receive; the want copy/pastable code and all they give is "well, it generated an error somewhere and I don't know what to do, pls can you fix it for me asap"?" ;-) In 'normal' live almost everybody does that and it is accepted but in programming you need to be a gruesome exact bean counter ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Relational Database or Object Database?
    By mattlindsay in forum New To Java
    Replies: 8
    Last Post: 09-24-2011, 06:44 PM
  2. Replies: 1
    Last Post: 03-24-2011, 04:01 AM
  3. Jar file does not work with database (Hibernate)
    By nonabhai in forum NetBeans
    Replies: 9
    Last Post: 05-21-2010, 12:08 PM
  4. Replies: 2
    Last Post: 02-11-2009, 05:43 AM
  5. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 03:58 AM

Posting Permissions

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