import javax.sql.DataSource; import java.sql.*; public class JDBCTest { public static void main(String[] args) throws Exception { int mode = 1; if(args != null && args.length > 0){ try{ mode=Integer.parseInt(args[0]); }catch(Exception e){ } } new JDBCTest(mode); } public JDBCTest(int mode) throws Exception{ Connection con = getConnection(mode); Statement stmt = con.createStatement(); ResultSet rs= stmt.executeQuery("select count(*) TOTAL_COUNT from tab1"); while(rs.next()){ System.out.println("Count : " + rs.getInt("TOTAL_COUNT")); } rs.close(); stmt.close(); con.close(); } private Connection getConnection(int mode) throws Exception{ if(mode==1){ return getDataSource().getConnection(); }else{ return getDriverBasedConnection(); } } private DataSource getDataSource() { com.sun.sql.jdbcx.sqlserver.SQLServerDataSource ds = new com.sun.sql.jdbcx.sqlserver.SQLServerDataSource(); ds.setUser("dbuser"); ds.setPassword("dbpassword"); ds.setDatabaseName("dbsmpl1"); //ds.setURL("jdbc:sun:sqlserver://129.145.133.124:1433;SID=dbsmpl1"); ds.setPortNumber(1433); ds.setServerName("129.145.133.124"); return ds; } private Connection getDriverBasedConnection() throws Exception{ Class.forName("com.sun.sql.jdbc.sqlserver.SQLServerDriver"); return DriverManager.getConnection("jdbc:sun:sqlserver://129.145.133.124:1433;SID=dbsmpl1","dbuser","dbpassword"); } }