Hi i am using Oracle DB to store string on Varchar2 column,
with using eclipselink my code is here,
pdescription = new String(this.description.getBytes("ISO-8859-9"));
sometimes its ok but, somtimes it only question marks, like that
it is taken "door" or "????"
I have column that is also string there is problem with that, their types are same both varchar2
Related
I am having a bit of trouble collating Arabic characters into MySQL database using Java.
I am using utf8 for all my tables and my database. Here are some screenshots from Mysql Workbench:
and
The String that I tried to collate is: عماد
It's worth mentioning that Arabic is coded over 2 bytes, so this is clearly not an issue of regular utf8 not being able to handle Arabic.
Code for connecting to the database:
String url = "jdbc:mysql://127.0.0.1:3306/mydatabase";
String user = "root";
String passwd = ".........";
String unicode= "?useUnicode=yes&characterEncoding=UTF-8";
setConnection((Connection) DriverManager.getConnection(url+unicode, user, passwd));
Code for inserting the value:
query3 = "INSERT INTO keyword (idkeyword, keyword) VALUES ("+keyWord.getId()+",'عماد')";
Statement state7 = (Statement) connection.createStatement();
state7.executeUpdate(query3);
The exception that I'm receiving:
java.sql.SQLException: Incorrect string value: '\xD8\xB9\xD9\x85\xD8\xA7...' for column 'keyword' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2482)
at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1552)
at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2607)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1480)
at controller.DataBaseAccess.saveProject(DataBaseAccess.java:285)
All help is greatly appreciated!
Assuming you are using Java 8 or greater, you could use Base64 to encode the arabic string and store it encoded in the database. This would prevent many other future errors with different type of strings in different languages. When reading the value from the database, you just Base64 decode it.
I am trying to run my java program, in which I have to transfer data from a DB to another one.
Well, the program works but one of the column field is named "public", and when I'm trying to transfer that data using jdbc, it crashes with the following statement:
"incorrect syntax near from keyword 'public'".
I've tried to load the column field as [public], (public), `public` works in SQL syntax but not in the program, since it has to check equivalence for "public", so "'public' is returning false.
Some code sample when i load the datas :
String publicDb = iArboFromExtranet.get(i).getString("public");
Some code sample when i transfer it :
private static final String PUBLIC = "public";
IArbo.set(PUBLIC, Strings.nullToEmpty(publicVar))
By the way, i cant rename the fields since i'm working for IT services industry. Its not our database
Since you're using a framework to generate your SQL, and you use a reserved word as a column name, you have a (tough?) choice to make:
Update the implementation of IArbo to quote reserved words (or all names).
Don't use IArbo.
Don't use a reserved word as a column name.
It looks like you're building SQL insert and/or update queries. Quote everything, so if the database field is MyField and the table is MyTable your query should have "MyTable" and "MyField" for this particular field. In Java you can build the string by escaping the double quote with a backslash:
queryString = "insert into \"MyTable\" (\"MyField\"...) values ('whatever'...)";
I'm trying to fill an SQLite database with data in my java program.
The data is read from an excel file using Apache POI. I have no trouble inserting the data into the db using normal methods.
However, when I check the database manually with the shell, all the Norwegian characters æ,ø,å are not displayed correctly. Whenever I fill out the database manually through the shell, they are displayed as they should.
Also, when printing out a java string in console containing these characters, they are displayed correctly.
The problem must occur when an action like this is performed:
String sql = "insert into db(name) values (æøå)";
stmt.executeUpdate(sql);
I have tried
byte[] b = sql.getBytes("utf-8");
sql = new String(b, "utf-8");
to no avail.
Any idea how to remedy the situation?
Thanks!
There is a very simple solution for you: Let Java and the SQLite driver do everything for you. You don't have to care about encodings and escaping of parameters.
How that is possible: Use a PreparedStatement:
String name = "æøå"
PreparedStatement prepStmt = conn.prepareStatement("insert into db(name) values (?)");
prepStmt.setString(1, name);
prepStmt.executeUpdate();
Furthermore this code fragment is secure against SQL injection attacks.
BTW: The second code fragment you posted is totally useless, it does nothing. Converting a String to byte[] and back to String does not change a single bit of the String.
I just converted from Oracle10g to Oracle11g. Suddenly some of the Oracle java classes that insert into date columns stopped working. I will list what is and is not working in both 10G and 11G
The Actual Error: ( Oracle 11g jdbc )
select sysdate from dual returns 2014-03-14 14:44:58.0
update users set expirationdate=to_date('2014-06-13','YYYY-MM-DD') where userId = 'NBKVLJN' ( successful )
update users set lastModifiedDate=sysdate where userId = 'NBKVLJN' ( successful )
update users set expirationdate='2014-06-13' where userId = 'NBKVLJN' ( failure )
err=java.sql.SQLDataException: ORA-01861: literal does not match format string
The very same sequence above works in all cases with Oracle 10g.
I was told to always use the TO_DATE() function when building a SQL statement string for Oracle.
I am actually surprised that option 4 worked in 10g! Perhaps it is a bug which has since been fixed?
Perhaps in the table in the 10g database, the expirationdate column was a VARCHAR2 instead of a DATE?
Ive been stunk in working with java and mysql these days.
The problem is, ive got a mysql database. There is a column in one table which shows the chinese city names. One collegue changed the db to utf8 for every character(connection, db, results, server and system) The consequence is that the data before the change didn't show correctly any more only if i set the %character% back to latin1. In either character set i can only retrive half the data correctly. Could you please help me how to solve the problem?
Ive tried to use java to solve the problem but it doesn't work.
String sql = "SELECT * FROM customer_addresses";
ResultSet result = query.executeQuery(sql);
while (result.next()) {
byte b[] = result.getBytes("city");
c = new String(result.getBytes("city"), "UTF-8");
}
For example: there is one city in db like this 乌é²æœ¨é½å¸‚
the java print: 乌�?木�?市
it should be:乌鲁木齐市
Thanks in advance
Default charset of your MySQL server is probably not UTF8. Try to execute the following SQL queries before getting data from the database:
SET NAMES utf8
and
SET CHARACTER SET utf8
Add characterEncoding=UTF-8 to the connection string, where you connect to the database. For example:
"jdbc:mysql://servername:3306/databasename?characterEncoding=UTF-8"
Incidentally, the data in the database appears to be broken. If you want the database to store 乌鲁木齐市, that's what should be in the table, not 乌é²æœ¨é½å¸.
Update: The problem with the how the data is stored in the database is easier to solve using database's own tools, not Java. For each table that stores text do this:
ALTER TABLE tablename CONVERT TO CHARACTER SET binary;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8;