1
try 
{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con1=DriverManager.getConnection("jdbc:odbc:MyDatabase");
    st1=con1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
    System.out.println("Connect database in BallMoves1.java .......");
    /*the below line giving error*/
    rs1 = st1.executeQuery("insert into highscore" + " (score) " + "values('"+score+"')");
    System.out.println("Score is inserted..");
    System.out.println("Score......."+score);
}catch(Exception e){ e.printStackTrace();}

/*highscore is table and attributes of table are (sid,score).

the resulting error is:

Connect database in BallMoves1.java .......
java.sql.SQLException: No ResultSet was produced
    at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(JdbcOdbcStatement.java:258)
    at BallMoves1.move(BallMoves1.java:378)
    at BallMoves1.run(BallMoves1.java:223)
    at java.lang.Thread.run(Thread.java:744)*/
1
  • And the error is...? (You should always include the details of the error message.) Also, use parameterized SQL instead of putting the value directly into your SQL... and please format your posts more carefully in future, using spaces instead of tabs. Commented Apr 29, 2014 at 10:09

1 Answer 1

2

You're calling executeQuery on something that isn't a query. But instead of calling execute with the same SQL, you should use a PreparedStatement:

String sql = "insert into highscore (score) values (?)";
try (Connection conn = DriverManager.getConnection("jdbc:odbc:MyDatabase");
    PreparedStatement statement = conn.prepareStatement(sql)) {
  statement.setInt(1, score);
  statement.executeUpdate();
  conn.commit();
}

Always use parameterized SQL, instead of plugging the values directly into the SQL - that protects you from SQL injection attacks, conversion errors, and hard-to-read code.

Use a try-with-resources statement (as I have) to automatically close the statement and connection at the end of the block.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.