【问题标题】:Updating data in MS Access DB using JTextField使用 JTextField 更新 MS Access DB 中的数据
【发布时间】:2013-03-20 20:04:23
【问题描述】:

我正在尝试使用 JTextField 更新我的数据库的内容,我有一个框架,其中包含有关特定记录的所有信息,然后我将正确或更改的数据输入到所有 JTextField,然后我尝试使用preparedStatement 执行 UPDATE 语句,但它似乎不起作用。我正在尝试这种方式:

.....
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String Base = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=DATABASE.mdb";
Connection con = DriverManager.getConnection(Base,"","");
PreparedStatement pst = con.prepareStatement("update table_name SET table_item1='?', table_item2='?', table_item3='?', table_item4='?', table_item5=?, table_item6=?, table_item7=? WHERE table_item8=?");
pst.setString(1, tf1.getText());
pst.setString(2, tf2.getText());
pst.setString(3, tf3.getText());
pst.setString(4, tf4.getText());
pst.setString(5, tf5.getText());
pst.setString(6, tf6.getText());
pst.setString(7, tf7.getText());
pst.setString(8, comboItem.getSelectedItem());
int res= pst.excecuteUpdate();
pst.close();
con.close();
......

在查询中我使用 ' ' 因为它是一个字符串组件,我在 pst.setString(5, tf5.getText()); 收到错误可能是因为它的整数值从那里开始,这只是一种预感。

【问题讨论】:

    标签: java ms-access sql-update jtextfield


    【解决方案1】:

    无需在准备好的语句中为字符串字段使用单引号 (') 并更正“更新”查询:

    PreparedStatement pst = con.prepareStatement("update table table_name SET table_item1='?'
    

    从所有 '?' 中删除 ''对于所有 string 字段都像这样:

    PreparedStatement pst = con.prepareStatement("update table table_name SET table_item1=?, 
    

    所以正确的查询变成了:

    PreparedStatement pst = con.prepareStatement("update table table_name SET table_item1=?, table_item2=?, table_item3=?, table_item4=?, table_item5=?, table_item6=?, table_item7=? WHERE table_item8=?");
    

    【讨论】:

    • [Microsoft][ODBC Microsoft Access Driver] 查询表达式“Pa_RaM000”中有语法错误(缺少运算符)。在 sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) 在 sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source) 在 sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source) 在 sun.jdbc.odbc.JdbcOdbcPreparedStatement .execute(Unknown Source) at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(Unknown Source)
    • 嘿正确的查询就像'更新表 table_name 设置...' 更正它
    • 仍然出现新错误 [Microsoft][ODBC Microsoft Access Driver] UPDATE 语句中的语法错误。
    • [Microsoft][ODBC Microsoft Access Driver] UPDATE 语句中的语法错误。在 sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) 在 sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source) 在 sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source) 在 sun.jdbc.odbc.JdbcOdbcPreparedStatement .execute(Unknown Source) at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(Unknown Source)
    • 显示错误,顺便说一句,您必须复制代码中的最后一个查询。
    猜你喜欢
    • 1970-01-01
    • 2017-12-22
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多