• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

update sql-table from JList

 
Robert Ingmarsson
Greenhorn
Posts: 27
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best forumists!

I have a real headache. I am a total beginner to jdbc in java and pretty much to programming in general. I want to update a table
in sql with data extracted from a JList. Is there here some kind souls that can show me some code on how you do this or
point me to examples or artciles out there on this topic. I know some basic jdbc programming and can connect to a database and insert
single records.

I'm totally stuck in this topic. Hope not my question is too far off. I have only managed to update components with data from a ResultSet but
i can't understand how you insert list based arrays of data in an sql table using jdbc. What is the general approach on how to do this with data
from a DefaultListModel ?

Best regards in advance.
//Robert
 
Paul Clapham
Sheriff
Posts: 21559
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two steps in that process:

1. Get the data from the list model.

2. Insert the data into the database.

Those are completely separate things and there's no reason to mix them together. In particular you aren't likely to find tutorials which tell you how to do both of them, any more than you'll find a recipe for tomato soup followed by mushroom quiche.

So: which of those two things are you having trouble with? From your post it sounds like you aren't quite sure of how to update, or insert, data into a database, so do you want to start with that one? At least it's on-topic for the forum your posted in.
 
Robert Ingmarsson
Greenhorn
Posts: 27
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:There are two steps in that process:

1. Get the data from the list model.

2. Insert the data into the database.

Those are completely separate things and there's no reason to mix them together. In particular you aren't likely to find tutorials which tell you how to do both of them, any more than you'll find a recipe for tomato soup followed by mushroom quiche.

So: which of those two things are you having trouble with? From your post it sounds like you aren't quite sure of how to update, or insert, data into a database, so do you want to start with that one? At least it's on-topic for the forum your posted in.


Best Mr Clapham!

I'm working on a simple movie database. My SQL table where I store information about the current title is pretty big. I have scaled down the code for this example. My problem is that I can only update a database with simple strings and not the
information stored in a JList object. How do I extract data from my JList which in this case is the genres of a film the user selects in a dialog. Below is a simplified version of the code that i am working on.

I am storing the information in the database by using a PreparedStatement and the setString method. But it doesn't work when it comes to processing the information extraxted from my DefaultListModel. This is where i give up. Sorry for bad code
though, but i am a total beginner. The code is part of a class called DataHandler which extends Thread and its main purpose is to store information the user have entered in the gui.


 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert Ingmarsson wrote:// This is where i know i am doing wrong.
// The only data that is saved in the database is the last element in an array
// of genre values.

That makes sense as you are iterating over genreModel and you overwrite the GENRE parameter with each selected value, so only the last selected value of genreModel will not be overwritten and thus will be saved to the database.

If you have multiple genres for one movie, your current database model is insufficient because you can only save one genre. In the column genre, you could save a comma-seperated list of different genres, but I definitely would not recommend to do this because that is a very poor design choice. Another possibility would be to create columns GENRE1, GENRE2, and GENRE3. But you immediately notices the drawback of this approach: if a movie should have 4 or 5 genres, it will also not be possible. This database model will be better than the first one, but because of its limitations still not the recommended one. The recommend database model/design is to have a seperate GENRE table. In this table you have an overview of all possible genres, e.g.This table will be used to populate the genreModel. And then you will have another table MEDIA_TITLE_GENRE which holds the genres for each movieSo for each selected value in genreModel you need to insert one row in the GENRE_PER_MOVIE table.

On a side note: it seems the table MEDIA_TITLE does not have a primary key. That's a very bad practice, because the primary key is the unique identifier of each record in the table. And you can't use the ORIGINAL_TITLE because there are several movies with the same title.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic