steven967
steven967

Reputation: 123

Insert a row to MySQL table with jsp

I want to insert rows to mysql database with an jsp code, but I can´t. Here is my jsp code without the html form:

<%
String login = request.getParameter("login");
String password = request.getParameter("password");
String full_name = request.getParameter("full_name");
String ulevel = request.getParameter("ulevel");
String team_id = request.getParameter("team_id");

Connection connection = null;
PreparedStatement pstatement = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
int updateQuery = 0;
if(login!=null && password!=null && full_name!=null && ulevel!=null && team_id!=null){
if(login!="" && password!="" && full_name!="" && ulevel!="" && team_id!="") {
    try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/android","root","root");
String queryString = "INSERT INTO users(user_id,login,password,full_name,ulevel,team_id) VALUES (null, ?, ?, ?, ?, ?)";
pstatement = connection.prepareStatement(queryString);
pstatement.setString(2, login);
pstatement.setString(3, password);
pstatement.setString(4, full_name);
pstatement.setString(5, ulevel);
pstatement.setString(6, team_id);
updateQuery = pstatement.executeUpdate();
if (updateQuery != 0) { %> 

When I press SUBMIT, the webpage shows me this:

type Status report message descriptionThe requested resource () is not available.

p.s.: column user_id is set to autoincrement in mysql table, so I use null.

But I dont know, that´s the right way...

I run the code from netbeans 7.0.1

Upvotes: 2

Views: 14250

Answers (3)

Datta Dhebe
Datta Dhebe

Reputation: 1

String queryString = "INSERT INTO users(user_id,login,password,full_name,ulevel,team_id) VALUES (?, ?, ?, ?, ?)";

You should not use anything not even null for query!

Upvotes: 0

steven967
steven967

Reputation: 123

I found the problem. The error was in html form, at attribute action, where i had an source file, which i never had before. :/

Upvotes: 0

John Woo
John Woo

Reputation: 263713

since ID is auto-incremented, get rid of it from the list, preparedstatement is looking for index 1 on its parameters.

String queryString = "INSERT INTO users(login,password,full_name,ulevel,team_id) VALUES (?, ?, ?, ?, ?)";
pstatement = connection.prepareStatement(queryString);
pstatement.setString(1, login);
pstatement.setString(2, password);
pstatement.setString(3, full_name);
pstatement.setString(4, ulevel);
pstatement.setString(5, team_id);

this one below is not tested

just start you parameter with 1 because there are only 5 parameters to be defined.

String queryString = "INSERT INTO users(user_id,login,password,full_name,ulevel,team_id) VALUES (null, ?, ?, ?, ?, ?)";
pstatement = connection.prepareStatement(queryString);
pstatement.setString(1, login);
pstatement.setString(2, password);
pstatement.setString(3, full_name);
pstatement.setString(4, ulevel);
pstatement.setString(5, team_id);

Upvotes: 2

Related Questions