com.microsoft.sqlserver.jdbc.SQLServerException: Socket closed 或者 该连接已关闭
com.microsoft.sqlserver.jdbc.SQLServerException: Socket closed 或者 该连接已关闭
解决方案:
DBUtil公共方法如下:
package com.dao; import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper; public class DBUtil {
//连接对象
//Statement 命令对象
//打开连接
//关闭连接
//得到一个连接对象
//查询(有参,无参)
//修改(有参,无参)
static Statement stmt = null;
//驱动,服务器地址,登录用户名,密码
static String DBDRIVER;
static String DBURL;
static String DBUID;
static String DBPWD; static {
//先创建资源文件,扩展名为.properties
//内容是以:dbuser=sa 格式 Properties prop = new Properties();//先获取资源对象
//创建输入流,读取资源文件
InputStream in =Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jdbc.properties");
try {
prop.load(in);//加载
DBDRIVER = prop.getProperty("DBDRIVER");
DBURL = prop.getProperty("DBURL");
DBUID = prop.getProperty("DBUID");
DBPWD = prop.getProperty("DBPWD");
//System.out.println(DBDRIVER);
} catch (IOException e) {
System.out.println("资源文件读取错误,请查看资源文件");
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//打开连接
static {
//加载驱动
try {
Class.forName(DBDRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//关闭连接
public static void close(Connection conn) {
try {
if(stmt!=null)
stmt.close();
if(conn!=null && !conn.isClosed())
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(ResultSet rs) {
Statement st = null;
Connection con = null;
try {
try {
if (rs != null) {
st = rs.getStatement();
rs.close();
}
} finally {
try {
if (st != null) {
con = st.getConnection();
st.close();
}
} finally {
if (con != null) {
con.close();
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//得到一个连接对象,当用户使用DBUtil无法解决个性问题时
//可以通过本方法获得连接对象
public static Connection getConnection() {
Connection conn = null;
try {
conn=DriverManager.getConnection(DBURL,DBUID,DBPWD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
} //executeQuery
//executeUpdate
//execute
//获得查询的数据集
//select * from student where name='' and sex=''
public static ResultSet executeQuery(String sql) {
Connection conn = getConnection();
try {
stmt = conn.createStatement();
return stmt.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
} //修改表格内容
public static int executeUpdate(String sql) {
Connection conn = getConnection();
int result = 0;
try {
stmt = conn.createStatement();
result = stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close(conn);
}
return result;
}
//如果执行的查询或存储过程,会返回多个数据集,或多个执行成功记录数
//可以调用本方法,返回的结果,
//是一个List<ResultSet>或List<Integer>集合
public static Object execute(String sql) {
Connection conn = getConnection();
boolean b=false;
try {
stmt = conn.createStatement();
b = stmt.execute(sql);
//true,执行的是一个查询语句,我们可以得到一个数据集
//false,执行的是一个修改语句,我们可以得到一个执行成功的记录数
if(b){
return stmt.getResultSet();
}
else {
return stmt.getUpdateCount();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(!b) {
close(conn);
}
}
return null;
} //
//select * from student where name=? and sex=?
public static ResultSet executeQuery(String sql,Object[] in) {
Connection conn = getConnection();
try {
PreparedStatement pst = conn.prepareStatement(sql);
for(int i=0;i<in.length;i++)
pst.setObject(i+1, in[i]);
stmt = pst;//只是为了关闭命令对象pst
return pst.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
} public static int executeUpdate(String sql,Object[] in) {
Connection conn = getConnection();
try {
PreparedStatement pst = conn.prepareStatement(sql);
for(int i=0;i<in.length;i++)
pst.setObject(i+1, in[i]);
stmt = pst;//只是为了关闭命令对象pst
return pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
close(conn);
}
return 0;
}
public static Object execute(String sql,Object[] in) {
Connection conn = getConnection();
boolean b=false;
try {
PreparedStatement pst = conn.prepareStatement(sql);
for(int i=0;i<in.length;i++)
pst.setObject(i+1, in[i]);
b = pst.execute();
//true,执行的是一个查询语句,我们可以得到一个数据集
//false,执行的是一个修改语句,我们可以得到一个执行成功的记录数
if(b){
System.out.println("----");
/*List<ResultSet> list = new ArrayList<ResultSet>();
list.add(pst.getResultSet());
while(pst.getMoreResults()) {
list.add(pst.getResultSet());
}*/
return pst.getResultSet();
}
else {
System.out.println("****");
List<Integer> list = new ArrayList<Integer>();
list.add(pst.getUpdateCount());
while(pst.getMoreResults()) {
list.add(pst.getUpdateCount());
}
return list;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(!b) {
System.out.println("====");
close(conn);
}
}
return null;
}
//调用存储过程 proc_Insert(?,?,?)
public static Object executeProcedure(String procName,Object[] in) {
Connection conn = getConnection();
try {
procName = "{call "+procName+"(";
String link="";
for(int i=0;i<in.length;i++) {
procName+=link+"?";
link=",";
}
procName+=")}";
CallableStatement cstmt = conn.prepareCall(procName);
for(int i=0;i<in.length;i++) {
cstmt.setObject(i+1, in[i]);
}
if(cstmt.execute())
{
return cstmt.getResultSet();
}
else {
return cstmt.getUpdateCount();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return null;
} /*
* 调用存储过程,并有输出参数
* @procName ,存储过程名称:proc_Insert(?,?)
* @in ,输入参数集合
* @output,输出参数集合
* @type,输出参数类型集合
* */
public static Object executeOutputProcedure(String procName,
Object[] in,Object[] output,int[] type){
Connection conn = getConnection();
Object result = null;
try {
CallableStatement cstmt = conn.prepareCall("{call "+procName+"}");
//设置存储过程的参数值
int i=0;
for(;i<in.length;i++){//设置输入参数
cstmt.setObject(i+1, in[i]);
//print(i+1);
}
int len = output.length+i;
for(;i<len;i++){//设置输出参数
cstmt.registerOutParameter(i+1,type[i-in.length]);
//print(i+1);
}
boolean b = cstmt.execute();
//获取输出参数的值
for(i=in.length;i<output.length+in.length;i++)
output[i-in.length] = cstmt.getObject(i+1);
if(b) {
result = cstmt.getResultSet();
}
else {
result = cstmt.getUpdateCount();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static String toJson(Object obj){
String reuqest=null;
//对象映射
ObjectMapper mapper=new ObjectMapper();
//设置时间格式
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy年MM月dd日");
mapper.setDateFormat(dateFormat);
try {
reuqest=mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return reuqest;
}
public static <T> T toObject(String src,Class<T> valueType){
T request=null;
//对象反射
ObjectMapper mapper=new ObjectMapper();
try {
request=mapper.readValue(src, valueType);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return request;
}
public static Date date(String date_str) {
try {
Calendar zcal = Calendar.getInstance();//日期类
Timestamp timestampnow = new Timestamp(zcal.getTimeInMillis());//转换成正常的日期格式
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//改为需要的东西
ParsePosition pos = new ParsePosition(0);
java.util.Date current = formatter.parse(date_str, pos);
timestampnow = new Timestamp(current.getTime());
return timestampnow;
}
catch (NullPointerException e) {
return null;
}
}
}
声明:使用该公共方法需要在WEB-INF/classes/jdbc.properties添加连接驱动等等 如图:
在classes中加入链接
DBDRIVER=com.microsoft.sqlserver.jdbc.SQLServerDriver
DBURL=jdbc:sqlserver://localhost:1433;databasename=AJAX_ProductDB
DBUID=sa
DBPWD=123456
com.microsoft.sqlserver.jdbc.SQLServerException: Socket closed 或者 该连接已关闭的更多相关文章
- atitit.故障排除------有时会错误com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset by peer: soc
atitit.故障排除------有时会错误com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset by peer: soc ...
- com.microsoft.sqlserver.jdbc.SQLServerException: 用户 'sa' 登录失败。
com.microsoft.sqlserver.jdbc.SQLServerException: 用户 'sa' 登录失败. at com.microsoft.sqlserver.jdbc.SQLSe ...
- com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
问题描述:最简单的数据库连接报错,到主机 的 TCP/IP 连接失败.(win 7 操作系统) 错误信息: com.microsoft.sqlserver.jdbc.SQLServerExcep ...
- 【J2EE】Java连接SQL Server 2000问题:“com.microsoft.sqlserver.jdbc.SQLServerException:用户'sa'登录失败。该用户与可信SQL Server连接无关联”
1.问题现象 E:\JSP\HibernateDemo\HibernateDemoProject\src\sine>java ConnectSQLServerConnect failed!com ...
- dbutils报错:com.microsoft.sqlserver.jdbc.SQLServerException: 无法识别元数据的表
今天用dbutils操作数据库,莫名地报错:com.microsoft.sqlserver.jdbc.SQLServerException: 无法识别元数据的表 检查了sql语句没有问题.经过仔细排查 ...
- com.microsoft.sqlserver.jdbc.SQLServerException: 结果集没有当前行
參考博客com.microsoft.sqlserver.jdbc.SQLServerException: 结果集没有当前行 java获取结果集,if(rs!=null).和while(rs.next( ...
- JDBC:SqlServer连接TCP/IP连接失败,到主机 的 TCP/IP 连接失败。报错信息:com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。
作者QQ:1161493927,欢迎互相交流学习. 报错信息:com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败. j ...
- 解决com.microsoft.sqlserver.jdbc.SQLServerException: 该连接已关闭
com.microsoft.sqlserver.jdbc.SQLServerException: 该连接已关闭. at com.microsoft.sqlserver.jdbc.SQLServerEx ...
- Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 不支持“variant”数据类型。
mybatis执行sqlserver的sql报错 com.microsoft.sqlserver.jdbc.SQLServerException: 不支持“variant”数据类型. at com.m ...
随机推荐
- 分享:JAVA和C# 3DES加密解密
最近 一个项目.net 要调用JAVA的WEB SERVICE,数据采用3DES加密,涉及到两种语言3DES一致性的问题,下面分享一下,这里的KEY采用Base64编码,便用分发,因为Java的Byt ...
- Caffe & Caffe2入门博客存档
caffe2 教程入门(python版) https://www.jianshu.com/p/5c0fd1c9fef9?from=timeline caffe入门学习 https://blog.csd ...
- select操作大全
每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selector"></select&g ...
- 1、Angular2 Component 组件
angular2借鉴了.http://www.cnblogs.com/lewis617/p/5191007.html 导入了自己的思维方式 1.基本属性 2.*语法与template标签 3.组件的嵌 ...
- java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ 解决方案
//第一个异常 Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysq ...
- html的标签(1)
首先补充上一次没有讲到的,html文件后缀名有2种,一种是.htm,一种是.html..html是老的教科书里面的文件后缀名,.html是新的教科书的文件后缀名..htm文件是不完整的支持html5的 ...
- Hadoop的安装与配置(虚拟机中的伪分布模式)
1引言 hadoop如今已经成为大数据处理中不可缺少的关键技术,在如今大数据爆炸的时代,hadoop给我们处理海量数据提供了强有力的技术支撑.因此,了解hadoop的原理与应用方法是必要的技术知识. ...
- 【IOS】Mac和IOS开发资源汇总
本文主要汇集一些苹果开发的资源,会经常更新,建议大家把这篇文章单独收藏(在浏览器中按**command+D**). 今天收录了许多中文网站和博客.大家一定要去感受一下哦. 如果大家有知道不错的站点,可 ...
- Python学习系列----第五章 模块
5.1 如何引入模块 在Python中用关键字import来引入某个模块,比如要引用模块math,就可以在文件最开始的地方用import math来引入.在调用math模块中的函数时,必须这样引用: ...
- Mac系统在finder拦显示当前所浏览的文件路径的方法
我们在使用MAC时,Finder栏默认只显示当前浏览的文件夹名称,而没有显示访问路径,这个问题该怎么解决呢? 编辑node的时候需要路径,亲测有效啦~可以试下! 操作步骤: 打开“终端”(应用程序-& ...