我希望使用数据源方式连接SQL2005,希望高手指点整个解决方案,不胜感激。我的环境是win2003+sql2005+jdk1.5+tomcat5.5。

解决方案 »

  1.   

    public class BasicDataSourceExample {    public static void main(String[] args) {
            // First we set up the BasicDataSource.
            // Normally this would be handled auto-magically by
            // an external configuration, but in this example we'll
            // do it manually.
            //
            System.out.println("Setting up data source.");
            DataSource dataSource = setupDataSource(args[0]);
            System.out.println("Done.");        //
            // Now, we can use JDBC DataSource as we normally would.
            //
            Connection conn = null;
            Statement stmt = null;
            ResultSet rset = null;        try {
                System.out.println("Creating connection.");
                conn = dataSource.getConnection();
                System.out.println("Creating statement.");
                stmt = conn.createStatement();
                System.out.println("Executing statement.");
                rset = stmt.executeQuery(args[1]);
                System.out.println("Results:");
                int numcols = rset.getMetaData().getColumnCount();
                while(rset.next()) {
                    for(int i=1;i<=numcols;i++) {
                        System.out.print("\t" + rset.getString(i));
                    }
                    System.out.println("");
                }
            } catch(SQLException e) {
                e.printStackTrace();
            } finally {
                try { rset.close(); } catch(Exception e) { }
                try { stmt.close(); } catch(Exception e) { }
                try { conn.close(); } catch(Exception e) { }
            }
        }    public static DataSource setupDataSource(String connectURI) {
            BasicDataSource ds = new BasicDataSource();
            ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
            ds.setUsername("scott");
            ds.setPassword("tiger");
            ds.setUrl(connectURI);
            return ds;
        }    public static void printDataSourceStats(DataSource ds) throws SQLException {
            BasicDataSource bds = (BasicDataSource) ds;
            System.out.println("NumActive: " + bds.getNumActive());
            System.out.println("NumIdle: " + bds.getNumIdle());
        }    public static void shutdownDataSource(DataSource ds) throws SQLException {
            BasicDataSource bds = (BasicDataSource) ds;
            bds.close();
        }
    }
      

  2.   

    兄弟,用数据源,那得用ODBC TO JDBC的桥连接,如果同时在线的人数一多,估计就会爆。
    我觉得你还是用JDBC好,你的环境都配好了没?那些系统补丁,SQL2005补丁,JDBC补丁都安装好,理论上和在2000上是一样的,只是JDBC补丁包的顺序不同了!得改下!
      

  3.   

    这是我做项目时用到的JDBC,仅供参考:
    import java.sql.*;
    public class ConnectDB {
    Connection connection;
     String jdbcDriverClassName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
     String jdbcURL = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName = ShoeManager";
     String user = "share";
     String password = "java";
    public ConnectDB() {
    try{
    try {
    Class.forName(jdbcDriverClassName);
    //System.out.print("hello");
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    connection = DriverManager.getConnection(jdbcURL,user,password);
    }
    catch(SQLException e){
    e.printStackTrace();
    }
    // TODO Auto-generated constructor stub
    }
    public Connection getConnection(){
    return connection;
    }}
    import java.sql.*;
    import java.util.Vector;import javax.swing.JOptionPane;public class Operation {
     Connection connection;
     Statement statement;
     ResultSet result ;

    public Operation() {
    try{
    connection = (new ConnectDB()).getConnection();
    statement = connection.createStatement();
    }
    catch(SQLException e){
    e.printStackTrace();
    }
    // TODO Auto-generated constructor stub
    }
    public boolean productadd(String sql){
    try{
    statement.executeUpdate(sql);
    statement.close();
    connection.close();
    return true;
    }
    catch(SQLException e){
    e.printStackTrace();
    return false;
    }
    //return true;
    }
    public ResultSet query(String sql){
    try{
    result = statement.executeQuery(sql);
    if(result.next()) 
    {
    //JOptionPane.showMessageDialog(null,"已经定位到第一条记录了!");
    return result;
    }
    else
    {
    JOptionPane.showMessageDialog(null,"不存在您输入的用户!");
    return null;
    }

    //statement.close();
    //connection.close();

    }
    catch(SQLException e){
    e.printStackTrace();
    return null;
    }
    //return true;

    }}
      

  4.   

    已经发布sqljdbc.jar了,你使用他就可以!
      

  5.   

    1 在server.xml中配置数据源
    2 在具体应用中的web.xml中配置对所配置数据源的引用
    3 在应用中使用数据源由于是使用数据源的方式,所以隔离了针对具体数据库的具体操作的不同,因此,前端应用与底层数据库相对无关。