tags:

views:

374

answers:

4

I use prepareStatement() when the id is a key of my SQL table and it will be created by SQL and I want to use this statement :(what should I write instead of X in the first column of SQL table(reminder:SQL create it automatically)

File file = new File(pathFile);
            FileInputStream input = new FileInputStream(file);


            query = ("insert into birthtable VALUES(?,?,?,?,?,?,?,?)");
            pstmt = (PreparedStatement) conn.prepareStatement(query);
            pstmt.setInt(1,**X** )
            pstmt.setString(2, name);
            pstmt.setString(3, family);
            pstmt.setString(4, fatherName);
            pstmt.setString(5, mName);
            pstmt.setString(6, dOfBirth);
            pstmt.setString(7, pOfBirth);
            // Method used to insert a stream of bytes
            pstmt.setBinaryStream(8, input);




            pstmt.executeUpdate();

I have done what you all say but I have this exception??

java.sql.SQLException: Column count doesn't match value count at row 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
    at database.Manager.addBirth(Manager.java:76)
    at AdminGUI.AddNewBornInformation.submit(AddNewBornInformation.java:358)
    at AdminGUI.AddNewBornInformation.setButtonActionPerformed(AddNewBornInformation.java:282)
    at AdminGUI.AddNewBornInformation.access$800(AddNewBornInformation.java:28)
    at AdminGUI.AddNewBornInformation$9.actionPerformed(AddNewBornInformation.java:139)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6038)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
    at java.awt.Component.processEvent(Component.java:5803)
    at java.awt.Container.processEvent(Container.java:2058)
    at java.awt.Component.dispatchEventImpl(Component.java:4410)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
    at java.awt.Container.dispatchEventImpl(Container.java:2102)
    at java.awt.Window.dispatchEventImpl(Window.java:2429)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
+6  A: 

If you have specified the type of that column as int and auto-increment e.g.

`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,

then you don't need to supply any value at all, so you can start the prepared statement params at 1 with name.

 query = ("insert into birthtable (nameCol, familyCol, fatherNameCol, mNameCol, dOfBirthCol, pOfBirthCol, inputCol) VALUES(?,?,?,?,?,?,?)");
        pstmt = (PreparedStatement) conn.prepareStatement(query);
        pstmt.setString(1, name);
        pstmt.setString(2, family);
        pstmt.setString(3, fatherName);
        pstmt.setString(4, mName);
        pstmt.setString(5, dOfBirth);
        pstmt.setString(6, pOfBirth);
        // Method used to insert a stream of bytes
        pstmt.setBinaryStream(7, input);

Note that, as others have said, you must include the column names, whatever they might be.

Joel
we have 8 question marks ,and then I start with 1,so where is the value of eight question mark???
Johanna
you need to remove one - so there will only be seven
Joel
A: 

You do not set the id parameter in the prepared statement assuming you're using auto increment and the column names are valid:

query = ("insert into birthtable (name, family, fatherName, mName, dOfBirth, pOfBirth, input) VALUES(?,?,?,?,?,?,?)");
pstmt = conn.prepareStatement(query);
pstmt.setString(1, name);
pstmt.setString(2, family);
pstmt.setString(3, fatherName);
pstmt.setString(4, mName);
pstmt.setString(5, dOfBirth);
pstmt.setString(6, pOfBirth);
pstmt.setBinaryStream(7, input);

You should state the column names explicitly otherwise you depend on the order in the table create statement.

Thomas Jung
in your query ,you have 7 column with 8 question marks ?
Johanna
@Johanna - That's right. Fixed ? and idx #.
Thomas Jung
thanks ,I get it
Johanna
A: 

You don't need to specify it yourself, so you can just leave it away. But you should however specify the columns yourself, e.g.

"INSERT INTO tablename (columnname1, columnname2, columnname3) VALUES (?, ?, ?);"

otherwise the DB wouldn't know where to insert the values because there is one column missing.

BalusC
wooow ,I get it,how tricky is this!!!!
Johanna
A: 

My only 2 cents is that if you want a generated ID, you may call

pstmt.getGeneratedKeys( );

If it's going to return anything valid depends on your JDBC driver implementation though.

Alexander Pogrebnyak