On 24.07.10 05:35, Mark D. Johnson wrote:
Hi -
I have run into a problem trying to insert the Swedish vowels Å, Ä and
Ö into Varchar fields in a Derby database. I first noticed a problem
with the capital Å using ij to do the insert. When I did a select on
the field, the character Å was shown as a question mark. This is the
only problem when I use ij - the small å and the other characters are
shown correctly. I wrote a little program (listing below) to test
this outside of ij, and none of the characters appear correct. It
seems that the characters are being stored incorrectly, since the
output from ij shows the same characters from a record inserted with
the program as the program shows, and the program shows the correct
vowels (with the Å replaced by a ?) for records inserted using ij.
Can anyone help track down what is going wrong?
Hi Mark,
Your little program produces the expected output for me. I can see both
the lower and upper case versions of the three last letters in the
Swedish alphabet correctly (as depicted on [1]). I copied the program
from your mail and pasted it into a file using vim in my terminal.
The question mark suggests a problem with your terminal settings. Are
you sure you're using a locale that can display/represent those letters?
(I guess things could go wrong both on the OS side and on the Java side,
the machine I tested on happened to use nb_NO.UTF-8)
Strings in Derby is stored as UTF-8 on disk, and are represented as Java
chars when decoded or being inserted as a String/Reader. To rule out
problems with storing and fetching, you could look up the Unicode code
points and make sure you get back what you put in. When I did that, I
could see that the code points that went in and came back out were
consistent with the contents of the Unicode chart "C1 Controls and
Latin-1 Supplement" (for instance \u00D6 and \u00E4).
For completeness, which platform/OS are you seeing the trouble on?
Regards,
--
Kristian
[1] http://en.wikipedia.org/wiki/Swedish_alphabet
Thanks for any assistance -
Mark Johnson
import java.sql.*;
/**
*
* @author Mark
*/
public class TestClass {
public static void main (String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
} catch(java.lang.ClassNotFoundException e) {
System.err.println("Class not found");
} catch (java.lang.InstantiationException ie) {
System.err.println("could not create driver instance");
} catch (java.lang.IllegalAccessException iae) {
System.err.println("not allowed access to driver");
}
try {
conn = DriverManager.getConnection
("jdbc:derby:testSvenskaDb;create=true;territory=sv_SE;collation=TERRITORY_BASED:PRIMARY;");
} catch (Throwable e) {
System.err.println("Failed to connect to database");
}
try {
stmt = conn.createStatement ();
stmt.execute ("CREATE TABLE testtbl (str VARCHAR(10))");
} catch (Throwable e) {
System.err.println("create table failed");
}
try { ps = conn.prepareStatement("INSERT INTO testtbl VALUES
(?)");
ps.setString(1, "ÅåÄäÖö");
ps.executeUpdate ();
} catch (Throwable e) {
System.err.println("insert failed");
}
try { stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM testtbl");
while ( rs.next()) {
System.out.println (rs.getString(1));
}
} catch (Throwable e) {
System.err.println("query failed");
}
try { rs.close();
stmt.close();
conn.close();
DriverManager.getConnection("jdbc:derby:;shutdown=true");
} catch (SQLException se) {
if (se.getSQLState().equals("XJ015")) {
System.out.println("Database shut down normally");
} else {
System.err.println("Abnormal shutdown");
}
}
}
}