I translate the function to the flowing application The application use PreparedStatement pstmt to insert String array of two dimention,when i make it start ,problems appeared like the photo
import java.sql.PreparedStatement;
public static void main(String[] args){
java.sql.Connection conn = java.sql.DriverManager.getConnection(
url, user, password);
the String array to write into databace.
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();
You need to be clever with how you use your column indices:
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();
you should start Index of preparedStatement from 1 , what you are doing is starting from 0.
index = must starts with index integer 1.
pstmt.setString(index, sheetContent[r][c]);
example: pstmt.setString(1, sheetContent[r][c]); pstmt.setString(2, sheetContent[r][c]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.