package com.sys;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class My {
private final static String jdbcdriver = "com.mysql.jdbc.Driver";
private final static String url = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8";
private Connection con =null;
public ResultSet findall(String filter){
PreparedStatement ps=null;
ResultSet rs=null;
try{
Class.forName(jdbcdriver);
con=DriverManager.getConnection(url);
String fil="";
if(filter!=null)fil="where"+" "+filter;
//打印SQL语句
System.out.println("select * from users "+fil);
ps=con.prepareStatement("select * from users "+fil);
rs=ps.executeQuery();
}
catch(SQLException e){
System.out.println(e.toString());
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
return rs;
}
public static void main(String[] args) {
My p = new My();
String username="mary";
p.findall("username="+"'"+username+"'");
}
}
此程序抛出异常
select * from users where username='mary'
java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='此程序的SQL语句试过在MYSQL可以查出结果.....
如果输入的是select * from users where userid=1这样就可以运行..但是换成string就会抛出异常..
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class My {
private final static String jdbcdriver = "com.mysql.jdbc.Driver";
private final static String url = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8";
private Connection con =null;
public ResultSet findall(String filter){
PreparedStatement ps=null;
ResultSet rs=null;
try{
Class.forName(jdbcdriver);
con=DriverManager.getConnection(url);
String fil="";
if(filter!=null)fil="where"+" "+filter;
//打印SQL语句
System.out.println("select * from users "+fil);
ps=con.prepareStatement("select * from users "+fil);
rs=ps.executeQuery();
}
catch(SQLException e){
System.out.println(e.toString());
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
return rs;
}
public static void main(String[] args) {
My p = new My();
String username="mary";
p.findall("username="+"'"+username+"'");
}
}
此程序抛出异常
select * from users where username='mary'
java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='此程序的SQL语句试过在MYSQL可以查出结果.....
如果输入的是select * from users where userid=1这样就可以运行..但是换成string就会抛出异常..
你设成jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gbk
你都试试吧,可能就是和连接字符串有关系。