Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to properly use a record which has a composite key in mySQL code in Java

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to properly use a record which has a composite key in mySQL code in Java

    So I am making a small software for a Hotel Management System and I need some help calling the values from the database. In my database I have a table called "keys". This table has the following columns: idfood , iditem , amount. 'idfood' and 'iditem' are both primary keys forming a composite key.

    This is what the database looks like with test values : database.JPG

    Now I made an interface in NetBeans which looks like this: interface.JPG

    What I want to happen is, when I enter a value in the text box (For example '1') all the records which have idfood as '1' should display in the table. I would appreciate any help.

    Following is my code (the button named 'Search'):

     
        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
     
        String a = jTextField1.getText();
     
        try {
     
            Statement s = DB.getConnection().createStatement();
            ResultSet rs = s.executeQuery("SELECT * FROM keys WHERE idfood = '"+a+"'");
     
            while(rs.next()){
     
                Vector v = new Vector();
                v.add(rs.getString("idfood"));
                v.add(rs.getString("iditem"));
                v.add(rs.getString("amount"));
     
                dtm.addRow(v);
            }
        } 
     
        catch (Exception e) {
            e.printStackTrace();
        }

    When I run the above code I get the following error in NetBeans 7.3:
    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'keys WHERE idfood = '1'' at line 1

    Please help. I'm still a bit new to java. Still a student. So please don't use complicated jargon. Much appreciated
    Last edited by AnkleBreaker; May 6th, 2014 at 11:30 AM. Reason: Adding tags


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to properly use a record which has a composite key in mySQL code in Java

    Please wrap all code using the proper code tags to preserve formatting, and please read Announcements - What's Wrong With My Code?

    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'keys WHERE idfood = '1'' at line 1
    First, I'd recommend using a PreparedStatement - its cleaner and prevents injection. Second, you said your table name is KOT, but you are selecting from keys?

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to properly use a record which has a composite key in mySQL code in Java

    Thanks for you reply

    First off, I didn't know about the tagging thing. I'm very sorry about that.

    2nd: I'm not sure how to use a PreparedStatement. In my college they just taught us the way I have shown above. I tried to watch a video tutorial on PreparedStatement but it was not very clear. Could you please edit my code using PreparedStatement?

    3rd: Yeah, I'm sorry about the confusion. In the final project, the table will be named KOT. I named the table "keys" as I wanted to first test out the composite key method. I'll edit my question accordingly.

    Again, thanks for you reply.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to properly use a record which has a composite key in mySQL code in Java

    See Using Prepared Statements (The Java™ Tutorials > JDBC(TM) Database Access > JDBC Basics)

Similar Threads

  1. Replies: 1
    Last Post: April 3rd, 2014, 02:11 PM
  2. JAVA CODE FOR IMPORTING XLS FILE INTO MYSQL
    By abhi1402 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: November 7th, 2013, 04:03 AM
  3. application in java to have the key code
    By blacksadangel in forum Java Theory & Questions
    Replies: 3
    Last Post: June 5th, 2013, 04:53 AM
  4. Newbie question: Hashmap with composite object key
    By credible58 in forum Java Theory & Questions
    Replies: 0
    Last Post: December 1st, 2012, 01:21 AM
  5. [SOLVED] restricting a particular key stroke or key code
    By chronoz13 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 22nd, 2011, 11:19 AM

Tags for this Thread