gpt4 book ai didi

java - 我们如何使用 prepareStatement()?

转载 作者:行者123 更新时间:2023-11-29 06:25:37 26 4
gpt4 key购买 nike

当 id 是我的 SQL 表的键时,我使用 prepareStatement() 它将由 SQL 创建,我想使用这个语句 :(我应该写什么而不是 SQL 表第一列中的 X(提醒:SQL 自动创建)

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();

你们说的我都做了但我有这个异常(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)

最佳答案

如果您将该列的类型指定为 int 和自动递增,例如

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

然后您根本不需要提供任何值,因此您可以使用名称从 1 开始准备好的语句参数。

 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);

请注意,正如其他人所说,您必须包括列名,无论它们是什么。

关于java - 我们如何使用 prepareStatement()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1908652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com