1

When I execute this code, I get the error:

Exception in thread "main" java.sql.SQLException: Can not issue empty query

What is wrong with my code?

public static void main(String[] args) throws IOException,
        ClassNotFoundException, SQLException {
    // TODO Auto-generated method stub

    DataInputStream d = new DataInputStream(System.in);
    // for keyboard input
    System.out.println("Enter Employee id: ");
    int empid = Integer.parseInt(d.readLine());

    System.out.println("Enter Employee name: ");
    String empname = d.readLine();

    System.out.println("Enter Employee Salary: ");
    double empsalary = Double.parseDouble(d.readLine());

    Class.forName("com.mysql.jdbc.Driver"); // Loading MYSQL Driver
    Connection con = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/employee", "root", "admin");
    PreparedStatement pst = con
            .prepareStatement("insert into employee values(?,?,?)");
    pst.setInt(1, empid);
    pst.setString(2, empname);
    pst.setDouble(3, empsalary);
    int rowcount = pst.executeUpdate("");
    System.out.println(rowcount + "row has been insereted");

}
3
  • Error massage seems to be related to pst.executeUpdate("");. Have you tried with pst.executeUpdate();? Commented Aug 14, 2014 at 21:50
  • WOrked thanks. But why should'nt i put quotes ? Commented Aug 14, 2014 at 22:27
  • 1
    You probably should read documentations of executeUpdate(String sql) and executeUpdate() and you will notice that String sql is parameter representing an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE which should be executed. In your case this statement is empty and because of that you see your SQLException. Commented Aug 14, 2014 at 22:31

1 Answer 1

4

Your are calling executeUpdate(String) from the Statement interface, not the executeUpdate() from PreparedStatement.

Remove the two quotes and it should work

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.