日期:2014-05-18  浏览次数:20768 次

java中连接SQLSqlserver
项目中需要连接多个服务器,数据库都是SQLServer只是版本不同。

public static Boolean changeDataSource(Map map) {
DriverManagerDataSource basicDS = (DriverManagerDataSource) createBean("dataSourceTrends");

basicDS.setUsername((String) map.get("sa"));
basicDS.setPassword((String) map.get("password"));
String url = "jdbc:sqlserver://" + (String) map.get("ip") + ":"
+ (String) map.get("port") + ";DatabaseName="
+ (String) map.get("DatabaseName");

basicDS.setUrl(url);
/* if (map.get("areacode").toString().equals("420300")) {
basicDS.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");// sql2005
  System.out.println("**********sql2005**********");
} else {
basicDS.setDriverClassName("com.microsoft.jdbc.sqlserver.SQLServerDriver");// sql2000
 System.out.println("**********sql2000**********");
}*/     
Boolean isCon;
try {
// isCon = basicDS.getConnection();
// int is = basicDS.getLoginTimeout();
// System.out.println("is :"+ is);
System.out.println(url);
if (basicDS.getConnection() != null)
System.out.println("******************远程数据库连接初始化成功!!*******************");


} catch (SQLException e) {

FileWriter fileWriter = null;
PrintWriter printWriter = null;
Date now = new Date();
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG);
// DateFormat d1 = DateFormat.getDateInstance();
// //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)
try {
fileWriter = new FileWriter("d:/4201/DBConnectLog.txt", true);
printWriter = new PrintWriter(fileWriter);
printWriter.println(df.format(now) + "--:" + url + " 没有连接上");
printWriter.close();
fileWriter.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (printWriter != null) {
printWriter.close();
}
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return false;

}
return true;

}


这是我动态JDBC连接的方法,传过来的map里,装的是数据库属性数据。

--------------------------------------------------------------
问题来了。。
连接其中一个数据库,执行了查询操作,可以查出数据,但是我点下一页后就报错了


[ERROR][2013-11-14 11:07:57,515][http-8989-8][org.hibernate.util.JDBCExceptionReporter][Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确。参数 1 (""): 数据类型 0x38 未知。
org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.h