数据库MySql中的数据不正确

时间:2016-08-18 13:44:05

标签: java mysql jsp servlets encoding

我有一个Web应用程序,它从浏览器接收俄语语言数据并将字符串写入MySql数据库。数据库中的字符串看起来像Riemann而不是俄语字符。如何正确地将字符串写入数据库?

数据库编码为ÐÑдел инÑоÑмаÑионнÑÑ ÑеÑно。 Jsp页面具有编码utf8_general_ci

发送数据的jsp页面示例:

utf8

的Servlet

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>ОИТ</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body id="altbody">
<div id="wrapper-header">
  <div id="header">
    <h1>Отдел информационных технологий</h1>
  </div>
</div>
<div id="wrapper-menu">
  <div id="menu">
    <ul>
        <li><a href="index.jsp">Главная</a></li>
        <li><a href="oio.jsp">ОИО</a></li>
        <li><a href="ooz.jsp">ООЗ</a></li>
        <li><a href="oit.jsp">ОИТ</a></li>
        <li><a href="okkio.jsp">ОККИО</a></li>
    </ul>
  </div>
</div>
<div id="content">
    <form action="Controller" enctype="multipart/form-data" method="post">
        <input type="hidden" id="department" name="department" value="Отдел информационных технологий">
        <input name="computerProblem" id="computerProblem" placeholder="Укажите имя компьютера на котором возникла техническая проблема!" class="textbox" required /><br>
        <input name="problem" id="problem" placeholder="Опишите техническую проблему!" class="textbox" type="text" required /><br>
        <input type="file" id="file" name="file"><br>
        <input name="submit" class="button" type="submit" value="Отправить" />
    </form>
</div>
</body>
</html>

连接数据库:

@WebServlet(name="Controller",urlPatterns={"/Controller"})
@MultipartConfig(fileSizeThreshold=1024*1024,
        maxFileSize=1024*1024*5, maxRequestSize=1024*1024*5*5)
public class Controller extends HttpServlet {

    private Executor executor;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = System.getProperty("user.name");
        String department = new String(req.getParameter("department").getBytes("windows-1251"), "utf-8");
        String computerProblem = new String(req.getParameter("computerProblem").getBytes("windows-1251"), "utf-8");
        String descriptionProblem = new String(req.getParameter("problem").getBytes("windows-1251"), "utf-8");
        Part part = req.getPart("file");
        executor = new Executor();
        if (part.getSize() != 0){
            String type = part.getContentType();
            if (type.equals("image/png") || type.equals("image/jpeg")){
                String path = executor.saveImage(part, executor.getMaxIdFromDb());
                executor.writeProblemToDbWithImage(new User(userName, department), new Problem(computerProblem, descriptionProblem, path, new Date()));
                req.getRequestDispatcher("/ok.jsp").forward(req, resp);
            }else {
                req.getRequestDispatcher("/error.jsp").forward(req, resp);
            }
        }else{
            executor.writeProblemToDb(new User(userName, department), new Problem(computerProblem, descriptionProblem, new Date()));
            req.getRequestDispatcher("/ok.jsp").forward(req, resp);
        }

        if(executor != null) {
            executor.closeDb();
        }
    }
}

执行人:

public class ConnectionDb {
    private String hostName;
    private Properties properties;

    public ConnectionDb(String hostName, String userName, String password) {
        this.hostName = hostName;
        properties=new Properties();
        properties.setProperty("user", userName);
        properties.setProperty("password", password);
        properties.setProperty("useUnicode", "true");
        properties.setProperty("characterEncoding","utf8");
    }

    /*
        Метод возвращает подключение к базе данных
     */
    public Connection getConnection(){
        Connection connection = null;
        try{
            //Class.forName("org.postgresql.Driver");
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(hostName, properties);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return connection;
    }
}

1 个答案:

答案 0 :(得分:0)

?useUnicode=yes&characterEncoding=UTF-8添加到JDBC URL。注意拼写“UTF-8”,而不是'utf8'。这是与Java交谈,而不是MySQL。

你拥有的是Mojibake。如果更改连接不够,请参阅Trouble with utf8 characters; what I see is not what I stored,尤其是SELECT HEX...和“Mojibake”的讨论。