0

This is my source code.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public class DBConnect {

    public static void main(String[] args) {

        try {
            String host = "jdbc:derby://localhost:1527/Employee";
            String uName = "jayani";
            String uPass = "jayani";
            Connection con = DriverManager.getConnection(host, uName, uPass);
            Statement stmt = con.createStatement();
            String sql = "SELECT*FROM WORKERS";

            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                int id_col = rs.getInt("ID");
                String fname = rs.getString("FIRST_NAME");
                String lname = rs.getString("LAST_NAME");
                String job = rs.getString("JOB_TITLE");

                System.out.println(id_col + "" + fname + "" + lname + "" + job);

            }
        } catch (SQLException ex) {

            System.out.println(ex.getMessage());
        }
    }
}

Here i got an exception like "executeQuery method can not be used for update." What is this exception and how can I solve this. Thank you.

2
  • Which one do you actually use: Microsoft sql-server or MySQL? Commented Feb 25, 2015 at 6:10
  • @IvanGerasimenko seems as if it is derby :-) Commented Feb 25, 2015 at 6:22

1 Answer 1

1

You need to use executeQuery like this

    Statement stmt = null;
    String query = "select * from <tablename>";
    stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, but when i used it ,i'm getting an error like "incompatible types required: ResultSet found: int ----"..Why is that?
What is the datatype of "FIRST_NAME", "LAST_NAME","JOB_TITLE" in database table
How? I had to use String sql = "SELECT * F\";ROM APP.Workers"; in place of String query = "select * from <tablename>"; :)
My Bad, that I gave you as an example only :)

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.