Java 从MSSQL NVarchar列检索阿拉伯语文本
我正在制作一个图书馆管理系统,我试图在我的项目中添加阿拉伯语支持,但我面临以下困难:Java 从MSSQL NVarchar列检索阿拉伯语文本,java,sql-server,resultset,arabic,nvarchar,Java,Sql Server,Resultset,Arabic,Nvarchar,我正在制作一个图书馆管理系统,我试图在我的项目中添加阿拉伯语支持,但我面临以下困难: 当通过查询在SQL中插入数据时,即插入图书信息值(25,N'作者',N'出版商',2014,N'主题',50,N'语言','Latest',5)图书信息中的列列表如下 图书标识(int) 书名(NVarchar(50)) 作者(NVarchar(50)) 出版商(NVarchar(50)) 出版年份(国际) 受试者(NVarchar(50)) 价格(整数) 语言(NVarchar(50)) 版本(NVarch
插入图书信息值(25,N'作者',N'出版商',2014,N'主题',50,N'语言','Latest',5)
图书信息中的列列表如下
- 图书标识(int)
- 书名(NVarchar(50))
- 作者(NVarchar(50))
- 出版商(NVarchar(50))
- 出版年份(国际)
- 受试者(NVarchar(50))
- 价格(整数)
- 语言(NVarchar(50))
- 版本(NVarchar(50))
- 部分(内部)
sqlmanagementstudio
不仅在sqlmanagementstudio
中,而且在项目中选择数据时,还会显示一些编码字符,如'。注意:查询中的这些值取自textfield
到.getText()
函数
sqlmanagementstudio
中执行查询来跳过第一个问题,会怎么样,通过这样做,第一个问题暂时得以解决,因为SQL Management Studio
在选择数据时会显示阿拉伯字符,但当我们从软件中选择数据并通过从ResultSet
获取数据在Textfield
中显示时,它会显示“TextF.setText(ResultS.getNString())
”文本字段中的“?
”代替阿拉伯字符试着像这样使用
COLLATE
语句:
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
您的数据库定义、查询和SQL实例都很好。您遇到的问题是如何将数据库中的字符串读入java 我认为Java正在尝试将nvarchar文本读取为ASCII字符,而实际上它们是Unicode。结果是看起来像乱码的文本。我认为,当您从数据库读取文本时,您需要确保读取的是Unicode
如果你能发布你的代码,我可能会找出导致你问题的原因。在花了几个小时寻找原因并咨询我的前辈后,我得到了解决方案,简而言之,我只需将传入的结果集编码为UTF-8(Unicode)编码,我是如何做到的: 使用nvarchar()作为数据类型创建列表,您必须在其中插入双向文本,然后使用以下查询来插入、更新、选择和删除(示例表只有一列,即nvarchar()数据类型) 在Java中,有很多方法可以连接SQL和执行查询,就像我在windows中使用JDBC-ODBC桥连接SQL一样,下面是我的方法
Connection cn=null;
ResultSet rs=null;
PreparedStatement ps=null;
String password = *******;
String data;
public static Connection conn() throws ClassNotFoundException,SQLException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:library_management";
return DriverManager.getConnection(url,"sa", password );
}
// the library_management is the connection name which is made in Windows 'ODBC Data Sources'
// Now suppose an event is fired which calls a method given below
public void executeQ(){
try {
cn = conn();
ps = cn.prepareStatement("SELECT FROM table_name WHERE column_name LIKE N'عربی%'");
rs = ps.executeQuery();
while(rs.next()){
data = rs.getString(1);
}
} catch (ClassNotFoundException | SQLException ex) {
ex.printStackTrace();
}
}
// this method should work according to the need but since windows default encoding is a non-unicode encoding , therefore it forcibly runs a JAR file on its own encoding so for avoiding this we add a line of code in the main method of the main class of the project.
public static void main(String args[]) {
System.setProperty("file.encoding","UTF-8");
// Any code
}
请注意,对于执行任何不生成结果集的查询,我们只使用
PreparedStatement.execute()
functionUnicode字段不需要排序规则来处理Unicode数据。我无法重现这个问题,这并不奇怪。SQL Server的nvarchar不需要特殊处理来存储任何Unicode字符。在加载或读取数据时,问题始终是转换,通常是尝试“帮助”“通过硬编码错误的代码页来恢复数据库。向数据库插入数据或读取数据的代码在哪里?您绝对确定要传递Unicode字符串吗?您是否尝试过使用参数化查询,以便不必创建带有值的字符串?最有可能的罪魁祸首是getText和setText方法。我怀疑他们假设您传递的字符串是ASCII字符串,导致转换错误(由?
字符表示)@PanagiotisKanavos实际上我使用的是参数化查询,而不是硬编码查询,我知道Nvarchar不需要特殊处理。我同意最有可能的罪魁祸首是getText和settText方法,以及我存储数据的向量。我在注意中提到的这件事,我不是硬编码,而是使用getText方法进行查询。但是,如何将数据发送到数据库?在到达表之前的某个地方有一个到ASCII的转换,这就是问题的原因。getText
返回ASCII(怀疑它),数据库语句是ASCII,或者数据库驱动程序将语句作为ASCII发送。请张贴用于插入数据的代码。您还可以尝试使用SQL Profiler查看实际发送到服务器的内容,调试应用程序以查看每个步骤中字符串的值。您能告诉我获取数据的正确方法吗?我有一个结果集“rs”,其中包含所述表的1行,如何在一组文本字段或Jtable中显示该行。我想你是对的,这行完成了工作System.setProperty(“file.encoding”,“UTF-8”)代码>
Connection cn=null;
ResultSet rs=null;
PreparedStatement ps=null;
String password = *******;
String data;
public static Connection conn() throws ClassNotFoundException,SQLException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:library_management";
return DriverManager.getConnection(url,"sa", password );
}
// the library_management is the connection name which is made in Windows 'ODBC Data Sources'
// Now suppose an event is fired which calls a method given below
public void executeQ(){
try {
cn = conn();
ps = cn.prepareStatement("SELECT FROM table_name WHERE column_name LIKE N'عربی%'");
rs = ps.executeQuery();
while(rs.next()){
data = rs.getString(1);
}
} catch (ClassNotFoundException | SQLException ex) {
ex.printStackTrace();
}
}
// this method should work according to the need but since windows default encoding is a non-unicode encoding , therefore it forcibly runs a JAR file on its own encoding so for avoiding this we add a line of code in the main method of the main class of the project.
public static void main(String args[]) {
System.setProperty("file.encoding","UTF-8");
// Any code
}