• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sql server update problem

 
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:
Why when I update one row in the database, all the options are updated correctly, but the image of another row is edited.

For example, I edit ID number 1, the date is edited correctly, but the photo of row 3 of the database is changed. But there is no change in the photo of row 1


 
Saloon Keeper
Posts: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
1. Class.forName() to load a database driver is obsolete. Get rid of it.

2. You have a hybrid Prepared Statement. That probably is part of your problem. And it can be used to create a SQL Injection Attack. Do this:
Also note that it is not good practice to start the name of a variable (TxtID) with an uppercase letter.

I prefer not to use uppercase on names of database schema items such as table and column names, because some databases can get confused when that is done. SQL itself is case-insensitive, though, and I prefer the common convention of writing SQL command names and operational elements in all caps.
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:
This is not my original code. For your better understanding I have created this code to find an answer for the solution of the problem faster and also this method did not solve my problem
 
Saloon Keeper
Posts: 7597
177
  • Number of slices to send:
    Optional 'thank-you' note:
Without seeing the actual values of the records in question, and an indication of how they did or did not change it'll be hard to track this down. Post that data, and the code you're now using after you made the change Tim suggested.

Swing likely has nothing to do with this.
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:
this is my all code
class for jtable

The continuation of the code is in the next post
 
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:
I'm suspicious of the byte array, since it's a mutable object. Is it possible that your code is using only a single byte array and putting a reference to that array in each instance of the Test_JTable object?
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:
java form

 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:


 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:


Now, even when I press the save button, the pictures of other lines in the database change, for example, when saving a new record, the pictures of the first record are replaced with the new record, and the new picture is saved in the first record in the database. The same problem exists when editing a record.
I think that if the problem was with the byte array, the photo of the same line of data should have been changed. Why should the photo of another record in the database be edited when I use save button?
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:
ok sorry
my data base is sql server 2022
my table name is test
Its fields are:
id int auto increment not NULL
dates date allow NULL
images image allow NULL
 
Paul Clapham
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:

maysam soleymani qorabaee wrote:Why when I update one row in the database, all the options are updated correctly, but the image of another row is edited.

For example, I edit ID number 1, the date is edited correctly, but the photo of row 3 of the database is changed. But there is no change in the photo of row 1



I was confused by this originally and I'm still confused. I think what's confusing me is that I don't understand the difference between "editing" and "updating" in your terminology.

When you update a row in the database... I think I understand that, I assume you are using an SQL UPDATE statement to do that. Am I right so far? But then you say an image from a different row is "edited". Well first of all I don't know whether you're talking about a row in the JTable or a row in the database table. And second, I don't know what you mean when you say the image is "edited". Does something on the screen change unexpectedly? Or is it something in the database which changes unexpectedly? Or am I totally misunderstanding "edited"?

You posted a lot of code, and sure, Swing code can be voluminous for a simple task, but if I don't know what is going wrong then I just give up looking at the code.
 
Saloon Keeper
Posts: 10779
86
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
I just glanced at the code.
I think this should be:
otherwise you keep overwriting 'row'.
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:
In the database, we use the update command to update or edit a row, and use the insert command to save or insert new row
In the above codes, when I use the insert command, a new row is added to the database
But the photo I have chosen for the new row is moved with the photo of one of the previous rows in database.
in other words, the new photo is inserted for the previous rows and the photo of the previous row is moved to the new row in sql server.
The same thing happens when updating a line in database.

that's mean the photos are moved randomly in the database when I use the save or edit button in the program
 
Paul Clapham
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:


I think my weak hypothesis is that "list.get(i).getImage()" always returns a reference to the same byte array.
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:
I don't think the problem is the value of the byte array
Because the selected photo is correctly inserted in the database, but it is insert or update in the wrong row
Interestingly,when i use save or edit button the date value is inserted or updated correctly and in its place (in database). but image It moves with other lines
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:
And the photo of the line where the photo is entered will be moved to the selected line. Now whether I use the save button or the edit button
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:I just glanced at the code.
I think this should be:
otherwise you keep overwriting 'row'.


