gpt4 book ai didi

java - 类型PreparedStatement中的错误: The method setString(int, String)不适用于参数(int,Object)

转载 作者:行者123 更新时间:2023-12-02 07:30:56 25 4
gpt4 key购买 nike

我有以下类文件。如果我尝试将 vector 中的值插入数据库,则会显示以下错误。我想在这种情况下使用 Vector 并寻找如何解决此问题。

error : The method setString(int, String) in the type PreparedStatement is not applicable for the arguments (int, Object) 

下面是类代码:

public class ReadExcelFile {

public static void main(String[] args) {

String fileName = "C:\\excelFile.xls";
Vector dataHolder = ReadCSV(fileName);
printCellDataToConsole(dataHolder);
}

public static Vector ReadCSV(String fileName) {
Vector cellVectorHolder = new Vector();

try {
FileInputStream myInput = new FileInputStream(fileName);

POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

HSSFSheet mySheet = myWorkBook.getSheetAt(0);

Iterator rowIter = mySheet.rowIterator();

while (rowIter.hasNext()) {
HSSFRow myRow = (HSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
Vector cellStoreVector = new Vector();
while (cellIter.hasNext()) {
HSSFCell myCell = (HSSFCell) cellIter.next();
cellStoreVector.addElement(myCell);
}
cellVectorHolder.addElement(cellStoreVector);
}
} catch (Exception e) {
e.printStackTrace();
}
return cellVectorHolder;
}

public static void printCellDataToConsole(Vector dataHolder) {

for (int i = 0; i < dataHolder.size(); i++) {
Vector cellStoreVector = (Vector) dataHolder.elementAt(i);
// System.out.println(cellStoreVector);
for (int j = 0; j < cellStoreVector.size(); j++) {
HSSFCell myCell = (HSSFCell) cellStoreVector.elementAt(j);
String stringCellValue = myCell.toString();
System.out.print(stringCellValue + "\t\t");
}
System.out.println();
}
}


}

//下面是数据库查询

String sql = "INSERT INTO table_name(EMP_ID,FNAME, LNAME, CATEGORY, DEPARTMENT, Title, REASON, Manager, sDate, eDate, ID) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement pst1 = conn.prepareStatement(sql);
pst1.setString(1, cellStoreVector.get(0));
pst1.setString(2, cellStoreVector.get(1));
pst1.setString(3, cellStoreVector.get(2));
pst1.setString(4, cellStoreVector.get(3));
pst1.setString(5, cellStoreVector.get(4));
pst1.setString(6, cellStoreVector.get(5));
pst1.setString(7, cellStoreVector.get(6));
pst1.setString(8, cellStoreVector.get(7));
pst1.setString(9, cellStoreVector.get(8));
pst1.setString(10, cellStoreVector.get(9));
pst1.setString(11, "555"); //Hardcoded for testing.
pst1.execute();

最佳答案

您的 vector 被定义为保存对象

Vector#get 将返回一个Object

PreparedStatement#setString 方法需要一个 String,这是一个简单的类型不匹配问题。

您可以声明您的Vector,以便它仅包含String...

Vector<String> myVector = new Vector<String>(25);

...但我认为你不能从你的例子中...

您可以将Vector中的值转换为String

pst1.setString(1, (String)cellStoreVector.get(0));

但这很危险,因为直到运行时您才会发现 Vector 不包含 String 元素

或者您可以让 PreparedStatement 处理它...

pst1.setObject(1, cellStoreVector.get(0));

关于java - 类型PreparedStatement中的错误: The method setString(int, String)不适用于参数(int,Object),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12926778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com