I designed a java desktop application using jdbc technology to connect to mysql database.
But when I want to store a data in my db which is in persian language it saved like some ????
I tried creating the database with both
CREATE DATABASE 'db' CHARACTER SET 'utf8';
and
CREATE TABLE `Table1` (
[...]) DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
I tried every other COLLATEs but they seem not working properly.
What should I do?
Make sure the driver properties are set. Check that useUnicode is true, characterEncoding is "UTF-8". I assume you are using the Connector/J JDBC driver.
Refer to this answer.
A brief version of what the answer says is that you should add
?useUnicode=true&characterEncoding=UTF-8
To the end of JDBC connection URL.
For example:
jdbc:mysql://localhost:3306/db_name?useUnicode=yes&characterEncoding=UTF-8
Related
Whenever I try to add any japanese characters to the mysql database through jdbc, the characters are converted to question marks. I want to add those actual japanese characters. How can I do so?
There are similar questions on stackoverflow but none addresses the same issue as this.
PS. The mysql database is an AWS RDS database.
This has more to do with the encoding of your database than your actual SQL client. Your database should be configured to use an encoding that will allow Japanese characters. UTF-8 is recommended.
Specifying the encoding of your database is usually done during DB set-up time, not with your (Java) code. As you mentioned that you are using Amazon RDS I'm not sure what specific steps you should take. However, I usually do this per table on my MySQL set-ups. See here for the particular MySQL documentation.
Additionally, it may be that the Japanese characters are getting inserted fine but your viewing client (terminal, browser, etc.) is not configured to the proper encoding.
I followed the instructions in this blog to create a basic embedded database application.However, although checking the steps over and over and searching for the problem on the web , I'm still getting the exception : Table view blabla does not exists. Table seems to exists when I expand my driver's app schema. Netbeans version is 7.3.
The most common reason for a table-does-not-exist error with Derby is confusion over the location of the database. The database that you are accessing via Netbeans is probably different than the database that your application is accessing.
The location of your database is controlled by the JDBC connection URL, so if you provide some details about your JDBC connection URL, that might help others to help you.
I was using MySQL for my application. And i was using hibernate in my spring mvc application.
So all the tables are automatically created in my databse (MyApp).
I wrote a command
create database Myapp
in Mysql command prompt.
And when i ran my application, all the tables were created automatically inside Myapp.
In my hibernate config file , to connect to Mysql i have used the following url
jdbc:mysql://localhost:3306/Myapp
But now i want to change my database client to Oracle.
But i have seen in oracle the URL is given as
jdbc:oracle:thin:#localhost:1521:xe
So the database is not mentioned in the URL?
And also the command i wrote to create database in Mysql is not working in oracle.
How i can do this?
Following table lists down popular JDBC driver names and database URL.
RDBMS JDBC driver name URL format
MySQL com.mysql.jdbc.Driver jdbc:mysql://hostname/ databaseName
ORACLE oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:#hostname:portNumber:databaseName
DB2 COM.ibm.db2.jdbc.net.DB2Driver jdbc:db2:hostname:port Number/databaseName
Sybase com.sybase.jdbc.SybDriver jdbc:sybase:Tds:hostname: port Number/databaseNam
So the database is not mentioned in the URL?
It is mentioned. In your example jdbc:oracle:thin:#localhost:1521:xe, **xe** is the database service name similar to Myapp in your MySQL example.
Try to read the following links below
http://www.mkyong.com/jdbc/connect-to-oracle-db-via-jdbc-driver-java/
http://www.rgagnon.com/javadetails/java-0112.html
and download the drivers here and i think it contains some examples for you
http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
and for creating the database please watch this tutorial video
http://www.youtube.com/watch?v=3ZQ9ihJYRyM
I found this video which shows how to connect to access database :
http://www.youtube.com/watch?v=ujJ4H9RpC7c
My question is : Is it possible to create ODBC datasource programatically ?
or from command line or anything like that?
Thank you
It is not possible to create a windows ODBC DSN programmatically with pure Java. It is possible with C++ and other native approaches.
However, you can connect to an Access MDB file directly (via ODBC) using a JDBC URL of the form:
String jdbcUrl = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=c:/path/to/myaccessfile.mdb"
This way you do not need to have predefined DSN. You might also want to review the answers to this question:
How can I add a password to this JDBC:ODBC connection string that is trying to connect to an MS Access database
From the command line, you can use an utility named odbcconf.
I guess that if you need to do that programatically, you'll need to use WinAPI somehow.
I am developing an application connecting to a database in Java. The customer has SQL Server and I tried the SQLExpress version von Microsoft as long as we don't want to buy a licence for the developement time. Sadly, the SQL Server Express does not allow network access so we can't work over network on the same database and have to install the SQL Server Express server on every developer client.
Today morning I decided to switch to MySQL during the developement process. I created a MySQL database with UTF8 charset and exported the data into CSV files which I also converted to UTF8 and imported them.
I connect with Java with the JDBC driver and now got weird behavior during execution. The results completetly differ from the client connecting to SQL Server. I have written the SQL to Java glue code myselft and am not using a framework like Hibernate or JPA.
I guess that the problem has to do with character encoding. The source code files are - dont't hit me - encoded with CP1252, because I just started developing in Eclipse on Windows and trusted the default settings. I query the database getting a ResultSet object and then read out the data with the getter methods provided by the ResultSet. I don't do any character conversion during the data fetching.
The problem is now that I don't get cryptic not well encoded output but instead NullPointerExceptions and weird data handling. For example: I have written a method which compares Jobs (an object representing a Job with a name, id, cargo and all that stuff). The results during the runtime differ. Some Jobs are equal on SQL Server and some on MySQL but the SQL Server result is the correct one.
I just viewed the database properties and saw that the character encoding of the SQL Server is in ISO-Latin-1.
Thanks for your help and regards from Cologne,
Marco
I know little about MS SQL, but if the MS SQL data is latin1 encoded, importing it into a UTF-8 database on the mySQL end must fail and result in broken data.
I would try to get the data fixed first. Can you retry the process without converting the dump file, and a latin1 database in mySQL?
Plus, there seem to be notable differences in data types between mySQL and MS SQL:
Be careful when planning this: you want to match data types by capacity and not necessarily by name. For example: a MySQL VARCHAR can hold up to 255 characters, whereas a SQL Server VARCHAR can hold up to 4000 characters. In this case you would need to use a MySQL TEXT column type instead of VARCHAR.