我正在尝试将数据插入到数据库中,而我似乎无法深入了解为什么会出现一般性错误。
关于如何克服这个问题的任何建议。
Class.forName(driverName);
connection = DriverManager.getConnection(SourceURL, user, password);
int rowAdded;
Object typeId = comboKeys.getSelectedItem();
String typeIdString = typeId.toString();
int typeIdInt = Integer.parseInt(typeIdString);
String animalIDString = txtAnimalID.getText();
int animalID = Integer.parseInt(animalIDString);
System.out.println(animalID);
String name = txtName.getText();
Statement statement = connection.createStatement();
String queryString = "INSERT INTO Animal (animalID, name, typeIDForeign) VALUES (" + animalID + ", '" + name + "', '" + typeIdInt + "')";
rowAdded = statement.executeUpdate(queryString);
connection.close();
亲切的问候
阿里安
答案 0 :(得分:1)
你有typeIdInt
作为Integer
,但是在sql查询中,你将它描述为字符串(因为你用单引号括起来)
String queryString = "INSERT INTO Animal (animalID, name, typeIDForeign) VALUES (" + animalID + ", '" + name + "', '" + typeIdInt + "')";
应该是:
String queryString = "INSERT INTO Animal (animalID, name, typeIDForeign) VALUES (" + animalID + ", '" + name + "', " + typeIdInt + ")";
看看" + typeIdInt + "
。你有'" + typeIdInt + "'