尝试将数据插入数据库时出现java常规错误
我试图将数据插入数据库,但我似乎无法弄清为什么会出现一般性错误
关于如何克服这个问题的任何建议
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 楼答案
错误
您将
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 + "'
祝你好运