mysql语法

来源:互联网 发布:dfa算法 编辑:程序博客网 时间:2024/06/04 19:43

SELECT * FROM t_student  WHERE  1=1 and stu_name LIKE %王%

[Err] 1064 - 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


注意字符是要带单引号的

SELECT * FROM t_student  WHERE  1=1 and stu_name LIKE '%王%'



if(stuName != null && !stuName.equals("")) {
System.out.println("stuName:"+stuName);
sql.append("and stu_name like ?");
paramList.add("'%"+stuName+"%'");
}


if(stuName != null && !stuName.equals("")) {
System.out.println("stuName:"+stuName);
sql.append("and stu_name like ?");
paramList.add("%"+stuName+"%");
}


USERNAME="root";
PASSWORD="root";
DRIVER="com.mysql.jdbc.Driver";
URL="jdbc:mysql://127.0.0.1:3306/imooc_pager";


  USERNAME="root";
PASSWORD="root";
DRIVER="com.mysql.jdbc.Driver";
URL="jdbc:mysql://127.0.0.1:3306/imooc_pager?useUnicode=true&characterEncoding=utf8";


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 'stu_name like '%王%'and gender = 1' at line 1

StringBuilder sql = new StringBuilder("select * from t_student where 1=1"); //??1=1

if(stuName != null && !stuName.equals("")) {
System.out.println("stuName:"+stuName);
sql.append("and stu_name = ?");
paramList.add("%"+stuName+"%");
}

if(gender == Constant.GENDER_MALE || gender == Constant.GENDER_FEMALE) {
System.out.println("gender:"+gender);
sql.append("and gender = ?");
paramList.add(gender);
}
System.out.println(sql);


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 'stu_name = 'h'and gender = 1' at line 1

StringBuilder sql = new StringBuilder("select * from t_student where 1=1"); // where1=1 后面得加空格跟其他条件隔开

StringBuilder sql = new StringBuilder("select * from t_student where 1=1 ");



类似文章:其他原因的

http://www.itnose.net/detail/982537.html


http://ask.csdn.net/questions/170059


http://blog.csdn.net/vanezuo/article/details/6686089


http://blog.sina.com.cn/s/blog_6d70461d01018xul.html



class Y {

public static void main(String[] args) {
String s = "hello";
if(s=="hello") {
System.out.println("==相等hello");
}
if(s.equals("hello")) {
System.out.println("equals相等hello");
}

String snew = new String("new");
if(snew=="new") {
System.out.println("==相等");
}
if(snew.equals("new")) {
System.out.println("equals相等new");
}

//==相等hello
//equals相等hello
//equals相等new
}
}

0 0