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;
}
java sqlite
add a comment |
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;
}
java sqlite
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 yourexecuteInsert()method as well
– mangusta
Nov 13 at 1:33
I can not see anywhere that you are doingcommits
– Scary Wombat
Nov 13 at 1:34
add a comment |
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;
}
java sqlite
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
java sqlite
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 yourexecuteInsert()method as well
– mangusta
Nov 13 at 1:33
I can not see anywhere that you are doingcommits
– Scary Wombat
Nov 13 at 1:34
add a comment |
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 yourexecuteInsert()method as well
– mangusta
Nov 13 at 1:33
I can not see anywhere that you are doingcommits
– 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
add a comment |
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);
}
}
add a comment |
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);
}
}
add a comment |
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);
}
}
add a comment |
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);
}
}
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);
}
}
edited Nov 13 at 5:08
answered Nov 13 at 5:02
Joakim Danielson
5,5063521
5,5063521
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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