A.Conroy A.Conroy - 1 year ago 110
Java Question

Delete from SQLite Databse

I have an SQLite database linked up to my Java project within Eclipse. I'm able to delete entries from the database when I give a hardcoded, specified ID such as '3'. I'm trying to alter the code in order to enable the user the manually pass any number and have it delete that entry.

public static String deleteRecords(String NumberDelete){
Connection dbConnection = null;
Statement statement = null;
dbConnection = getDBConnection();
statement = dbConnection.createStatement();
String sql = "DELETE from employees where ID='NumberDelete';";
} catch(Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
return NumberDelete;

Answer Source

For user input, you might want to check out the Scanner class: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Once you get an integer from the user, parse it, and store it in a variable, you can simply use String concatenation:

String sql = "DELETE from employees where ID='" + userInput + "';";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download