具體事例: [java] package Utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DB { private static Connection con = null; private static Statement statement = null; private static ResultSet set = null; private String sql = ""; // 加載SqlServer JDBC驅動 private static String driverNameOfSqlServer = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // IP地址(改為自己的IP地址) private static String DatabaseIP = "localhost"; // 數據庫用戶名 private static String DatabaseUser = "sjf"; // 數據庫密碼 private static String DatabasePassword = "123456"; // 數據庫名稱 private static String DatabaseName = "pubs"; // URL private static String DatabaseUrl = "jdbc:sqlserver://" + DatabaseIP + ":1433;DatabaseName = " + DatabaseName; //獲取一個數據庫的連接 public Connection getConnection() { try { //注冊驅動程序 Class.forName(driverNameOfSqlServer); // 獲取連接 con = DriverManager.getConnection(DatabaseUrl, DatabaseUser,DatabasePassword); } catch (Exception e) { System.out.println("getConnection出現錯誤"); e.printStackTrace(); } return con; } //創建會話 public Statement getStatement(Connection con){ if(con != null){ try { statement = con.createStatement(); return statement; } catch (SQLException e) { System.out.println("getStatement出現錯誤"); e.printStackTrace(); } } return null; } //查詢 public ResultSet getResultSetQuery(Statement statement,String sql) { if(statement != null){ try { set = statement.executeQuery(sql); return set; } catch (SQLException e) { System.out.println("getResultSetQuery出現錯誤"); e.printStackTrace(); } } return null; } //增加,修改,刪除記錄 public void getResultSetUpdate(Statement statement,String sql) { if(statement != null){ try { statement.executeUpdate(sql); } catch (SQLException e) { System.out.println("getResultSetUpdate出現錯誤"); e.printStackTrace(); } } } //關閉連接 public static void colse(Connection con){ if(con != null){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } //關閉會話 public static void close(Statement statement){ if(statement != null){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } } //關閉查詢集 public static void close(ResultSet set){ if(set != null){ try { set.close(); } catch (SQLException e) { e.printStackTrace(); } } } } package Utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DB { private static Connection con = null; private static Statement statement = null; private static ResultSet set = null; private String sql = ""; // 加載SqlServer JDBC驅動 private static String driverNameOfSqlServer = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // IP地址(改為自己的IP地址) private static String DatabaseIP = "localhost"; // 數據庫用戶名 private static String DatabaseUser = "sjf"; // 數據庫密碼 private static String DatabasePassword = "123456"; // 數據庫名稱 private static String DatabaseName = "pubs"; // URL private static String DatabaseUrl = "jdbc:sqlserver://" + DatabaseIP + ":1433;DatabaseName = " + DatabaseName; //獲取一個數據庫的連接 public Connection getConnection() { try { //注冊驅動程序 Class.forName(driverNameOfSqlServer); // 獲取連接 con = DriverManager.getConnection(DatabaseUrl, DatabaseUser,DatabasePassword); } catch (Exception e) { System.out.println("getConnection出現錯誤"); e.printStackTrace(); } return con; } //創建會話 public Statement getStatement(Connection con){ if(con != null){ try { statement = con.createStatement(); return statement; } catch (SQLException e) { System.out.println("getStatement出現錯誤"); e.printStackTrace(); } } return null; } //查詢 public ResultSet getResultSetQuery(Statement statement,String sql) { if(statement != null){ try { set = statement.executeQuery(sql); return set; } catch (SQLException e) { System.out.println("getResultSetQuery出現錯誤"); e.printStackTrace(); } } return null; } //增加,修改,刪除記錄 public void getResultSetUpdate(Statement statement,String sql) { if(statement != null){ try { statement.executeUpdate(sql); } catch (SQLException e) { System.out.println("getResultSetUpdate出現錯誤"); e.printStackTrace(); } } } //關閉連接 public static void colse(Connection con){ if(con != null){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } //關閉會話 public static void close(Statement statement){ if(statement != null){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } } //關閉查詢集 public static void close(ResultSet set){ if(set != null){ try { set.close(); } catch (SQLException e) { e.printStackTrace(); } } } } 測試: [java] DB db = new DB(); Connection con = db.getConnection(); Statement statement = db.getStatement(con); String sql = "select * from dbo.jobs"; ResultSet rs = db.getResultSetQuery(statement, sql); try { if(rs.next()){ System.out.println("fdfsdfsdff"+rs.getString("job_desc")); } } catch (SQLException e) { e.printStackTrace(); } www.2cto.com DB db = new DB(); Connection con = db.getConnection(); Statement statement = db.getStatement(con); String sql = "select * from dbo.jobs"; ResultSet rs = db.getResultSetQuery(statement, sql); try { if(rs.next()){ System.out.println("fdfsdfsdff"+rs.getString("job_desc")); } } catch (SQLException e) { e.printStackTrace(); }