unicode supported jdbc connection with sqlserver using Data source name

谁都会走 提交于 2019-12-12 19:22:23

问题


Case 1: I am trying to connect to the sql-server 2005 database which is situated on the remote server using

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        connection = DriverManager.getConnection("jdbc:sqlserver://190.128.4.195/unicodedemo?user=ab&password=ab@Admin&useUnicode=true&characterEncoding=UTF-8");

When I execute, I got the following exception :

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 190.128.4.195/unicodedemo?user=ab&password=ab@Admin&useUnicode=true&characterEncoding=UTF-8, port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".

Case 2: But when I tried using Data source name using

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
connection  = java.sql.DriverManager.getConnection("jdbc:odbc:unidemo","ab","ab@Admin");

Data successfully gets inserted but in that case it was not showing the Unicode data inserted. Its showing as ?????.

Can anybody help in solving the error? When I am committing in Case 1, how can I insert Unicode values using the second case (i.e using DSN).

Thanks..


回答1:


Case 1: The format of connection URL to SQL Server is this:

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

which means that you should probably change your connection URL to

jdbc:sqlserver://190.128.4.195;databaseName=unicodedemo;user=ab;password=ab@Admin;useUnicode=true;characterEncoding=UTF-8



回答2:


Try using like this, it worked for me:

jdbc:sqlserver://190.128.4.195\instanceName:1433;databaseName=unicodedemo;user=ab&password=ab@Admin&useUnicode=true&characterEncoding=UTF-8


来源:https://stackoverflow.com/questions/8969981/unicode-supported-jdbc-connection-with-sqlserver-using-data-source-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!