//...public class Test_1{//....    public static void main(String args[]) throws Exception{

//...
        PreparedStatement pStmt = null; String sql = "SELECT id, name, password, age, birthday FROM user"
+ "WHERE name LIKE ?";

        String keyWord = "a";
       
pStmt = con.prepareStatement(sql); pStmt.setString(1, "%" + keyWord + "%");
//...
}
}
这个代码报错:Exception in thread "main" 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 'LIKE '%a%'' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2293)
at Test_1.main(Test_1.java:27)
应该是SQL语句的问题,应该如何改呢??? 还有 SQL 的 字符串拼接 有什么简单的学习资料吗??  就是很多引号的那个

解决方案 »

  1.   

    数据库连接的配置确定没有问题吗?
    like后面的?要加引号吧 -> '?'
      

  2.   


    数据库连接没有问题加上不对的, 会提示没有参数 错误信息:Exception in thread "main" java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3729)
    at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3713)
    at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4553)
    at Test_1.main(Test_1.java:26)
      

  3.   

    "'%" + keyword + "%'"  在双引号里面还要用 单引号 表示是字符串模糊匹配!
      

  4.   

    pStmt.setString(1, "%" + keyWord + "%");
    把1改为0试试,我也不懂
      

  5.   

    LIKE 后是一个字符串,字符串要放到 单引号里。
      

  6.   

    pStmt.setString(1, "%" + keyWord + "%");
    把1改成0就可以了!
      

  7.   


    还是报错 :
    Exception in thread "main" 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 'LIKE '\'%a%\''' at line 1"\'%" + keyword + "%\'"这样也错的 
      

  8.   

     = = 好像不行  数据库没有第 0 列吧Exception in thread "main" java.sql.SQLException: Parameter index out of range (0 < 1 ).
      

  9.   


    恩 这个好像我知道 但是我试过这几种1:  pStmt.setString(1, "'%" + keyWord + "%'");2:  pStmt.setString(1, "\'%" + keyWord + "%\'");3:  String keyWord = "'%a%'";4:   String keyWord = "\'%a%\'";5:   "..." + "WHERE name LIKE '?'";都有报错 这是我的程序(MySQL : 5.5.17) :
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.Date;
    import java.sql.ResultSet;public class Test_1{    public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
        public static final String DBURL = "jdbc:mysql://localhost:3306/lb";
        public static final String DBNAME = "root";
        public static final String DBPASSWORD = "admin";    public static void main(String args[]) throws Exception{

    Class.forName(DBDRIVER);
    Connection con = null;
    PreparedStatement pStmt = null;
    ResultSet rs = null;
    String sql = "SELECT id, name, password, age, birthday FROM user"
    + "WHERE name LIKE ?";
    String keyWord = "a";

    con = DriverManager.getConnection(DBURL, DBNAME, DBPASSWORD);
    pStmt = con.prepareStatement(sql);
    pStmt.setString(1, "%" + keyWord + "%");
    rs = pStmt.executeQuery();
    while(rs.next()){
        int id = rs.getInt(1);
        String name = rs.getString(2);
        String password = rs.getString(3);
        int age = rs.getInt(4);
        java.util.Date birthday = rs.getDate(5);
        System.out.print("ID:" + id + "    ");
        System.out.print("NAME:" + name + "    ");
        System.out.print("PASSWORD:" + password + "    ");
        System.out.print("AGE" + age + "   ");
        System.out.println("BIRTHDAY:" + birthday + "    ");
        System.out.println("---------------------------------");
            }
            pStmt.close();
    con.close();
        }
    }
    请问如何改正,  SQL语句的拼接有什么窍门不?? 有没有这方面的学习实例??  
      

  10.   

    like 是不能直接这么用的这个不知道算不算 jdbc 的疏忽, 总之 jdbc 不建议也不支持 like 用 ? 传递楼主用拼接字符串或者用 String.format 也能达到效果,而且效率应该也不差可参考http://bbs.blueidea.com/thread-2716081-1-1.htmlhttp://social.msdn.microsoft.com/Forums/zh-SG/sqldataaccess/thread/6187ef2a-ced3-41da-a484-b63b216ef7b4我也木有测试,不知道现在的 jdbc 行不行了
      

  11.   

    MassgeFormat.format("select * from employee where name = {0}", userName);
      

  12.   

    在WHERE前加一空格就好了
    LZ结贴吧