首页 > 解决方案 > 另一个类中的 SQL 更新

问题描述

感谢您帮助我解决我的问题。因此,我尝试使用名为 Functions 的类中的方法插入 SQL 更新,主程序没有给出任何错误,并且每当我使用该函数时,刷新后它不会更改 SQL 表中的任何内容。

这是我在类 Functions 中的 updateSQL 方法:

public static void updateSQL(String sql, String updatenames[]) 
{
    Connection connected = null;
    PreparedStatement prepared = null;
    connected = connexionmysql.connexionDB();
    try 
    {
        prepared = connected.prepareStatement(sql);
        for(int i = 1; i < updatenames.length+1 ; i++) {
            prepared.setString(i, updatenames[i-1]);
        }
        prepared.execute();
    }
    catch (SQLException e) 
    {
        e.printStackTrace();
    }

}

这是我在主类中调用此函数的方式:

String datan[] = { inputPASS, type1, inputEMAIL, inputNAME };
                    Functions.updateSQL("UPDATE users SET userPASS = ? ,userTYPE = ? ,userEMAIL = ? WHERE userNAME = ? ", datan);

标签: javasqlclassmethods

解决方案


我确实解决了这个问题。我编写的函数按预期工作,但问题在于我调用该方法的方式。

我没有以静态方式调用该函数,而是以“通常”的方式调用它,因此 Eclipse 在该行中没有给我任何错误。


推荐阅读