Imran Shad Imran Shad - 1 month ago 5
SQL Question

insert into database having large number of parameter

i have to insert data into database .Because sql statement VALUES(......) have 8 parameters . Is it proficient way to use insert statement ?

Class UserRegistration {
public void insertToDatabase(){
String sql=" INSERT INTO db1(......) Values(id,fname,lastname,username,password,usertype,email,contact,address )"



The correct way to do it in Java, assuming you're using JDBC, is to use a PreparedStatement.

String sql = "INSERT INTO db1" +
            " (id,fname,lastname,username,password,usertype,email,contact,address)" +
            " VALUES (?,?,?,?,?,?,?,?,?)";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
    stmt.setInt   (1, id);
    stmt.setString(2, fname);
    stmt.setString(3, lastname);
    stmt.setString(4, username);
    stmt.setString(5, password);
    stmt.setString(6, usertype);
    stmt.setString(7, email);
    stmt.setString(8, contact);
    stmt.setString(9, address);