R
R
reset372014-11-12 20:48:14
MySQL
reset37, 2014-11-12 20:48:14

How to set up the correct encoding in MySQL + Tomcat?

There is a mysql database. The base encoding is UTF-8. There is a connection pool based on tomcat, when data is received through it, all Russian letters are replaced with '?'. Moreover, if you make a request to the database not from Tomcat, but from a regular java application without a pool, then everything is fine with the encoding. To be honest, I'm new to working with the database and I don't understand how it works, I took the code from some lesson.
Code that returns in the correct encoding:

Class.forName("com.mysql.jdbc.Driver");
String s = "jdbc:mysql://jdbc:mysql://ip:3306/name";
Connection c = DriverManager.getConnection(s, "root", "pass");
ResultSet rs = st.executeQuery("select * from menu");

Code that returns in the wrong encoding:
Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource dataSource = (DataSource)envContext.lookup("jdbc/testdb");
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from menu");

context.xml:
<Context>
    <Resource name="jdbc/testdb" auth="Container"
        type="javax.sql.DataSource" username="root" password="pass"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://ip:3306/name"
        maxActive="10" maxIdle="4" />
</Context>

web.xml:
<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/name</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Smirnov, 2014-11-13
@reset37

Try to specify the encoding in the connection url in context.xml like this:

url="jdbc:mysql://ip:3306/name?useUnicode=true&amp;characterEncoding=UTF-8"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question