Java SQLite ResultSet Return nothing











up vote
0
down vote

favorite












I am try to get data from a database but it's returning nothing when I just inserted something. I don't know what I am doing wrong. I have followed many tutorials but found nothing working.



    sql = "INSERT INTO optionsetvalue (optionsetid, value, text) VALUES ("+id+", 10000, 'Monday')";
executeInsert(sql);

sql = "SELECT * FROM optionsetvalue";
ResultSet rs = execute(sql, true);
try {
while(rs.next())
{
System.out.println(rs.getInt("value"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


and I have this method



protected ResultSet execute(String sql, boolean returnResults)
{
ResultSet results = null;
if(databaseExists)
{
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {

if(returnResults) results = stmt.executeQuery(sql);
else stmt.execute(sql);

} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
return results;
}


What is the mistake I am making? It is definately making it into the stmt.executeQuery method as I have stepped through the code. When I run the rs.Next() it returns false so never gets to the System.out.println.



protected long executeInsert(String sql)
{
try(
Connection conn = DriverManager.getConnection(url);
PreparedStatement statement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
) {

int affectedRows = statement.executeUpdate();

if (affectedRows == 0) {
throw new SQLException("Creating user failed, no rows affected.");
}

try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
if (generatedKeys.next()) {
return generatedKeys.getLong(1);
}
else {
throw new SQLException("Creating user failed, no ID obtained.");
}
}
} catch(SQLException e)
{
System.out.println(e.getMessage());
}
return -1;
}









share|improve this question
























  • A guess: As the insert/select are in to same method then I am guessing that the trnsaction has not been commited at this time. Manually handle commits.
    – Scary Wombat
    Nov 13 at 1:28










  • In the same method? I am executing the Insert before running the select. What do you mean by Manual handle commits?
    – Tom Hanson
    Nov 13 at 1:31






  • 1




    could you post your executeInsert() method as well
    – mangusta
    Nov 13 at 1:33










  • I can not see anywhere that you are doing commits
    – Scary Wombat
    Nov 13 at 1:34















up vote
0
down vote

favorite












I am try to get data from a database but it's returning nothing when I just inserted something. I don't know what I am doing wrong. I have followed many tutorials but found nothing working.



    sql = "INSERT INTO optionsetvalue (optionsetid, value, text) VALUES ("+id+", 10000, 'Monday')";
executeInsert(sql);

sql = "SELECT * FROM optionsetvalue";
ResultSet rs = execute(sql, true);
try {
while(rs.next())
{
System.out.println(rs.getInt("value"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


and I have this method



protected ResultSet execute(String sql, boolean returnResults)
{
ResultSet results = null;
if(databaseExists)
{
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {

if(returnResults) results = stmt.executeQuery(sql);
else stmt.execute(sql);

} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
return results;
}


What is the mistake I am making? It is definately making it into the stmt.executeQuery method as I have stepped through the code. When I run the rs.Next() it returns false so never gets to the System.out.println.



protected long executeInsert(String sql)
{
try(
Connection conn = DriverManager.getConnection(url);
PreparedStatement statement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
) {

int affectedRows = statement.executeUpdate();

if (affectedRows == 0) {
throw new SQLException("Creating user failed, no rows affected.");
}

try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
if (generatedKeys.next()) {
return generatedKeys.getLong(1);
}
else {
throw new SQLException("Creating user failed, no ID obtained.");
}
}
} catch(SQLException e)
{
System.out.println(e.getMessage());
}
return -1;
}









share|improve this question
























  • A guess: As the insert/select are in to same method then I am guessing that the trnsaction has not been commited at this time. Manually handle commits.
    – Scary Wombat
    Nov 13 at 1:28










  • In the same method? I am executing the Insert before running the select. What do you mean by Manual handle commits?
    – Tom Hanson
    Nov 13 at 1:31






  • 1




    could you post your executeInsert() method as well
    – mangusta
    Nov 13 at 1:33










  • I can not see anywhere that you are doing commits
    – Scary Wombat
    Nov 13 at 1:34













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am try to get data from a database but it's returning nothing when I just inserted something. I don't know what I am doing wrong. I have followed many tutorials but found nothing working.



    sql = "INSERT INTO optionsetvalue (optionsetid, value, text) VALUES ("+id+", 10000, 'Monday')";
executeInsert(sql);

sql = "SELECT * FROM optionsetvalue";
ResultSet rs = execute(sql, true);
try {
while(rs.next())
{
System.out.println(rs.getInt("value"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


and I have this method



protected ResultSet execute(String sql, boolean returnResults)
{
ResultSet results = null;
if(databaseExists)
{
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {

if(returnResults) results = stmt.executeQuery(sql);
else stmt.execute(sql);

} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
return results;
}


What is the mistake I am making? It is definately making it into the stmt.executeQuery method as I have stepped through the code. When I run the rs.Next() it returns false so never gets to the System.out.println.



protected long executeInsert(String sql)
{
try(
Connection conn = DriverManager.getConnection(url);
PreparedStatement statement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
) {

int affectedRows = statement.executeUpdate();

if (affectedRows == 0) {
throw new SQLException("Creating user failed, no rows affected.");
}

try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
if (generatedKeys.next()) {
return generatedKeys.getLong(1);
}
else {
throw new SQLException("Creating user failed, no ID obtained.");
}
}
} catch(SQLException e)
{
System.out.println(e.getMessage());
}
return -1;
}









share|improve this question















I am try to get data from a database but it's returning nothing when I just inserted something. I don't know what I am doing wrong. I have followed many tutorials but found nothing working.



    sql = "INSERT INTO optionsetvalue (optionsetid, value, text) VALUES ("+id+", 10000, 'Monday')";
executeInsert(sql);

sql = "SELECT * FROM optionsetvalue";
ResultSet rs = execute(sql, true);
try {
while(rs.next())
{
System.out.println(rs.getInt("value"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


and I have this method



protected ResultSet execute(String sql, boolean returnResults)
{
ResultSet results = null;
if(databaseExists)
{
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {

if(returnResults) results = stmt.executeQuery(sql);
else stmt.execute(sql);

} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
return results;
}


What is the mistake I am making? It is definately making it into the stmt.executeQuery method as I have stepped through the code. When I run the rs.Next() it returns false so never gets to the System.out.println.



protected long executeInsert(String sql)
{
try(
Connection conn = DriverManager.getConnection(url);
PreparedStatement statement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
) {

int affectedRows = statement.executeUpdate();

if (affectedRows == 0) {
throw new SQLException("Creating user failed, no rows affected.");
}

try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
if (generatedKeys.next()) {
return generatedKeys.getLong(1);
}
else {
throw new SQLException("Creating user failed, no ID obtained.");
}
}
} catch(SQLException e)
{
System.out.println(e.getMessage());
}
return -1;
}






java sqlite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 3:49

























asked Nov 13 at 1:22









Tom Hanson

417315




417315












  • A guess: As the insert/select are in to same method then I am guessing that the trnsaction has not been commited at this time. Manually handle commits.
    – Scary Wombat
    Nov 13 at 1:28










  • In the same method? I am executing the Insert before running the select. What do you mean by Manual handle commits?
    – Tom Hanson
    Nov 13 at 1:31






  • 1




    could you post your executeInsert() method as well
    – mangusta
    Nov 13 at 1:33










  • I can not see anywhere that you are doing commits
    – Scary Wombat
    Nov 13 at 1:34


















  • A guess: As the insert/select are in to same method then I am guessing that the trnsaction has not been commited at this time. Manually handle commits.
    – Scary Wombat
    Nov 13 at 1:28










  • In the same method? I am executing the Insert before running the select. What do you mean by Manual handle commits?
    – Tom Hanson
    Nov 13 at 1:31






  • 1




    could you post your executeInsert() method as well
    – mangusta
    Nov 13 at 1:33










  • I can not see anywhere that you are doing commits
    – Scary Wombat
    Nov 13 at 1:34
















A guess: As the insert/select are in to same method then I am guessing that the trnsaction has not been commited at this time. Manually handle commits.
– Scary Wombat
Nov 13 at 1:28




A guess: As the insert/select are in to same method then I am guessing that the trnsaction has not been commited at this time. Manually handle commits.
– Scary Wombat
Nov 13 at 1:28












In the same method? I am executing the Insert before running the select. What do you mean by Manual handle commits?
– Tom Hanson
Nov 13 at 1:31




In the same method? I am executing the Insert before running the select. What do you mean by Manual handle commits?
– Tom Hanson
Nov 13 at 1:31




1




1




could you post your executeInsert() method as well
– mangusta
Nov 13 at 1:33




could you post your executeInsert() method as well
– mangusta
Nov 13 at 1:33












I can not see anywhere that you are doing commits
– Scary Wombat
Nov 13 at 1:34




I can not see anywhere that you are doing commits
– Scary Wombat
Nov 13 at 1:34












1 Answer
1






active

oldest

votes

















up vote
0
down vote













You’re using try-with-resource in your execute method which means your connection and result set are closed when you exit the try {}, you need to extract your data directly from the result set and return that data from the method as a custom class or some collection class.



A very simplified example



if(returnResults) {
results = stmt.executeQuery(sql);
if (results.next()) {
value = results.getInt(“value”);
System.out.println(value);
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272424%2fjava-sqlite-resultset-return-nothing%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You’re using try-with-resource in your execute method which means your connection and result set are closed when you exit the try {}, you need to extract your data directly from the result set and return that data from the method as a custom class or some collection class.



    A very simplified example



    if(returnResults) {
    results = stmt.executeQuery(sql);
    if (results.next()) {
    value = results.getInt(“value”);
    System.out.println(value);
    }
    }





    share|improve this answer



























      up vote
      0
      down vote













      You’re using try-with-resource in your execute method which means your connection and result set are closed when you exit the try {}, you need to extract your data directly from the result set and return that data from the method as a custom class or some collection class.



      A very simplified example



      if(returnResults) {
      results = stmt.executeQuery(sql);
      if (results.next()) {
      value = results.getInt(“value”);
      System.out.println(value);
      }
      }





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        You’re using try-with-resource in your execute method which means your connection and result set are closed when you exit the try {}, you need to extract your data directly from the result set and return that data from the method as a custom class or some collection class.



        A very simplified example



        if(returnResults) {
        results = stmt.executeQuery(sql);
        if (results.next()) {
        value = results.getInt(“value”);
        System.out.println(value);
        }
        }





        share|improve this answer














        You’re using try-with-resource in your execute method which means your connection and result set are closed when you exit the try {}, you need to extract your data directly from the result set and return that data from the method as a custom class or some collection class.



        A very simplified example



        if(returnResults) {
        results = stmt.executeQuery(sql);
        if (results.next()) {
        value = results.getInt(“value”);
        System.out.println(value);
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 at 5:08

























        answered Nov 13 at 5:02









        Joakim Danielson

        5,5063521




        5,5063521






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272424%2fjava-sqlite-resultset-return-nothing%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to send String Array data to Server using php in android

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Is anime1.com a legal site for watching anime?