这样写,
String sql = "INSERT INTO te (ab) VALUES (?)";
preStmt=conn.prepareStatement(sql);
preStmt.setString(1,"asd");执行SQL时发生异常: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 若换一下,不用问号就没问题String sql = "INSERT INTO te (ab) VALUES ('asd')";
preStmt=conn.prepareStatement(sql);
为什么啊,不用问号prepareStatement就没多大意义了,错在哪里?

解决方案 »

  1.   

    这样写也不行,String sql = "INSERT INTO te (?) VALUES ('asd')";
    preStmt=conn.prepareStatement(sql);
    preStmt.setString(1,"ab");一用问号就异常,我弄一天了,baidu 和 google 都查遍了,都没明白哪儿出错了,望大家能帮帮我
      

  2.   

    1楼的写法肯定不行,顶楼的应该可以的。异常看起来好像preStmt.setString(1,"asd");没起作用一样。
      

  3.   


    import java.sql.*;
    public class JDBC {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123");
    String sql = "INSERT INTO test(name) VALUES (?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    pstmt.setString(1, "liuming");
    pstmt.executeUpdate();

    pstmt.close();
    conn.close();
    }
    }我这样写了一个,没问题的!
      

  4.   

    OK,想了一下,
    如果后面你执行的时候,用
    preStmt.executeUpdate(sql);
    是不行的,要用
    preStmt.executeUpdate();
      

  5.   

    pstmt.setString(1, "liuming");
            pstmt.executeUpdate();
    格式是这样的
      

  6.   

    是的,问题确实在executeUpdate书上是这样写的
    preStmt.executeUpdate(sql);没怀疑过这里,一直在前面找原因,找了一整天,我郁闷死了
    我又长见识了!谢谢大家!!