This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
To begin, I am pretty new at Java, so I apologize for any amateurish mistakes. Basically, what I have tried to do is create a tool that will allow me to copy many items from a database and then randomize them.
Some of the things I am using, I am not entirely confident about because I am only a Freshman computer science major, so I have not actually encountered them in class, so I am not entirely sure if there is a problem or where it is.
If i put a few IDs in the main field and then attempt to randomize their stats, everything will work well, and when it is done, I will get the message saying that it has completed; however, when I try to randomize a list of a couple of hundred of things,
it appears to work, but I do not get a success message which makes me think it is stopping somewhere and just not telling me.
I would appreciate it if anyone could look over this and tell me what errors there are. The following code is just for the randomizing part of my program.
The actual program itself is written using the same style and far too long to put in one post (~1000 lines). If you're wondering, this is for a video game server too.
First of all, you need to edit your post (click the button on your post) and wrap the extremely long lines at 80 (at most 120) characters.
I don't think anyone is going to read that code in its current form.
This way your code is completely unreadable, not only here on the website, but in your development environment as well.
Secondly, please be more precise in describing the faulty behavior of your program. What exactly does "it appears to work" mean?
Also remember that database operations can be really long. A query that works well on one row can take ages when run over a thousand rows.
Writing performant queries (the famous "SQL tuning") can be pretty demanding task and does require some knowledge and experience with relational databases.
Ok, I formatted it so it does not look as terrible anymore. To be more specific, the code I found online for doing things with SQL had this
which I assumed to be a way to print out that everything had successfully completed. When I run this part of my program with a few item IDs,
it will return that message. Where I am not entirely sure if there is a problem though is when I am modifying upwards of 100 things because after I click the button,
I do not get any message printed out unless there is a blank ID error. As I said, most of this code was copy/paste/modify, so I'm not sure if the way I have it written makes
it more likely for something to go wrong for large amounts of IDs. I said it appears to work though because I will look at the first ID, last ID, and a middle ID after execution and
they will have been affected in at least some of the fields (the program is designed for some fields to get a +0 value, so staying the same on some things is expected.) So, what I am asking
is does anyone know a reason why that success message comes up for small groups of numbers but not large ones? It just makes me think that there is an error in there somewhere that's preventing
me from getting the message.
I'm not familiar with multi-update queries; when you get the result from Statement.execute("update, update, update, update") what exactly is it the result of? The API documentation doesn't mention this particular case.
Also, I think your code would be very much simplified if you used PreparedStatement to do that, rather than generating a giant SQL statement with string concatenation like you did.
I also don't understand why you're trying to get the generated keys from a large list of Update statements. I wouldn't have thought that any keys would be generated, since you aren't doing any Inserts. (Not that I can see, anyway.)
Joined: Feb 19, 2013
Ok, I fixed that. I rewrote it to remove the keys. I tested it, and it worked fine a few times, but then stopped. Like, I randomized 100 items perfectly, then tried to just modify one item, and it stopped doing anything and did not give an error anywhere. Clearly, I have some problem in there or it is an issue with BlueJ. Does anyone have any idea how I could better locate the problem?