【发布时间】: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