This appears in 2 places and will display the wrong contents for the rows.
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:
I edited this part of the code but it didn't fix the problem
Does anyone have a solution?
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:
Is it possible that the problem is from the database library or the database itself?
 
Paul Clapham
Marshal
Posts: 28258
95
  • 3
  • Number of slices to send:
    Optional 'thank-you' note:

maysam soleymani qorabaee wrote:Is it possible that the problem is from the database library or the database itself?



There's a useful rule in debugging: "It's your fault". Yes, occasionally you will get burned by a long-hidden bug in some well-tested system software. But normally it's your fault. Don't waste any time in looking for bugs in other people's code until you're sure it isn't your bug.

I would suggest you take that code and get rid of all of the Swing code. Just reduce it to some code in which you put some images into your database. When you have that working, encapsulate it in a method. Then put the Swing code back in and have it call that method. If the bug reappears, then (as I said) it's your fault but at least you have restricted the number of places you have to look for it.
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • 2
  • Number of slices to send:
    Optional 'thank-you' note:
In addition to Paul's suggestion about ripping out the GUI stuff, I would refactor your code so that all your database code is not intertwined with the rest of the business logic. For example:
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
This should be almost everything you need to run, the only thing that needs to be filled in is the "getImage(String fname)" method that returns a byte[] given a file name. For "Keyboard" see the link in my signature below for "KeyboardUtility".
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:
I noticed that when I call show_Test_Last(); after initComponents(); or saving or editing  the images are not displayed correctly
But when show_Test(); I call it after initComponents(); or registration or editing, the photos are displayed correctly
What is wrong with this code that after saving or editing or initComponents(); Does it ruin the photo!
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:
I noticed that when I use order by ID DESC in my query, there is a problem in saving and editing the photo
Why do you think this happens?
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
I'm not familiar with the database you are using but you have
This is what I've seen on other DBs

 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
How about a before and after screen shot of your table showing the problem data.
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:I'm not familiar with the database you are using but you have
This is what I've seen on other DBs



it's not working in sql server

Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'LIMIT'.

in sql server must use
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
OK. Each DB is slightly different so no great surprise.
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
Have you thought of trying the non-GUI code I posted?
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:How about a before and after screen shot of your table showing the problem data.



Pay attention to the ID, date and photos carefully

All Image's
image 1
Image 2
Image 3
Image 4
Image 5
Image 6
Image 7
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:Have you thought of trying the non-GUI code I posted?


Yes, the problem still remains
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
Well, for one thing, it seems that when you select a row it is using the row number as the ID, not the ID lists  in the row number.
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
The code to populate the table is based off of TestListLast, e.g. reverse order. The code to select from the list is based off of TestList which is in ascending order.

Beddy for me.
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:Well, for one thing, it seems that when you select a row it is using the row number as the ID, not the ID lists  in the row number.


I thought the same at first, so I entered the ID number manually, but the problem was still not solved. I even printed the ID number or entered it in the text box to make sure, but the problem was not solved.
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:The code to populate the table is based off of TestListLast, e.g. reverse order. The code to select from the list is based off of TestList which is in ascending order.

Beddy for me.



That's exactly why I don't understand where the problem comes from
 
maysam soleymani qorabaee
Ranch Hand
Posts: 32
  • Number of slices to send:
    Optional 'thank-you' note:
I tried to remove the order by ID DESC . and With the following code, display the data in the table from the last to the first


And again, the photos were not displayed in their place
I am almost sure that the problem is with the table
But I don't know which part is the problem and how to fix it
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:

maysam soleymani qorabaee wrote:

Carey Brown wrote:The code to populate the table is based off of TestListLast, e.g. reverse order. The code to select from the list is based off of TestList which is in ascending order.


That's exactly why I don't understand where the problem comes from


You are keeping a shadow copy of your table in a list. You are highlighting a row from the table but then using the data from the list which is not in the same order.

In fact you have two shadow copies, one in ascending order and one in descending order and you are not matching up which one you are using to which way the JTable is currently displayed.

I suggest ditching the shadow copies. You do need a list temporarily to load the database results into the table but that's it. After that, when you select a row, use the row cells for your parameters  (no list).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |