尝试将数据插入数据库时​​的常规错误

时间:2012-02-14 09:37:06

标签: java jdbc

我正在尝试将数据插入到数据库中,而我似乎无法深入了解为什么会出现一般性错误。

关于如何克服这个问题的任何建议。

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();

亲切的问候

阿里安

1 个答案:

答案 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 + "'

祝你好运!!!