2013-09-08 12 views
7

Próbowałem wstawić identyfikatora użytkownika (int) i nazwę użytkownika (ciąg) przy użyciu PreparedStatement użyciu JDBC stosując następujące metody:MySQLSyntaxErrorException gdy próbuje wykonać PreparedStatement

public boolean saveUser(int userId, String userName){ 
    boolean saveStatus = false; 
    try { 
     connection.setAutoCommit(true); 
     String sql = "insert into testtable values(?,?)"; 
     PreparedStatement statement = connection.prepareStatement(sql); 
     statement.setInt(1, userId); 
     statement.setString(2, userName); 
     saveStatus = statement.execute(sql); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    }finally{ 
     Connector.closeConnections(); 
    } 
    return saveStatus; 
} 

otrzymuję następujący StackTrace:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in 
    your SQL syntax; check the manual that corresponds to your MySQL server version 
    for the right syntax to use near '?,?)' at line 1 

Co robię źle?

+1

To nie ma sensu, ponieważ błąd "użyj w pobliżu"? Gdzie id =? "nie jive z instrukcją SQL. – OldProgrammer

+0

Pokaż nam instrukcję tworzenia tabeli dla testtable –

+0

utworzyć tabelę testową testową (id liczba całkowita, nazwa char (50)); – bhattedon

Odpowiedz

13

Spróbuj to powinno działać:

try { 
     connection.setAutoCommit(true); 
     String sql = "insert into testtable values(?,?)"; 
     PreparedStatement statement = connection.prepareStatement(sql); 
     statement.setInt(1, userId); 
     statement.setString(2, userName); 
     saveStatus = statement.execute(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
    return saveStatus; 
} 

PreparedStatement jest skompilowany stwierdzenie. nie musisz podawać ciągu sql podczas wykonywania.

+1

Zajęło mi godzinę, aby dowiedzieć się, że ** MUSISZ ** przekazać kwerendę sql do 'statement.execute()' podczas używania 'PreparedStatement'. Jest to naruszenie [Design by contract] (http://en.wikipedia.org/wiki/Design_by_contract) – Abbas

5

Istnieje podstawowy błąd w kodzie.

Po PreparedStatement jest tworzony, musimy zadzwonić preparedStatement.executeUpdate(); się przechodząc z SQL ponownie

Oto pełny kod do Twojego pytania.

import java.sql.Connection; 
import java.sql.Driver; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.SQLException; 


public class SampleMysql { 

    public static Connection getConnection() throws SQLException { 
     Driver drv = new com.mysql.jdbc.Driver(); 
     DriverManager.registerDriver(drv); 
     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","sample", "sample"); 
     return con; 
    } 

    public static int saveUser(int userId, String userName){ 
     int saveStatus = 0; 
     Connection connection = null; 
     try { 
      connection = getConnection(); 
      connection.setAutoCommit(true); 
      String sql = "INSERT INTO testtable VALUES(?,?)"; 
      PreparedStatement preparedStatement = connection.prepareStatement(sql); 
      preparedStatement.setInt(1, userId); 
      preparedStatement.setString(2, userName); 
      saveStatus = preparedStatement.executeUpdate(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     }finally{ 
      if (connection != null) { 
       try { 
        connection.close(); 
       } catch (SQLException e) { 

        e.printStackTrace(); 
       } 
      } 
     } 
     return saveStatus; 
    } 

    public static void main(String[] a) { 
     System.out.println(saveUser(1, "sample")); 
    } 
} 

Skrypt tabeli podano poniżej.

CREATE TABLE testtable(user_ID INT(10), user_name VARCHAR(10)); 
+0

Próbowałem również użyć 'executeUpdate()', ale to nie zadziałało – bhattedon

+0

Czy możesz uruchomić ten sam program co dałem? Kod jest testowany. – SmartTechie

+0

Czy możesz uruchomić ten sam program, który podałem? Kod jest testowany. Ilekroć jest dostępny fragment kodu, spróbuj porównać kod. Tak więc, znajdziesz różnicę. W kodzie powiedziałem, użyj metody executeUpdate(). Jeśli to zauważysz, nie przekazuję ponownie sql. – SmartTechie

Powiązane problemy