I have a form and I want to send the data to a database. How can I pass the request data through the method parameters and send it to the Database?
int status = InsertCustomer(fName, mName, lName , iage, issn, city, state, country);
//Method
// This method should return an int that the executeUpdate // methods returns. Note: the driver name and the URL are // already available in the init() method.
private int InsertCustomer(String firstName, String midName, String lastName, int age, int ssn, String city, String state, String country) {
// JDBC logic
try {
Class.forName(driverName);
Connection conn = DriverManager.getConnection(databaseURL);
java.sql.Statement st = conn.createStatement();
st.executeUpdate("INSERT INTO Customer(firstName, midName, lastName, age, ssn, city, state, country)" +
"VALUES ('?', '?', '?', ?, ?, '?', '?', '?')";
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return 1;
}
I’m a lil bit lost, a little would be very much appreciated.
Either you use a simple (i.e. not prepared statement), and you can’t pass any parameter:
Or (and in your case, that’s what you should do), you use a prepared statament and pass parameters:
In your code, you’re using a simple statement and try to execute a SQL query which needs parameters. That’s not possible. You need a prepared statement to do that.
More information in the JDBC tutorial.