Java Queries
Java Queries

Reputation: 3

Unable to search Turkish Characters in SQL Server 2008 using Java code

I am trying to execute a database query which searches on Turkish charachter and returns the result.

The code works on searching English charachters but on turkish it fails to search the record.

Below is the code being used :

public static void main(String[] args) throws ClassNotFoundException,    SQLException {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
Connection conn = DriverManager.getConnection("jdbc:sqlserver://hostname;user=username;password=pwd;database=test;characterEncoding=UTF-8");    
System.out.println("test");
String returnCol1 = "NAME_Type_ID";
String tableName = "tablename";
String lookupCol1 = "NAME_TYPE";
String lookupData1 = "Göbek Adı";

Statement sta = conn.createStatement();

String myquery = "select "+returnCol1+" from "+tableName +" where "+lookupCol1+"='"+lookupData1+"';" ;

System.out.println(myquery);

ResultSet rs = sta.executeQuery(myquery);

while (rs.next()) {
    System.out.println("here");
    System.out.println(rs.getString(returnCol1));
}

}

The query is getting formed correctly , but does not return the result. The same query when executed on SQL server 2008 returns the result. Can you please suggest ? Is there any method in SQL Server 2008 , as in PostGreSQL in the post below : Foreign/accented characters in sql query

Upvotes: 0

Views: 910

Answers (1)

wiretext
wiretext

Reputation: 3342

N denotes that the subsequent string is in Unicode More Info...

Plain SQL Code would be

SELECT returnCol1 FROM tableName WHERE lookupCol1 = N'turkeyCharactor';
--OR
SELECT returnCol1 FROM tableName WHERE lookupCol1 LIKE N'%turkeyCharactor%';

passing from Java (Sorry if some syntax Problem because i don't no much about it)

"select "+ returnCol1 + " from " + tableName +" where "+ lookupCol1 + "= N'" + lookupData1 + "';" 
--OR
"select "+ returnCol1 + " from " + tableName +" where "+ lookupCol1 + "LIKE N'%" + lookupData1 + "%';" 

Upvotes: 1

Related Questions