[Microsoft][SQLServer 2000 Driver for JDBC]End of stream was detected on a read

ZXS824 2015-10-21 01:32:22
package com.jdbc.util;
import javax.xml.crypto.Data;
import java.io.*
import java.sql.*;


public class DBUtil {

/**
* 获取数据库连接的对象
* @return 目标对象
*/
public static Connection getConn()
{
Connection conn = null;
try {
File file = new File("file.txt");
FileReader is = new FileReader(file);

BufferedReader br = new BufferedReader(is);
int len = 1024;
char[] cbuf = new char[len];
int line = 0;
StringBuffer buffer = new StringBuffer();
while((line = br.read(cbuf, 0, len))!=-1){
buffer.append(cbuf);
}

String data[] = buffer.toString().split(",");
System.out.println(data[0]);
System.out.println(data[1]);
System.out.println(data[2]);
System.out.println(data[3]);
System.out.println("jdbc:microsoft:sqlserver://localhost:1433;databaseName=spdb1:sa:123321");
Class.forName(data[0]);
conn = DriverManager.getConnection(data[1],data[2],data[3]);
} catch (ClassNotFoundException e) {

e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return conn;
}


/**
* 关闭数据库
* @param rs
* @param stmt
* @param conn
*/
public static void closeDB(ResultSet rs, Statement stmt, Connection conn)
{
try {
if (rs!=null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (stmt!=null) {
stmt.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}finally {
try {
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}

package com.jdbc.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.jdbc.util.DBUtil;

public class Test {


public static void main(String[] aaa)
{
PreparedStatement ps = null;
Connection conn = DBUtil.getConn();
ResultSet rs = null;
String sql = "select * from emp";
try {
ps = conn.prepareStatement(sql);
rs= ps.executeQuery();
while(rs.next())
{
System.out.println(rs.getString("empno")+"\t"+rs.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.closeDB(rs, ps, conn);
}

}

}


控制台显示的异常:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]End of stream was detected on a read.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.jdbc.util.DBUtil.getConn(DBUtil.java:41)
at com.jdbc.test.Test.main(Test.java:16)
Exception in thread "main" java.lang.NullPointerException
at com.jdbc.test.Test.main(Test.java:20)
...全文
69 回复 点赞 打赏 收藏 举报
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
相关推荐
发帖
疑难问题
创建于2007-09-28

2.1w+

社区成员

MS-SQL Server 疑难问题
申请成为版主
帖子事件
创建了帖子
2015-10-21 01:32
社区公告
暂无公告