0

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.

2
Contributors
1
Reply
8
Views
3 Years
Discussion Span
Last Post by stultuske
0

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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.