我正在尝试更新我的数据库,但我的代码中出现了一些语法错误..请帮助..我尝试将其更改为“st.execute(statement);”和“st.executeUpdate(statement);”但错误仍然存在。 这是我的代码......
public void editBook(String inputTitle, String[] newBookInfo)
{
boolean result = false;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:database");
st = con.createStatement();
rs = st.executeQuery("select * from library");
while(rs.next())
{
if(boyerMoore(rs.getString("Title").toLowerCase(), inputTitle.toLowerCase()))
{
isbn = rs.getString("ISBN");
statement = String.format("UPDATE library SET ISBN = '%s', Title = '%s', Author = '%s', Publisher = '%s', Published Year = '%s', Available Copies = '%s', Total Copies = '%s', WHERE ISBN = '%s'", newBookInfo[0], newBookInfo[1], newBookInfo[2], newBookInfo[3], newBookInfo[4], newBookInfo[5], newBookInfo[6], isbn);
st.executeQuery(statement);
rs.close();
st.close();
con.close();
JOptionPane.showMessageDialog(null, "Edit Succes", "Succes", JOptionPane.PLAIN_MESSAGE);
result = true;
}
}
if(!result)
JOptionPane.showMessageDialog(null, "\"" + inputTitle + "\" not Found in the Library", "Error", JOptionPane.ERROR_MESSAGE);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
这是完整的堆栈跟踪..
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in UPDATE statement.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)
at dataBase.editBook(dataBase.java:161)
at mainFrame.actionPerformed(mainFrame.java:177)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:1)
删除','之前
, WHERE
UPDATE库SET ISBN ='%s',Title ='%s',作者='%s',发布者='%s',发布年份='%s',可用份数='%s',总份数='%s'WHERE ISBN ='%s'“,newBookInfo [0],newBookInfo [1],newBookInfo [2],newBookInfo [3],newBookInfo [4],newBookInfo [5],newBookInfo [6] ,isbn
答案 1 :(得分:1)
括起包含空格的字段名称:[Published Year]
; [Available Copies]
; [Total Copies]
。无论您是继续构建UPDATE
语句还是切换到参数查询,您都必须将这些名称括起来。
如前所述,在WHERE
...
[Total Copies] = '%s', WHERE ISBN
^
如果在进行这些更改后出现“类型不匹配”错误,请检查目标字段的数据类型。目前,您的语句正在为这些字段提供文本值。如果有任何数字而不是文本,请删除更新值周围的单引号。
答案 2 :(得分:1)
有些事情搞混了。
Connection con = DriverManager.getConnection("jdbc:odbc:database");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT Title, ISBN FROM library");
PreparedStatement statement = con
.prepareStatement("UPDATE library "
+ "SET ISBN = ?, "
+ "Title = ?, "
+ "Author = ?, "
+ "Publisher = ?, "
+ "[Published Year] = ?, "
+ "[Available Copies] = ?, " // [...] needed.
+ "[Total Copies] = ? " // No comma.
+ "WHERE ISBN = ?");
while (rs.next()) {
if (boyerMoore(rs.getString("Title").toLowerCase(),
inputTitle.toLowerCase())) {
isbn = rs.getString("ISBN");
statement.setString(1, newBookInfo[0]);
statement.setString(2, newBookInfo[1]);
statement.setString(3, newBookInfo[2]);
statement.setString(4, newBookInfo[3]);
statement.setString(5, newBookInfo[4]);
statement.setString(6, newBookInfo[5]);
statement.setString(7, newBookInfo[6]);
// statement.setInt(7, Integer.parseInt(newBookInfo[6]));
statement.setString(8, isbn);
statement.executeUpdate();
JOptionPane.showMessageDialog(null, "Edit Succes",
"Succes", JOptionPane.PLAIN_MESSAGE);
result = true;
}
}
rs.close(); // Here.
st.close();
statement.close();
con.close();
答案 3 :(得分:0)
在where
子句之前删除逗号。
另请尝试使用PreparedStatement
而不是Statement
以这种方式用于PreparedStatement
public void editBook(String inputTitle, String[] newBookInfo)
{
boolean result = false;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:database");
PreparedStatement pt=con.prepareStatement("select * from library")
//st = con.createStatement();
//rs = st.executeQuery("select * from library");
rs=pt.executeQuery();
while(rs.next())
{
//your codes
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
答案 4 :(得分:0)
使用两个不同的Statement对象,一个用于Read另一个用于更新。或者关闭现有语句并重新实例化。