【问题标题】:Unable to insert products into ms access db无法将产品插入 ms access db
【发布时间】:2019-05-27 13:16:08
【问题描述】:

尝试将产品插入我的访问数据库时,我收到了许多不同的错误。如畸形字符串:)。用户缺少权限或找不到对象。当我尝试插入不同的产品时会出现不同的错误。

尝试重新创建数据库,调试到最后。

public boolean addNewProduct(Product product) {

    String Make = "";
    String Model = "";
    String Type = "";
    String Genre = "";
    String AttConsole = "";
    String Desc = "";

    if(product.getClass().getName().equals("Models.Game"))
    {
         Game game = (Game)product;
         Genre = String.valueOf(game.getGenre());
         AttConsole = String.valueOf(game.getAttributedConsole());
         Desc = String.valueOf(game.getDescription());
    }
    else if(product.getClass().getName().equals("Models.Console"))
    {
         Console console = (Console)product;
         Make = String.valueOf(console.getMake());
         Model = String.valueOf(console.getModel());
         Desc = String.valueOf(console.getDescription());
    }
    else
    {
        Peripheral peripheral = (Peripheral)product;
        Type = String.valueOf(peripheral.getType());
        Desc = String.valueOf(peripheral.getDescription());
    }

     try
    {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(connectionString);
        Statement stmt = conn.createStatement();
        stmt.executeUpdate("INSERT INTO Products (ProductName, Price, StockLevel, Description, Genre, AttributedConsole, Make, Model, Type) VALUES "
                + "('" + product.getProductName() + "','" + product.getPrice() + "','" + product.getStocklevel() 
                + "','" +  Desc + "','" + Genre + "','" + AttConsole + 
                "','"  + Make + "','" + Model + "'," + Type + ")");
        //sql statement to add new products to database
        conn.close();
        return true;
    }

     catch(Exception ex)
     {
          String message = ex.getMessage();    
          return false;
     }

}

ex = (net.ucanaccess.jdbc.UcanaccessSQLException) net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.4 意外令牌:) ex = (net.ucanaccess.jdbc.UcanaccessSQLException) net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.4 用户缺少权限或找不到对象:RAZOR

【问题讨论】:

  • SQL 中没有引用Type,这可能是原因。不要通过字符串连接来构建 SQL。正如您已经发现的那样容易出错并打开 SQL 注入攻击向量。将PreparedStatement 与参数一起使用。
  • 感谢您的回答。此刻正在为此苦苦挣扎。你能给我举个例子吗?干杯
  • 有一个关于prepared statements的教程:docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

标签: java jdbc netbeans ucanaccess


【解决方案1】:

不要使用字符串连接将列 values 插入 SQL 命令文本。搜索“SQL Injection”或“Little Bobby Tables”,了解有关为什么这是“坏事”™ 的更多信息。

改为使用PreparedStatement 来运行参数化查询,例如,

String sql = "INSERT INTO tableName (intColumn, textColumn) VALUES (?, ?)";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
    ps.setInt(1, 12345);
    ps.setString(2, "my text value");
    ps.executeUpdate();
}

【讨论】:

    猜你喜欢
    • 2019-08-05
    • 2013-05-06
    • 1970-01-01
    • 2010-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多