数据库名称,登陆名, 登陆密码换成你自己的 如果你做查询就 调用 openResultSet(String sql)方法如果你做插入、更新、删除就调用execute(String sql)方法记得做完后关闭数据库 调用clossAll() 方法这个就是sql2005的JDBC直连,不需要你做任何修改~~~import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.util.Properties;public class DBConnection extends Properties { Connection conn; Statement sm; ResultSet rs; static { try { Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Connection openConnection() { try { conn = DriverManager.getConnection( jdbc:sqlserver://localhost:1433;databasename=数据库名称, 用户名, 密码); return conn; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return null; } } public Statement openStatement() { try { if (conn == null || conn.isClosed()) conn = openConnection(); sm = conn.createStatement(); return sm; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return null; } } public ResultSet openResultSet(String sql) { try { if (sm == null) sm = openStatement(); rs = sm.executeQuery(sql); return rs; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return null; } } public int execute(String sql) { try { if (sm == null) sm = openStatement(); int result = sm.executeUpdate(sql); return result; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return -1; } } public void clossAll() { try { if (rs != null) rs.close(); if (sm != null) sm.close(); if (conn != null && !conn.isClosed()) conn.close(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } }}