Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-26-2008, 07:56 PM
Member
 
Join Date: Jul 2008
Posts: 27
int80 is on a distinguished road
MySQL + Java - auto incrementation issue
I've made a program that takes details from a person, and puts them into a MySQL database. I've set an order_id to auto_increment, but I also want a group_order_id: So if 10 people make an order as a group, I want them all to have the same group_order_id, but individual order_ids. I can't set group_order_id to auto increment as it wont give the group their shared IDs.

I was just wondering if anyone knew how to do this. The end of the order would be triggered by an event such as a button, so maybe I could just check to see what the last group_order_id is, then just do something to that.

But I want to know if there a way of doing this in mysql? I'm a bit of a database noob, so take it easy

Code:
mysql> DESCRIBE order_details; +-------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+------------------+------+-----+---------+----------------+ | order_id | int(10) unsigned | NO | PRI | NULL | auto_increment | |group_order_id| int(10) unsigned| NO | | NULL | | | f_name | varchar(20) | YES | | NULL | | | l_name | varchar(20) | YES | | NULL | | | date | varchar(10) | YES | | NULL | | | no_people | int(10) unsigned | YES | | NULL | | | hire_length | int(10) unsigned | YES | | NULL | | | Bike_Type | varchar(25) | YES | | NULL | | | Frame_Size | varchar(10) | YES | | NULL | | | Helmet_Size | int(10) unsigned | YES | | NULL | | | Panniers | varchar(5) | YES | | NULL | | | Helmet | varchar(5) | YES | | NULL | | | Sex | varchar(10) | YES | | NULL | | +-------------+------------------+------+-----+---------+----------------+

Thanks in advance.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-26-2008, 08:32 PM
Member
 
Join Date: Jul 2008
Posts: 27
int80 is on a distinguished road
I've just worked it out....But still, if anyone knows anything about that, I would still like to hear if it's the same as what I've done...It's quite easy really, just stumped me for a bit.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-28-2008, 05:11 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
Eku is on a distinguished road
Im suggesting you can have a another table for Group that will contain the following.

group_id = auto increament
group_description = description
and others

your program can validate and fetch from the group_table which group will be the customer in. And your program can also create new groups for some customer. therefore when executing a transaction the group_id will be from the group table NOT from the order details.

I hope that helps
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-29-2008, 04:54 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 533
fishtoprecords is on a distinguished road
It easy. JDBC has a function specifically for this.

Code:
int numRows = stmt.executeUpdate(command, Statement.RETURN_GENERATED_KEYS); ResultSet rs = null; try { rs = stmt.getGeneratedKeys(); if (rs.next()) { autoIncKeyFromApi = rs.getInt(1); } else { throw new RuntimeException("PIB, can't find most recent insert we just entered"); } rs.close(); } catch (SQLException ex) { ex.printStackTrace(); }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-29-2008, 09:20 AM
Member
 
Join Date: Jul 2008
Posts: 34
jack239 is on a distinguished road
I think you should go by the solution given by EU in database side as it is very reliable than the jdbc side. What do you think guys?
__________________
New to Java/PHP/Javascript development?
For free help go to-
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-29-2008, 11:43 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
Eku is on a distinguished road
fishtoprecords 's suggestion is Ok for me, i havent tried that one yet. I used to get the last number by using a select statement with count. or in our case here we have a algorithm in the database where in we use "Select sequence_table from duel". We have a seperate generator for primary key in the database.

In the case here, (for example just to give you an idea), we have a common database and not everyone accesing the database uses java ^_^ Thats why we have the autoincreament part in the database rather in the software. ^_^ and it is the most common practice that i observe here in our company
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.

Last edited by Eku : 07-29-2008 at 11:47 AM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-29-2008, 08:04 PM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 533
fishtoprecords is on a distinguished road
Quote:
Originally Posted by jack239 View Post
I think you should go by the solution given by EU in database side as it is very reliable than the jdbc side. What do you think guys?
I don't fullly grok his idea. But if he is suggesting using a second table just to hold the unique id, I think this is a very bad idea. You want fewer tables, fewer joins. Tables grow and reproduce themselves, sometimes like bunnies. No need to encourage them.

What do you do when the second table is out of sync with the first? Don't say it can never happen, if you have two things to do anything, there is a chance for them to get out of sync. Now what do you do?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to combine mysql and java? sandeeprao.techno Advanced Java 1 05-21-2008 06:41 AM
java to mysql thamizhisai New To Java 12 04-28-2008 09:48 AM
java to mysql thamizhisai Advanced Java 1 04-26-2008 10:21 AM
MySQL issue: Exception while getting MetaDataInfo sandeepspatil Database 2 07-27-2007 08:54 AM
how to issue the command of Ctrl-C (copy) in Java bilal_ali_java Advanced Java 0 07-18-2007 05:14 PM


All times are GMT +3. The time now is 08:28 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org