CentOS 65 java 访问 MS SQL

#install unixODBC
sudo yum install unixODBC unixODBC-devel -y

#install freetds
#1st download freetds
cd 
wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-stable.tgz
#2nd unpack freetds
tar -xzvf freetds-stable.tgz

#configure make make install freetds
cd freetds-0.91/
./configure -prefix=/usr/local/freetds  -with-tdsver=8.0 -enable-msdblib  -with-gnu-ld -enable-shared -enable-static 
sudo make
sudo make install

#建立odbc driver描述文件
vim tds.driver
[TDS] Description=driver to connect to MS Sql Driver=/usr/local/lib/libtdsodbc.so Setup=/usr/lib64/libtdsS.so #install freetds driver
sudo odbcinst -i -d -f tds.driver

#建立数据源描述文件
vim mydatasource
[devserver] description=sql2000 on devserver driver=TDS server=192.168.0.22 port=1433 user=sa password=****** database=testdb TDS_Version=8.0 #install datasource
odbcinst -i -s -f mydatasource

#测试
isql devserver sa passwd -v

 如果unixODBC连接失败,isql测试的时候加上-v可以打印出详细信息便于纠错。

经我测试,数据源设置中不能有中文,否则isql测试同样出现“意外EOF”错误.

二、java中使用sun.jdbc.odbc.JdbcOdbcDriver链接上面配置的数据源,或者指定配置的driver,然后设置服务器的详细信息也可以连接。

sun.jdbc.odbc.JdbcOdbcDriver c =(JdbcOdbcDriver) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") .newInstance();
		  
//String URL = "jdbc:odbc:devserver";
//String URL = "jdbc:odbc:driver=TDS;server=192.168.0.22;port=1433;database=lcmis;uid=sa;pwd=*****";
String URL = "jdbc:odbc:driver=TDS;server=192.168.0.22;port=1433;database=lcmis;uid=sa;pwd=*****";
原文地址:https://www.cnblogs.com/dajianshi/p/3877837.html