java.sql.SQLException: No angegebene Wert für parameter 5, aber die string-Länge ist 4, nicht 5

ich Plane zur Verwendung von yahoo finance csv-api zum generieren von url zu bekommen Aktie realtime Preis-und Zeit. Die url ist wie folgt http://download.finance.yahoo.com/d/quotes.csvs=%40%5YHOO,GOOG,MSFT,BAC&f=nsl1o&e=.csv, und es gibt eine Tabelle mit 4 Spalten.

Benutze ich jdbc-Daten zu speichern Mysql, aber es kommt immer eine Fehlermeldung:"Kein Wert angegeben für die parameter-5". Der code ist wie folgt:
public class Quote_Saver {
private void Verbindung(){
try {
Klasse.forName("com.mysql.jdbc.Treiber");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

private String retrieveQuote() {
    StringBuffer buf = new StringBuffer();
    try {
        URL page = new URL("http://download.finance.yahoo.com/d/quotes.csv?s=%40%5YHOO,GOOG,MSFT,BAC&f=nsl1o&e=.csv");
        String line;
        URLConnection conn = page.openConnection();
        conn.connect();
        InputStreamReader in = new InputStreamReader(conn.getInputStream());
        BufferedReader data = new BufferedReader(in);
        while ((line = data.readLine()) != null) {
            buf.append(line + "\n");
        }
    } catch (MalformedURLException mue) {
        System.out.println("Bad URL: " + mue.getMessage());
    } catch (IOException ioe) {
        System.out.println("IO Error:" + ioe.getMessage());
    }
    return buf.toString();
}

//use the retrieveQuote class , pass it to data in ConnectionToMysql
private void ConnectionToMysql(String data){
    StringTokenizer tokens = new StringTokenizer(data, ",");
    String[] fields = new String[4];
    for (int i = 0; i < fields.length; i++) {
        fields[i] = stripQuotes(tokens.nextToken());
    }

    connection();
    String host ="jdbc:mysql://localhost/yahoo";
    String username = "root";
    String password = ".....";
    try {
        Connection connection = DriverManager.getConnection(host, username, password);
        String query = "insert into `stocks`(?,?,?,?) values (?,?,?,?);";
        PreparedStatement statement = (PreparedStatement) connection.prepareStatement(query);
        statement.setString(1, fields[0]);
        statement.setString(2, fields[1]);
        statement.setString(3, fields[2]);
        statement.setString(4, fields[3]);
        statement.executeUpdate();
        connection.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

private String stripQuotes(String input) {
    StringBuffer output = new StringBuffer();
    for (int i = 0; i < input.length(); i++) {
        if (input.charAt(i) != '\"') {
            output.append(input.charAt(i));
        }
    }
    return output.toString();
}

public static void main (String args[])
{
    Quote_Saver qd= new Quote_Saver();
    String data = qd.retrieveQuote();
    qd.ConnectionToMysql(data);
}

}

Den Fehler

java.sql.SQLException: No value specified for parameter 5
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1086)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2594)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2569)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2366)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2350)
at Quote_Saver.ConnectionToMysql(Quote_Saver.java:68)
at Quote_Saver.main(Quote_Saver.java:89)

Aber klar, der string-Felder nur hat eine Größe von 4,so sieht meine Tabelle Spalten, also meine Frage ist wo ist der parameter 5? und wie löst man das? Ich bin neu in java,plz help me. Vielen Dank!

InformationsquelleAutor user3263529 | 2014-03-09

Schreibe einen Kommentar