I have two different format of code for insert query using jsp+servlet+mysql.

Code-1
Class.forName(JDBC_DRIVER);
// Open a connection
out.println("Connecting to a selected database...");
con = DriverManager.getConnection(DB_URL, USER, PASSWORD);
out.println("Connected database successfully...");

// Execute a query
st = con.createStatement();
sql = "insert into users (name,email,password) values ('" + name + "','" + email + "','" + password + "')";
out.println(sql);
int i = st.executeUpdate(sql);
if (i > 0) {
 out.println("Inserted!!");
}
Code-2
Class.forName(JDBC_DRIVER);
con = DriverManager.getConnection(DB_URL, USER, PASSWORD);
PreparedStatement ps;
st = con.createStatement();
sql = "insert into users (name,email,password) values (?,?,?)";
ps = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
ps.setString(1, name);
ps.setString(2, email);
ps.setString(3, password);
ps.executeUpdate();

Above both code not working for me.
Please help me to solve this this issue.

I don't want to update mysql table. Suppose If there is an application in PHP language and now I want to convert into JSP. Why should I have to disturb MySql. In Another case, If there is two different Language Application accessing same database, then? So there should be solution from server side scripting.

auto increment is something you configure in your database, not in your servlet code.

anyway, if you want to 'convert it to jsp/servlet', you should re-write your code.

saying "not working for me" doesn't help us. what exactly doesn't work? it doesn't compile? it doesn't run? does it throw an exception? do you get unexpected results? what do you expect? what do you get?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.