String sql = "SELECT * FROM articles WHERE title LIKE ?"; PreparedStatement stmt = conn.prepareStatement(sql); stmt.setString(1, "%" + keyword + "%"); ResultSet rs = stmt.executeQuery();使用这段代码进行模糊查找时无法查找中文
时间: 2024-01-31 10:02:07 浏览: 226
可能是因为数据库的字符集不支持中文,可以尝试修改数据库的字符集为 utf8mb4。同时,在代码中也需要指定字符集为 utf8mb4,可以在连接数据库时加上以下语句:?useUnicode=true&characterEncoding=utf8mb4。修改后的代码如下:
String sql = "SELECT * FROM articles WHERE title LIKE ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "%" + keyword + "%");
ResultSet rs = stmt.executeQuery();
rs.close();
stmt.close();
conn.close();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf8mb4", "username", "password");
相关问题
@WebServlet("/ChangePasswordServlet")public class ChangePasswordServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String oldPassword = request.getParameter("oldPassword"); String newPassword = request.getParameter("newPassword"); // 验证旧密码是否正确 if (checkPassword(username, oldPassword)) { // 更新密码 updatePassword(username, newPassword); response.sendRedirect("success.jsp"); } else { response.sendRedirect("error.jsp"); } } private boolean checkPassword(String username, String password) { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; boolean result = false; try { conn = DBUtil.getConnection(); String sql = "SELECT * FROM user WHERE username=? AND password=?"; stmt = conn.prepareStatement(sql); stmt.setString(1, username); stmt.setString(2, password); rs = stmt.executeQuery(); result = rs.next(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, stmt, conn); } return result; } private void updatePassword(String username, String password) { Connection conn = null; PreparedStatement stmt = null; try { conn = DBUtil.getConnection(); String sql = "UPDATE user SET password=? WHERE username=?"; stmt = conn.prepareStatement(sql); stmt.setString(1, password); stmt.setString(2, username); stmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(null, stmt, conn); } }}具体解释一下各代码意思
这段代码是一个 Java Servlet,用于处理用户修改密码的请求。它接收来自客户端的 POST 请求,请求参数包括用户名、旧密码和新密码。如果旧密码验证成功,它会更新用户的密码,并重定向到成功页面;否则,它重定向到错误页面。
下面是对每个方法的解释:
1. `doPost` 方法:处理客户端 POST 请求,并根据请求参数调用相关方法验证旧密码和更新密码。
2. `checkPassword` 方法:验证用户输入的旧密码是否正确。它首先通过 `DBUtil` 工具类获取数据库连接,然后执行一个 SELECT SQL 查询,查找用户名和密码都匹配的用户。如果查询结果集有数据,则返回 `true`,否则返回 `false`。最后,关闭数据库连接和相关资源。
3. `updatePassword` 方法:更新用户的密码。它也通过 `DBUtil` 工具类获取数据库连接,然后执行一个 UPDATE SQL 语句,更新用户名对应的密码。最后,关闭数据库连接和相关资源。
需要注意的是,这段代码中的数据库连接和资源都需要在 finally 语句块中关闭,以确保资源得到释放。同时,为了防止 SQL 注入攻击,应该使用 PreparedStatement 对象来执行 SQL 语句,而不是直接拼接字符串。
优化这段代码CREATE TABLE user ( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) NOT NULL, password VARCHAR(50) NOT NULL); import java.sql.*; public class UserDAO { private final String url = "jdbc:mysql://localhost:3306/mydb"; private final String user = "username"; private final String password = "password"; public boolean register(String username, String password) { try (Connection conn = DriverManager.getConnection(url, user, password); PreparedStatement stmt = conn.prepareStatement( "INSERT INTO user (username, password) VALUES (?, ?)")) { stmt.setString(1, username); stmt.setString(2, password); int rows = stmt.executeUpdate(); return rows > 0; } catch (SQLException e) { e.printStackTrace(); return false; } } public boolean login(String username, String password) { try (Connection conn = DriverManager.getConnection(url, user, password); PreparedStatement stmt = conn.prepareStatement( "SELECT * FROM user WHERE username = ? AND password = ?")) { stmt.setString(1, username); stmt.setString(2, password); ResultSet rs = stmt.executeQuery(); return rs.next(); } catch (SQLException e) { e.printStackTrace(); return false; } } }
首先,建议将数据库连接池引入代码中,以避免每次调用数据库时都要重新连接。这将大大减少连接和关闭数据库的开销。
其次,建议使用预编译语句,以避免每次执行 SQL 语句时都要重新编译。这也将大大减少编译和执行 SQL 语句的开销。
最后,可以在数据库中添加索引以加快查询速度。在这个例子中,可以在 username 和 password 字段上创建索引。
下面是经过优化的代码:
import java.sql.*;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
public class UserDAO {
private DataSource dataSource;
public UserDAO() {
BasicDataSource ds = new BasicDataSource();
ds.setUrl("jdbc:mysql://localhost:3306/mydb");
ds.setUsername("username");
ds.setPassword("password");
ds.setInitialSize(5);
dataSource = ds;
}
public boolean register(String username, String password) {
try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(
"INSERT INTO user (username, password) VALUES (?, ?)")) {
stmt.setString(1, username);
stmt.setString(2, password);
int rows = stmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean login(String username, String password) {
try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(
"SELECT * FROM user WHERE username = ? AND password = ?")) {
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
阅读全文