The SQL statement contains Chinese characters and cannot be queried.

Source: Internet
Author: User

When I was writing JDBC, I met the problem that the SQL statement cannot run because it contains Chinese characters. So I put this sentence in the mysql client for testing and the result passes, I guess it's not a matter of character encoding. It's because the PreparedStatement interface does something I don't know that causes this problem. And is generally used directly? The question mark replaces the dynamic query requirement and must be enclosed in double quotation marks on the client. So I tested it and wrote the SQL statement directly to run it. The result is OK, only two escape characters \ "are added \". Next I will send the code,

  1. PackageOop. hu. ytu. dao;
  2. ImportJava. SQL. Connection;
  3. ImportJava. SQL. PreparedStatement;
  4. ImportJava. SQL. ResultSet;
  5. ImportOop. hg. ytu. beans. LoginBean;
  6. ImportOop. hg. ytu. utils. JdbcUtils;
  7. /** 
  8. * Process user login requests 
  9. * @ Author Administrator 
  10. */
  11. Public ClassLoginDomain {
  12. PublicLoginBean select (String tableName, String username ){
  13. Connection con =Null;
  14. PreparedStatement pt =Null;
  15. ResultSet rs =Null;
  16. LoginBean bean =NewLoginBean ();
  17. Try{
  18. Username ="\""+ Username +"\"";
  19. Con = JdbcUtils. getConnection ();
  20. String SQL ="Select name, password from"+ TableName +"Where name ="+ Username +"";
  21. Pt = con. prepareStatement (SQL );
  22. // Pt. setString (1, tableName );
  23. // Pt. setString (2, u );
  24. Rs = pt.exe cuteQuery ();
  25. While(Rs. next ()){
  26. Bean. setUsername (rs. getString ("Name"));
  27. Bean. setPassword (rs. getString ("Password"));
  28. }
  29. }Catch(Exception e ){
  30. Throw NewRuntimeException (e );
  31. }Finally{
  32. JdbcUtils. free (rs, pt, con );
  33. }
  34. ReturnBean;
  35. }
  36. }

Username ="\""+ Username +"\"".

String SQL ="Select name, password from"+ TableName +"Where name ="+ Username +"";

This sentence is important. You must not use the string processing method provided by PreparedStatement. You must splice the string directly to run the statement. In this way, the processing of Chinese characters will be okay. This is a tangle of me. It took a sigh of relief!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.