PreparedStatement错误:当前语句的至少一个参数未初始化

时间:2014-09-04 02:31:31

标签: java jdbc prepared-statement

我无法找出为什么我的executeUpdate()语句返回此异常:“当前语句的至少一个参数未初始化”。谁能发现我做错了什么? (我知道代码中存在过多的尝试/捕获)

public class MetadataRetriever2 extends SwingWorker<MediaMeta, MediaMeta> {

private final List<File> dataFlavorFileList;
private final DefaultTableModel activeModel;
private static MediaMeta trackMetaData;
private static final MediaPlayerFactory factory = new MediaPlayerFactory();

int row = 0;
private int dropLocationRow;

Connection connection = null;
PreparedStatement myPreparedStatement = null;
EmbeddedDataSource dataSource = null;

MetadataRetriever2(List dataFlavorFileList, DefaultTableModel activeModel, int dropLocationRow) {
    this.dropLocationRow = dropLocationRow;
    this.dataFlavorFileList = dataFlavorFileList;
    this.activeModel = activeModel;

}

@Override
protected MediaMeta doInBackground() throws Exception {

    for (File file : dataFlavorFileList) {

        if (MediaFileFilter.INSTANCE.accept(file)) {

            trackMetaData = factory.getMediaMeta(file.toPath().toString(), true);

        } else {

        }

        publish(trackMetaData);
    }
    return trackMetaData;
}

@Override
protected void process(List<MediaMeta> trackMetaData) {

    String sql = "INSERT INTO DEFAULT_LIBRARY (ARTIST, TITLE, ALBUM, TRACK_NUMBER, LENGTH, "
            + "DESCRIPTION, RATING,DATE ,SETTING, URL, LANGUAGE, NOW_PLAYING, PUBLISHER, ENCODED_BY,"
            + "ARTWORK_URL, TRACK_ID) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    dataSource = new EmbeddedDataSource();
    dataSource.setDatabaseName("audioPlayerDB");

    try {
        connection = dataSource.getConnection();
    } catch (SQLException ex) {
        System.out.println("connection failed");
    }

    try {
        myPreparedStatement = connection.prepareStatement(sql);
    } catch (SQLException ex) {
        System.out.println("preparedStatement failed: " + ex.getCause());
    }

    String[] oArray = new String[16];

    for (MediaMeta trackData : trackMetaData) {

        oArray[0] = trackData.getArtist();
        oArray[1] = trackData.getTitle();
        oArray[2] = trackData.getAlbum();
        oArray[3] = trackData.getTrackNumber();
        oArray[4] = String.valueOf(trackData.getLength());
        oArray[5] = trackData.getDescription();
        oArray[6] = trackData.getRating();
        oArray[7] = trackData.getDate();
        oArray[8] = trackData.getSetting();
        oArray[9] = trackData.getUrl();
        oArray[10] = trackData.getLanguage();
        oArray[11] = trackData.getNowPlaying();
        oArray[12] = trackData.getPublisher();
        oArray[13] = trackData.getEncodedBy();
        oArray[14] = trackData.getArtworkUrl();
        oArray[15] = trackData.getTrackId();

        try {

            myPreparedStatement.setString(1, oArray[0]);
            myPreparedStatement.setString(2, oArray[1]);
            myPreparedStatement.setString(3, oArray[2]);
            myPreparedStatement.setString(4, oArray[3]);
            myPreparedStatement.setString(5, oArray[4]);
            myPreparedStatement.setString(6, oArray[5]);
            myPreparedStatement.setString(7, oArray[6]);
            myPreparedStatement.setString(8, oArray[7]);
            myPreparedStatement.setString(9, oArray[8]);
            myPreparedStatement.setString(10, oArray[9]);
            myPreparedStatement.setString(11, oArray[10]);
            myPreparedStatement.setString(12, oArray[11]);
            myPreparedStatement.setString(13, oArray[12]);
            myPreparedStatement.setString(14, oArray[13]);
            myPreparedStatement.setString(15, oArray[15]);

            activeModel.insertRow(dropLocationRow, oArray);

            dropLocationRow++;

        } catch (SQLException ex) {
            Logger.getLogger(MetadataRetriever2.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            myPreparedStatement.executeUpdate();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }
}

}

1 个答案:

答案 0 :(得分:1)

您的陈述需要16个参数,而您的对象[]不会排列

myPreparedStatement.setString(14, oArray[13]);
myPreparedStatement.setString(15, oArray[15]);

应该是

myPreparedStatement.setString(14, oArray[13]);
myPreparedStatement.setString(15, oArray[14]);
myPreparedStatement.setString(16, oArray[15]);