JDBC Fuzzy query and preparestatement Chinese garbled solution

Source: Internet
Author: User

1. Recently, because of writing the Java EE version of the bookstore management system, encountered the problem of fuzzy query, because I query the keyword is Chinese, resulting in the beginning of a lot of problems, ResultSet rs-> such as: SELECT * from the book Where bookname = ' Operating system '; This statement can be found in the MySQL database, but Rs.next () is always not false; it is strange that the only reason I can think of is the Chinese garbled problem;

Is the format you want to encode when you write the URL:---. URL = "Jdbc:mysql://localhost:3306/bookshopping?characterencoding=utf8"; This URL is ct = drivermanager.getconnection (URL, USER, PASSWORD); I believe I don't have to say more. In this way, the Chinese query statement can be used normally;

2. Solve the problem of fuzzy query;

The fuzzy query statement is generally:select * from the book where the BookName like '% operating system% '; But if this is true, it is absolutely not found, and will error (the content of the specific error I do not explain); then the solution is: Use a wildcard character?

SELECT * from book where bookname?;

Then use the SetString method of preparestatement;

Like what:

String str = "operating system";

Ps.setstring (1, "%" +str + "%"); it's going to work out.

JDBC Fuzzy query and preparestatement Chinese garbled solution

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.