日期:2014-05-18  浏览次数:20675 次

求助,连数据库查询的问题
request.setCharacterEncoding("gbk");
String telnum = request.getParameter("telnum");
String location = request.getParameter("location");
String remark = request.getParameter("remark");
int sex = Integer.parseInt(request.getParameter("sex"));

Class.forName("com.mysql.jdbc.Driver");
String url= "jdbc:mysql://localhost:3306/clientmanage?user=root&password=absolute";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from clientlist where telnum = " + telnum + " and location = " + location);if(rs.next()) {

stmt.executeUpdate("update clientlist set count = " + (rs.getInt("count") + 1) 
+ " where telnum = " + rs.getString("telnum") + " and location = " + rs.getString("location"));
stmt.close();

} else {
String sql = "insert into clientlist values (null, ?, ?, ?, ?, now(), 1)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, telnum);
pstmt.setString(2, location);
pstmt.setString(3, remark);
pstmt.setInt(4, sex);
pstmt.executeUpdate();

pstmt.close();
conn.close();
}

我加了" and location = " + location后总是报错:javax.servlet.ServletExceptioncom.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '(这里是乱码 )' in 'where clause',
是什么问题啊,location是中文。在mysql中这样查询都没有问题。

------解决方案--------------------
 location = " + location要用单引号引起来 改成 location = '" + location+"'"