I have a simple program where I want to take a user supplied first name and last name, then enter those into a MySql database. The two variables, call them first and last, will be simple command line prompts, so scanner first then scanner last. Then take first and last and add them to my MySql statement. Here is the code I have, but for some reason I cannot get the syntax correct. In this example I'm using statments, although I can and would use prepared statements. In the real world I would use prepared statements, but even a statement would work for this project.
Here is my code with one scanner line. Right now the code does work with the two constant values Cathy and Jones. I want those to be variables.
class AddStudent
{
public static void main (String[] args)
{
try
{
Connection conn = DriverManager.getConnection(url,user,pass);
Statement st = conn.createStatement();
Scanner first = new Scanner(System.in);
System.out.println(first.nextLine());
String SQL = "INSERT INTO test VALUES ('Cathy', 'Jones')";
st.executeUpdate(SQL);
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
private static String url = "jdbc:mysql://localhost:3306/registrar";