2017-04-30 44 views
0

java.sql.SQLException:參數索引超出範圍(1>參數數目,它是0)。異常在jsp中關於mysql更新與準備語句

我正在嘗試使用jsp進行動態web項目,當我嘗試使用PreparedStatment對象更新de mysql數據庫時,則出現此異常,我在很多線程中讀到了這意味着您沒有使用索引引用'?',但我認爲這不是我的情況,我希望這不是一個愚蠢的問題,但我無法找到我的錯誤。

這裏是代碼:

package classes; 

import java.sql.DriverManager; 
import java.sql.SQLException; 

import com.mysql.jdbc.Connection; 
import com.mysql.jdbc.PreparedStatement; 

public class DataUser { 
    private String username; 
    private String name; 
    private String surname; 
    private String email; 
    private String wallet; 
    private String password; 
    private String coin; 
    private Connection conexion; 
    private PreparedStatement preparedSt; 
    public DataUser(String username, String name, String surname, String email, String wallet, String password, String coin) throws ClassNotFoundException{ 

     this.username = username; 
     this.name = name; 
     this.surname = surname; 
     this.email = email; 
     this.wallet = wallet; 
     this.password = password; 
     this.coin = coin; 

     Class.forName("com.mysql.jdbc.Driver"); 

     try { 
      this.conexion = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/pruebajsp", "root", ""); 
      this.preparedSt = (PreparedStatement) conexion.prepareStatement("INSERT INTO USERS (USERNAME, NAME, SURNAME, EMAIL, WALLET, PASSWORD, INVESTMENT_COIN) VALUES ('?', '?', '?', '?', '?', '?', '?')"); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getSurname() { 
     return surname; 
    } 

    public void setSurname(String surname) { 
     this.surname = surname; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getWallet() { 
     return wallet; 
    } 

    public void setWallet(String wallet) { 
     this.wallet = wallet; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public String getCoin() { 
     return coin; 
    } 

    public void setCoin(String coin) { 
     this.coin = coin; 
    } 

    @Override 
    public String toString() { 
     return "DataUser [username=" + username + ", name=" + name + ", surname=" + surname + ", email=" + email 
       + ", wallet=" + wallet + ", password=" + password + ", coin=" + coin + "]"; 
    } 




    public void updateDB(){ 
     try{ 
      this.preparedSt.setString(1, this.username); 
      this.preparedSt.setString(2, this.name); 
      this.preparedSt.setString(3, this.surname); 
      this.preparedSt.setString(4, this.email); 
      this.preparedSt.setString(5, this.wallet); 
      this.preparedSt.setString(6, this.password); 
      this.preparedSt.setString(7, this.coin); 
      this.preparedSt.executeUpdate(); 
     }catch(SQLException e){ 
      e.printStackTrace(); 
     } 
    } 

} 

我在jsp文件裏做的是:

<% 
    DataUser user = new DataUser(request.getParameter("username"), request.getParameter("name"), request.getParameter("surname"), 
      request.getParameter("email"), request.getParameter("wallet"), request.getParameter("password"), 
      request.getParameter("coin")); 
    user.updateDB(); 
%> 

預先感謝您。

回答

0
values (?, ?, ?, ...) 

圍繞佔位符沒有單引號。否則,它們是文字字符串。

+0

OMG這只是一個愚蠢的錯誤,謝謝你和遺憾的浪費你的時間D: –