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.