7

I am making a Library Management System, I am trying to add arabic support in my project but I am facing difficulties as follows,

  1. when inserting data in SQL through Query i.e Insert into book_info values(25, N'جاوا', N'Author',N'Publisher',2014,N'Subject',50,N'Language','Latest',5) The List of Columns in book_info is as follows

    • Book ID(int)
    • Book Title(NVarchar(50))
    • Author(NVarchar(50))
    • Publisher(NVarchar(50))
    • Publish Year(int)
    • Subject(NVarchar(50))
    • Price(int)
    • Language(NVarchar(50))
    • Edition(NVarchar(50))
    • Part(int)

    Therefore when I execute this query through my Project, SQL Management Studio Shows some Encoded Characters like 'مجتبیٰ' in place of Arabic Characters, and not only in SQL Management Studio but also when Selecting data in project. Note: these values in query are taken from textfield through .getText() function

  2. What if we just skip the first problem by executing the query in SQL Management Studio, by doing this the first problem is temporarily resolved as SQL Management Studio is showing arabic characters when selecting the data but again when we select it from our software and show it in Textfield by Getting data from ResultSet like "TextF.setText(ResultS.getNString())" it shows "??????" in the text field in place of Arabic Characters.

ashokramcse
  • 2,841
  • 2
  • 19
  • 41
MMujtabaRoohani
  • 483
  • 4
  • 19
  • I can't reproduce the problem which isn't surprising. SQL Server's nvarchar needs no special treatment to store any Unicode character. The problem is always a conversion when loading or reading the data, typically by trying to "help" the database by hard-coding the wrong codepage. Where is the code that inserts data to the database or reads it? Are you *absolutely* sure you are passing Unicode strings? Have you tried using parameterized queries so you don't have to create strings with the values? – Panagiotis Kanavos Dec 15 '14 at 12:20
  • The most likely culprits are the getText, setText methods. I suspect they assume the strings you pass are ASCII strings, resulting in conversion errors (which are represented by the `???` characters) – Panagiotis Kanavos Dec 15 '14 at 12:30
  • @PanagiotisKanavos Actually I am using a parameterized query not a hard coded query and I knew that Nvarchar needs no special treatment. I agree that most likely the culprits are getText and setText methods and also the Vector In which I am storing the data – MMujtabaRoohani Dec 16 '14 at 13:00
  • This thing I mentioned in Note that I am not hard coding but using getText methods for query – MMujtabaRoohani Dec 16 '14 at 13:01
  • but how are you sending the data to the database? Somewhere before reaching the table there *is* a conversion to ASCII, that's what causes the problem. Either `getText` returns ASCII (doubt it), your database statement is ASCII or the database driver sends the statement as ASCII. Please post the code you use to insert the data. You can also try using SQL Profiler to see what is actually sent to the server, debug your application to see what are the values of strings at each step. – Panagiotis Kanavos Dec 16 '14 at 13:29
  • Also, convert your statements to parameterized queries with nvarchar parameters. This will prevent any conversions *and* improve security and performance – Panagiotis Kanavos Dec 16 '14 at 13:29
  • can you please post a code to successfully connect to MSSQL through SQL Authentication and then inserting and retrieving data from SQL in Unicode Format from TextFields, since I have never made SSCCE and using netbeans so I am facing difficulty in making an SSCCE – MMujtabaRoohani Dec 16 '14 at 15:21
  • please provide me with the complete code as soon as possible so that I can award the bounty, I have only 2 hours left to award the bounty – MMujtabaRoohani Dec 16 '14 at 15:21
  • I have only 1 hour left to award the bounty, please reply – MMujtabaRoohani Dec 16 '14 at 15:56
  • You don't have to do anything different to connect to SQL Server. I can't tell you how to fix the code you already have because you haven't posted it – Panagiotis Kanavos Dec 16 '14 at 16:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67133/discussion-between-jprogrammer-and-panagiotis-kanavos). – MMujtabaRoohani Dec 17 '14 at 15:00

2 Answers2

1

Your database definition, query, and SQL instance is fine. The issue you are having is with reading the string from the database into java.

I think Java is trying to read your nvarchar text as ASCII characters, when they are actually in Unicode. The result is garbled looking text. I think when you read the text from the database, you'll need to ensure you're reading as Unicode.

If you can post your code, I could probably pinpoint what is causing you issues.

ChrisG
  • 1,403
  • 13
  • 22
0

Try using the COLLATE statement like this:

CREATE TABLE #book_info
(
Book_ID int,
Book_Title NVarchar(50) COLLATE Arabic_ci_as,
Author NVarchar(50),
Publisher NVarchar(50),
Publish_Year int,
Subject NVarchar(50),
Price int,
Language NVarchar(50),
Edition NVarchar(50),
Part int
)

Insert into #book_info values(25, N'جاوا', N'Author',N'Publisher',2014,N'Subject',50,N'Language','Latest',5) 

SELECT *
FROM #book_info
Steve Ford
  • 7,433
  • 19
  • 40