• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mysql how to update fields and delete  RSS feed

 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to make a 'update and delete' query in my test database which is MySql 5.5, so far I have managed to make a 'select and insert'. We proceed in order to make an 'update'. I have divided my code into several parts, a package "ddb" with a class for each query, there are four that you know well, but I admit that for the query update I have trouble selecting update a record to retrieve the index number 'id' and execute my query that I tested under openworkbench.
Here is my code:

first bean


second bean


Servlet update


th jsp update (i use jstl'code



Tomcat error 500

Etat HTTP 500 -

type Rapport d''exception

message

description Le serveur a rencontré une erreur interne qui l''a empêché de satisfaire la requête.

exception

java.lang.NullPointerException
com.boticiel.servlets.Db_update.doPost(Db_update.java:56)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note La trace complète de la cause mère de cette erreur est disponible dans les fichiers journaux de Apache Tomcat/8.0.36.
Apache Tomcat/8.0.36


Sorry for the french english mix of my code, but you can always get a translator !

kinds regards
Philippe
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code won't compile:

That's not valid.  I assume that should be:

?

Also, shouldn't this be using the setId (and why does getId take a parameter?)?

Removing the parameter from getId will also allow you to set the correct parameters for your PreparedStatement, which also needs fixing.
At the moment it won't execute as you have no parameters in there at all (no '?' placeholders).
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I understand your recommendations, but (Id) is of type integer, I liked to know how you do a "cast" of this field of the "noms" table so that I can use it in a good way.
Also my scanning table names dans la jsp :


in utilisateur.java

This is what I write because Id is of type integer, Id is the index of the table !

so, how to do ?

Regards
Philippe
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer.parseInt
That will parse a String to an int.

However that still doesn't explain why getInt takes a parameter.
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, the browsing of the table "noms" to display the fields "prenom" and "nom" do not work in the JSP JSTL encoding,
the index is of type "integer", I do not know in JSTL the parse From an "integer" to a "String"


 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since 'id' in Utilisateur is an int (or should be) I'm not sure what the issue with JSTL is.
Why do you need to transform the int to a String?
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello I
go back to understand how to insert an string from a select query here the index is an integer can convert it to string so that the array contains only string, I have only three fields in my test table: id, name , First name.
How to convert id in my result array



kinds regards
philippe
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still not sure what your problem is.
First question:

Why are you treating 'id' as a String?
It is normally better to try and keep the datatypes the same.
So why are you turning it into a String?

Second point:

If you have an error here then you will never know.
you should at least log the error message.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Unless you have a very long old JDBC driver, I don't think you need lines 56-60.
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:I'm still not sure what your problem is.
>> My main problem is to be able to use the index "id" to process the requests "update" and "delete"
First question:

Why are you treating 'id' as a String?
It is normally better to try and keep the datatypes the same.
So why are you turning it into a String?
>> because i must to work with string for display to the JSTL !
Second point:

If you have an error here then you will never know.
You should at least log the error message
>> That I advise you to handle the exceptions ?.

 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSTL is happy displaying integers.
The value doesn't have to be a String in the class.
So there's no reason to change the number returned by the query into a String.

As for the exceptions, you need to be logging them, even if it's only:


At the moment they are just disappearing.
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why this code JSTL doesn't display int the JSP file ?



Should you declare something else instead of "$" to display an integer ?
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If no <li> blocks are appearing in the HTML then utilisateurs is empty.

If you mean something else then can you copy/paste the block of HTML that this <c:forEach> tag produces?
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:If no <li> blocks are appearing in the HTML then utilisateurs is empty.

If you mean something else then can you copy/paste the block of HTML that this <c:forEach> tag produces?


the jsp file :




the utilisateur.java


the nom.java file :




the servlet for query SELECT


i hope that with this code you can help me

Kinds regards
Philippe
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to post the HTML produced.
That is, what appears on the client browser, not the JSP code.

I still don't know what the actual problem is, because I can't see the problem.
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Ha ok i anderstand this the output of html :



If there is not enough html code say me, I work under firefox, to have a complement html tell me how to access the source code of the page in question!
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:If no <li> blocks are appearing in the HTML then utilisateurs is empty.

It looks like that's your problem, assuming lines 50-52 should have list items in it.
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help, and to be able to support my skills default
For the query SELECT it was just to extract the index, I succeed!
Here is my mistake:


Here is the correction

Now that I know how to extract and display the table names, I must be able to do an UPDATE with the correct index number,
I suggest the following code that does not work:

my dopost of servlet :


An extract from my bean update





my body JSP file




What do you think !

Kins regards
Philippe
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What do you think !

i think like this




Kins regards
Philippe
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A PreparedStatement is of the form:

You would then use the setString and setInt methods to set the values of those placeholder '?', as you did in your earlier post:

However, note that the indexes begin at 1, not 0, and are numbered in the order they appear in the SQL.

Since this is for an UPDATE you will need to get the id to update from the request, so this line needs uncommenting and fixing:

You will need to parse the String returned by getParameter("id") into an int, using Integer.parseInt().
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:A PreparedStatement is of the form:

You would then use the setString and setInt methods to set the values of those placeholder '?', as you did in your earlier post:

However, note that the indexes begin at 1, not 0, and are numbered in the order they appear in the SQL.

>> So far so good, I understand !

Since this is for an UPDATE you will need to get the id to update from the request, so this line needs uncommenting and fixing:

You will need to parse the String returned by getParameter("id") into an int, using Integer.parseInt().


I have trouble to extract the id number from the jsp, I give it correctly but I have no error but the request UPDATE does nothing,
I wrote this::
I have an error on:
The completion offers many choices, but what should you do?

Regards
Philippe
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If request is a ResultSet, then there isn't a method called "parseInt".  You could try
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my code for UPDATE

I was forced to add this code in my method, 36/5000
Auto generated by the stub (completion)

I raise exceptions from TOMCAT >> error 500

for example
Etat HTTP 500 - L''exécution de la servlet a lancé une exception

type Rapport d''exception

message L''exécution de la servlet a lancé une exception

description Le serveur a rencontré une erreur interne qui l''a empêché de satisfaire la requête.

exception

javax.servlet.ServletException: L''exécution de la servlet a lancé une exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

cause mère

java.lang.StackOverflowError
com.boticiel.bdd.Up_noms.getString(Up_noms.java:105)
com.boticiel.bdd.Up_noms.getString(Up_noms.java:105)
com.boticiel.bdd.Up_noms.getString(Up_noms.java:105)
com.boticiel.bdd.Up_noms.getString(Up_noms.java:105)
com.boticiel.bdd.Up_noms.getString(Up_noms.java:105)
com.boticiel.bdd.Up_noms.getString(Up_noms.java:105)
com.boticiel.bdd.Up_noms.getString(Up_noms.java:105)
com.boticiel.bdd.Up_noms.getString(Up_noms.java:105)
com.boticiel.bdd.Up_noms.getString(Up_noms.java:105)
com.boticiel.bdd.Up_noms.getString(Up_noms.java:105)
com.boticiel.bdd.Up_noms.getString(Up_noms.java:105)
com.boticiel.bdd.Up_noms.getString(Up_noms.java:105)


Can be something to add in the servlet ?
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An HTTP error 500 in this situation often means a programming problem.  The Java error StackOverflowError often means that you have a method calling itself with no exit.  And this is the case:

What are you expecting this method to do?  You have a method call getString(String) calling a method getString(String) so it will keep calling itself until it runs out of memory.
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since I added this code to extract the index from the table :

Auto completion add this code:


I think I'm not interpreting my code because I have to work on an integer while auto completion makes me work with String!

I have difficulties to understand
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If i remove auto completion



I have an error on getstring

int id = Integer.parseInt(request.getString("id"));
    utilisateur.setId(id);
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't rely on the compiler or IDE to give you the correct fix for a problem in your code.
They are often only guessing.

From what I remember, you are getting the id originally from an HttpRequest.
So the parsing needs to be done on the parameter retrieved from the request object:


then you can pass the completed utilisateur reference into your method:


Your miseajourUtilisateur method will look something like:
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:

Your miseajourUtilisateur method will look something like:


You are wrong, i have to extract the number of the row to update, the index extracted from the JSP gives the row of the table to update!

This code must be there, but it returns an error on the "getstring"

But how to get around this problem

error :
int id = Integer.parseInt(request.getString("id"));
    utilisateur.setId(id);

Regards
Philippe
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the id field on your HTML form not the id of the row it represents?

I'm a little confused now.
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Why is the id field on your HTML form not the id of the row it represents?

I'm a little confused now.

I do not know what to do the identification since I process it at the beginning of my intranet, and that I have no problem of identification, I treat a table of last name and first name with index, it is very simple,
But I want to understand the queries "SELECT, INSERT, UPDATE'DELETE" first with jdbc.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philippe Ponceblanc wrote:
This code must be there, but it returns an error on the "getstring"

But how to get around this problem

error :
int id = Integer.parseInt(request.getString("id"));
    utilisateur.setId(id);

What's the full text of the error?  What type is request?
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What's the full text of the error?  What type is request?



Here is the code for the update method



What type is [tt]request[


Id is index of my table, the ask type is an integer that is converted to string from the JSP file, but the type is Integer !
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So your Up_noms class needs to have a getString(String input) method. Or perhaps it has some other method with a signature which is slightly different and you've used an incorrect spelling? You should start by looking at that class for the method which you should be using.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!