Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: java SQL problem

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java SQL problem

    Connection connection = null;
            try {
                connection = DriverManager.getConnection("jdbc:derby://localhost:1527/database","root","root");
                connection.setAutoCommit(false);
                PreparedStatement query = connection.prepareStatement("INSERT INTO table (number) VALUES (?)");
                query.setInt(1, 12);
                int temp = query.executeUpdate();
     
                Sytem.out.println(temp);
     
                ResultSet rs = query.getGeneratedKeys();
     
                while (rs.next()) {
                    System.out.println(rs.getInt(1));
                }
     
                connection.commit();
            } catch (SQLException ex) {
                if(connection != null){
                    try {
                        connection.rollback();
                    } catch (SQLException ex1) {
                        Logger.getLogger("meh").log(Level.SEVERE, null, ex1);
                    }
                }
                Logger.getLogger("meh").log(Level.SEVERE, null, ex);
            }finally{
                try {
                    if(connection != null)
                        connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger("meh").log(Level.SEVERE, null, ex);
                }
            }

    Why am I getting NullPointerException error on line with while? Data are inserted correctly I can check that but am still getting error on line with while.

    Edit: Oh sure and data am trying to get should be id generated by DB.
    Last edited by Slapy; March 22nd, 2014 at 04:52 PM. Reason: forgot some information


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: java SQL problem

    getting NullPointerException error on line with while
    while (rs.next()) {
    What value does the rs variable have?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java SQL problem

    System.out.println(rs); is getting back null but by my opinion it should get back that id or am I wrong?

    god am so dumb. Problem was in line PreparedStatement query = connection.prepareStatement("INSERT INTO table (number) VALUES (?)"); It should be connection.prepareStatement("INSERT INTO item (weight, storedays, dangerous) VALUES (?,?,?)",Statement.RETURN_GENERATED_KEYS); now I hate myself.
    Last edited by Slapy; March 22nd, 2014 at 05:11 PM. Reason: found reason

Similar Threads

  1. SQL statement is not executed! java.sql.SQLException: General error
    By ppz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 20th, 2014, 05:36 PM
  2. problem incrementing field in sql with java
    By nachos in forum JDBC & Databases
    Replies: 0
    Last Post: May 6th, 2013, 09:42 AM
  3. [SOLVED] How to modify core Java interface java.sql.Statement.execute(String sql)?
    By amughost in forum Java Theory & Questions
    Replies: 6
    Last Post: June 9th, 2012, 04:31 PM
  4. sql date problem
    By realosso in forum JDBC & Databases
    Replies: 2
    Last Post: June 3rd, 2010, 08:32 AM
  5. Problem of getting result than SQL to JTable
    By MS_Dark in forum Exceptions
    Replies: 1
    Last Post: March 10th, 2009, 06:26 AM