我将函数转换为流动的应用程序,应用程序使用PreparedStatement pstmt插入二维字符串数组,当我启动它时,出现了类似于照片的问题。
import java.sql.PreparedStatement;
public static void main(String[] args){
java.sql.Connection conn = java.sql.DriverManager.getConnection(
url, user, password);要写入数据库的字符串数组。
String[][] sheetContent={{"100007"," 钢笔", "200", "xxx"},{"100020", "鞋子","700", "AAA"}};
int rows=2;
int columns = 4;
String sql = "INSERT INTO product(编号,商品名,单价,提供商) VALUES(?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
for(int r=0;r<rows;r++){
for(int c=0;c<columns;c++){
pstmt.setString(c, sheetContent[r][c]);
}
}
pstmt.executeUpdate();

发布于 2016-05-06 06:28:09
您应该从1开始preparedStatement索引,您正在做的是从0开始。
index =必须以索引整数1开头。
pstmt.setString(index,sheetContentr);
例如: pstmt.setString(1,sheetContentr);pstmt.setString(2,sheetContentr);
发布于 2016-05-06 01:30:18
您需要聪明地使用列索引:
PreparedStatement pstmt = conn.prepareStatement(sql);
for (int r=0; r < rows; r++) {
for (int c=0; c < columns; c++) {
int index = 1 + (r * columns) + c;
pstmt.setString(index, sheetContent[r][c]);
}
}
pstmt.executeUpdate();https://stackoverflow.com/questions/37062749
复制相似问题