日期:2014-05-20  浏览次数:20870 次

有关JDBC的DAO封装
public List<Map<String, String>> query(String sql)
{

try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

String url= "jdbc:microsoft:sqlserver://123.138.74.24:1433;DatabaseName=组件化训练";
Connection conn = DriverManager.getConnection(url, "sa", "sa");

PreparedStatement stmt = conn.prepareStatement(sql);

ResultSet rs = stmt.executeQuery();
ResultSetMetaData md = rs.getMetaData();

List<Map<String, String>> list= new ArrayList<Map<String,String>>();

while (rs.next()) 
{
int zds = md.getColumnCount();
Map<String,String> map= new HashMap<String, String>();
for (int i = 1; i <=zds ; i++)
{
String zdm = md.getColumnName(i);
String zdz = rs.getString(zdm
map.put(zdm, zdz);
}
list.add(map);
}

rs.close();
stmt.close();
conn.close();

return list;
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

  public void testQueryString()
{
IDao dao = new DaoImpl();
String sql="select * from dbo.单词";
List<Map<String, String>> list = dao.query(sql);
System.out.println(list);
Map<String, String> ss = list.get(0);
String yw = ss.get("英文");
System.out.println(yw);
}
报错内容
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Software caused connection abort: recv failed
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(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(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at 未来编码规格.DaoImpl.query(DaoImpl.java:31)
at 未来编码规格.IDaoTest.testQueryString(IDaoTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)