try{
Connection db = ConDB.conToDB();
Statement stmt = db.createStatement();
ResultSet rs =stmt.executeQuery("select * from room where internet ='"+internet+"'");
//ResultSet rs =stmt.executeQuery("select * from room where type ='"+type+"' and bed='"+bed+"' and position ='"+position+"' and internet ='"+internet+"' and breakfast='"+breakfast+"'");
while(rs.next()){
JSONObject room = new JSONObject();
room.put("roomId",rs.getString("roomid"));
room.put("roomType", rs.getString("type"));
room.put("bedType", rs.getString("bed"));
room.put("position", rs.getString("position"));
room.put("internet", rs.getBoolean("internet"));
room.put("breakfast", rs.getBoolean("breakfast"));
room.put("price", rs.getString("price"));
roomDataArray.add(room);
}
if(roomDataArray.size()==0){
result = "Sorry!";
}else{
result = roomDataArray.toString();
}
db.close();
}catch(Exception e){
e.printStackTrace();
}其中type,bed都是中文的。
我在mysql command line里面可以使用中文:
mysql> select * from room where type ="标准房" and bed="双人床" and position="东
" and internet ="1" and breakfast ="0";
+----------+--------+--------+-------+----------+----------+----------+---------
--+------+
| roomid | type | bed | price | discount | position | internet | breakfas
t | note |
+----------+--------+--------+-------+----------+----------+----------+---------
--+------+
| 天字一号 | 标准房 | 双人床 | 300 | 1 | 东 | 1 |
0 | NULL |
+----------+--------+--------+-------+----------+----------+----------+---------
--+------+
就是在java中不行,为什么啊。但是如果使用internet作为条件,可以搜索而且显示不乱码,正确的。
所以有什么解决方法?
谢谢
Connection db = ConDB.conToDB();
Statement stmt = db.createStatement();
ResultSet rs =stmt.executeQuery("select * from room where internet ='"+internet+"'");
//ResultSet rs =stmt.executeQuery("select * from room where type ='"+type+"' and bed='"+bed+"' and position ='"+position+"' and internet ='"+internet+"' and breakfast='"+breakfast+"'");
while(rs.next()){
JSONObject room = new JSONObject();
room.put("roomId",rs.getString("roomid"));
room.put("roomType", rs.getString("type"));
room.put("bedType", rs.getString("bed"));
room.put("position", rs.getString("position"));
room.put("internet", rs.getBoolean("internet"));
room.put("breakfast", rs.getBoolean("breakfast"));
room.put("price", rs.getString("price"));
roomDataArray.add(room);
}
if(roomDataArray.size()==0){
result = "Sorry!";
}else{
result = roomDataArray.toString();
}
db.close();
}catch(Exception e){
e.printStackTrace();
}其中type,bed都是中文的。
我在mysql command line里面可以使用中文:
mysql> select * from room where type ="标准房" and bed="双人床" and position="东
" and internet ="1" and breakfast ="0";
+----------+--------+--------+-------+----------+----------+----------+---------
--+------+
| roomid | type | bed | price | discount | position | internet | breakfas
t | note |
+----------+--------+--------+-------+----------+----------+----------+---------
--+------+
| 天字一号 | 标准房 | 双人床 | 300 | 1 | 东 | 1 |
0 | NULL |
+----------+--------+--------+-------+----------+----------+----------+---------
--+------+
就是在java中不行,为什么啊。但是如果使用internet作为条件,可以搜索而且显示不乱码,正确的。
所以有什么解决方法?
谢谢
然后把数据库删除重新建个。
如果是,要在Mysql instance config wizard中,将语言设置为gbk,
然后将你的数据库和表都删掉重新建;
在连接数据库的时候,要指定语言,如“jdbc\:mysql\://localhost\:3306/exam?useUnicode\=true&characterEncoding\=GBK”。