无法将产品插入 ms access db

Posted

技术标签:

【中文标题】无法将产品插入 ms access db【英文标题】:Unable to insert products into 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 【参考方案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();

【讨论】:

以上是关于无法将产品插入 ms access db的主要内容,如果未能解决你的问题,请参考以下文章

MS Access 插入多行

由于“SQL 语法”错误,无法插入 MS Access [关闭]

插入查询中发生语法错误(MS Access)

MS Access 子表单数据条目在插入 SQL 时被锁定

使用 Python 在 MS Access 中传输数据

将日期插入 Ms-Access 数据库的问题