I am trying to find out if it is possible to use the same insert function each time with new different parameters in java 8 & MySQL. I know it is possible in PHP as I have the PHP code below :
this is the array records:
$Customer_details=[
'firstname' => $Firstname,
'lastname'=> $lastname,
'phone' => $phone,
'email' => $email,
'password'=> $hash
];
After created the class object which is $object2 this is the records:
$object2->insert($pdo,'customer',$Customer_details);
The following code is the PHP insert function code:
function insert( $pdo, $table, $record) {
$keys = array_keys( $record);
$values = implode( ', ' , $keys);
$valuesWithColon = implode( ', :' , $keys);
$query = 'INSERT INTO ' . $table . ' (' . $values . ') VALUES (:' .
$valuesWithColon . ')' ;
$stmt = $pdo->prepare( $query);
$stmt->execute( $record);
return $stmt;
}
Can anybody just show me an example of the same thing in java, Please?
Yes, you can do that in much the same way you would achieve it with php, as @lexicore commented, a common pattern for parameterised sql queries in Java is to use PreparedStatements
Here is an example.. you would just need to wrap the code in that example in a function to make it reusable like your php example.. https://alvinalexander.com/java/java-mysql-insert-example-preparedstatement