首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java将数据插入Mysql数据库

java将数据插入Mysql数据库
EN

Stack Overflow用户
提问于 2016-05-06 01:20:24
回答 2查看 87关注 0票数 0

我将函数转换为流动的应用程序,应用程序使用PreparedStatement pstmt插入二维字符串数组,当我启动它时,出现了类似于照片的问题。

代码语言:javascript
复制
import java.sql.PreparedStatement;

public static void main(String[] args){

java.sql.Connection conn = java.sql.DriverManager.getConnection(
        url, user, password);

要写入数据库的字符串数组。

代码语言:javascript
复制
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();

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-06 06:28:09

您应该从1开始preparedStatement索引,您正在做的是从0开始。

index =必须以索引整数1开头。

pstmt.setString(index,sheetContentr);

例如: pstmt.setString(1,sheetContentr);pstmt.setString(2,sheetContentr);

票数 1
EN

Stack Overflow用户

发布于 2016-05-06 01:30:18

您需要聪明地使用列索引:

代码语言:javascript
复制
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();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37062749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档