Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › SQL and JAVA issues: Adding data to primary key?
New Posts  All Forums:Forum Nav:

SQL and JAVA issues: Adding data to primary key?

post #1 of 4
Thread Starter 
Basically I've got some code. I would paste it here but there's around 300 in just one of the files and there's 5... So I'll post as much as I can. Basically my java file writes a MS Access database. Using this code and variants.

Code:
 static String getLabel(String id) {
        try {
            // Need single quote marks ' around the id field in SQL. This is easy to get wrong!
            // For instance if id was "04" the SELECT statement would be:
            // SELECT * FROM MessageTable WHERE id = '04'
            ResultSet res = stmt.executeQuery("SELECT * FROM MessageTable WHERE ID = '" + id + "'");
            if (res.next()) { // there is a result
                return res.getString("Label");
            } else {
                return null; // null means no such item
            }
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    static String setLabel(String id, String label) {
        // SQL UPDATE statement required. For instance if label is "todo" and id is "04" then updateStr is
        // UPDATE Libary SET Label = 'todo' WHERE id = '04'
        String updateStr = "UPDATE MessageTable SET Label = '" + label + "' WHERE ID = '" + id + "'";
        System.out.println(updateStr);
        try {
            stmt.executeUpdate(updateStr);
        } catch (Exception e) {
            System.out.println(e);
        }
        return listLabelled(label);
    }
Basically the email client can modify my existing field. But I can't create ones. I know I'd I'd have to write the ID field which is the primary key but whatever I do it just won't work. Essentially each email has an ID which is the primary key and they're number 1 to whatever. I have 8 "messages" in the data base but I just can't seem to add the 9th and so on... What SQL statement or codes would I have to use??
Edited by kidshenlong - 3/12/11 at 10:58am
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #2 of 4
I would make the ID column an Autonumber in Access. You don't have to send the value to Access, it automatically increments. And to "write new data," you're going to have to use an Insert statement, not an Update statement.
Main System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X6 1090T MSI 890FXA-GD70 XFX Radeon 5850 16 GB Corsair XMS3 DDR3 1333 
Hard DriveOSMonitorPower
3 X 7200 RPM / 1 TB Win 7 Ultimate 64 2 x ASUS 23" VH232H 1080P SeaSonic X-650 
Case
Corsair 600T 
  hide details  
Reply
Main System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X6 1090T MSI 890FXA-GD70 XFX Radeon 5850 16 GB Corsair XMS3 DDR3 1333 
Hard DriveOSMonitorPower
3 X 7200 RPM / 1 TB Win 7 Ultimate 64 2 x ASUS 23" VH232H 1080P SeaSonic X-650 
Case
Corsair 600T 
  hide details  
Reply
post #3 of 4
As mentioned above, it either needs to be made an auto incremental number, or you need to start the table again and don't make it a primary key, an example of how to make it an auto number below.

Quote:
CREATE TABLE Users
(UserID INT IDENTITY(1,1) NOT NULL,
UserName VARCHAR(50) NOT NULL,
Password VARCHAR(50) NOT NULL)
(UserID INT IDENTITY(1,1) NOT NULL, Syntax being "field" "type" "function" (starting point, increment by)
post #4 of 4
Thread Starter 
I managed to figure it out guys. But thanks for the help anyway. Rep+
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › SQL and JAVA issues: Adding data to primary key?