1

In this code I want to display single record where the input is taken from the user, but it shows "You have an error in your SQL syntax".

import java.sql.*;
import java.util.Scanner;

public class Display {

    public static void main(String[] args) throws ClassNotFoundException{

        try{

            Scanner scan= new Scanner(System.in);
            String driver = "com.mysql.jdbc.Driver", u_name="root", pwd="123456", url="jdbc:mysql://localhost:3306/student";
            Class.forName(driver);
            Connection con=DriverManager.getConnection(url, u_name, pwd);
            if(con!=null)
                System.out.println("Connection Successful!");


            System.out.print("Enter the Enrolment to be Updated: ");
            String en=scan.next();
            String qry="SELECT * FROM studentdb WHERE enrolment = ?";
            PreparedStatement stmt=con.prepareStatement(qry);
            stmt.setString(1, en);

            ResultSet result = stmt.executeQuery(qry);
            result.next();
            System.out.print(result.getString(1)+"\t\t"+result.getString(2));
        }
        catch(SQLException e){
            System.out.println("Error: "+e);            
        }    
    }
}
2
  • ResultSet result = stmt.executeQuery();... Commented Sep 5, 2017 at 14:41
  • First, to select only one record, use SELECT * FROM studentdb WHERE enrolment=? LIMIT 1, and secondly I hope that this is not your real username and password you use in the database. Oh, and for the error yes, use the code from @Reimeus one comment above mine. Commented Sep 5, 2017 at 14:42

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.