我们公司常用的数据库连接类,或许对你有些帮助。DbConnect.java:package com.web.db;import java.io.File;
import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;public class DbConnect
{    private static DbConnect instance = null;
    private static DataSource ds = null;
    private static String dsName = null;
    private static String username = null;
    private static String password = null;    public DbConnect()
    {
    }    public static DbConnect getInstance()
    {
        if(instance == null)
            instance = new DbConnect();
        return instance;
    }    public void loadCfgFile()
        throws Exception
    {
        String cfgFileName = null;
        try
        {
            cfgFileName = System.getProperties().getProperty("CONFIG_FILE","c:/boot.properties");
            File cfgFile = new File(cfgFileName);
            FileInputStream in = new FileInputStream(cfgFile);
            Properties p = new Properties();
            p.load(in);
            dsName = p.getProperty("system.datasource.name");
            username = p.getProperty("system.database.username");
            password = p.getProperty("system.database.password");
            in.close();
        }
        catch(Exception ex)
        {
            throw new Exception(String.valueOf(String.valueOf((new StringBuffer("Load from configfile(")).append(cfgFileName).append(") error . ").append(ex.getMessage()))));
        }
    }    public Connection getConnection(String url, String name, String password, int timeout)
        throws Exception
    {
        Driver driver = (Driver)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
        DriverManager.registerDriver(driver);
        if(timeout > 0)
            DriverManager.setLoginTimeout(timeout);
        Connection conn = DriverManager.getConnection(url, name, password);
        return conn;
    }    public Connection getConnection(String dsName, String name, String password)
        throws Exception
    {
        Connection conn = null;
        if(ds == null)
            try
            {
                Context ctx = new InitialContext();
                ds = (DataSource)ctx.lookup(dsName);
            }
            catch(Exception e)
            {
                throw new Exception(" can't get db connection , result : ".concat(String.valueOf(String.valueOf(e.getMessage()))));
            }
        try
        {
            if(username == null || username.equals(""))
                conn = ds.getConnection();
            else
                conn = ds.getConnection(name, password);
        }
        catch(Exception e)
        {
            throw new Exception(" can't get db connection , result : ".concat(String.valueOf(String.valueOf(e.getMessage()))));
        }
        return conn;
    }    public Connection getConnection()
        throws Exception
    {
        if(dsName == null)
            loadCfgFile();
        return getConnection(dsName, username, password);
    }}

解决方案 »

  1.   

    只是想多少给你点提示:
    public class TestBean{
      public static void main(String[] args) {
        try {
          Class.forName("oracle.jdbc.driver.OracleDriver");//如果是Ms Sql的就换成相应的就行
          String url = "jdbc:oracle:thin:@localhost:1521:数据库名称";
          String user = "xxx";
          String password = "xxx";
          Connection conn = DriverManager.getConnection(url, user, password);
          Connection con=conn.getConn();
          Statement stmt = con.createStatement();
          String sql = "select ........ ";
          ResultSet rs = stmt.executeQuery(sql);
          while (rs.next()) {
            System.out.println("first:"+rs.getString(1));
            System.out.println("second:"+rs.getString(2));
            ........
          }
          rs.close();
          stmt.close();
          con.close();
        }
        catch (Exception e) {
          e.printStackTrace();
          System.out.println("连接失败!");
        }  }
    }
      

  2.   

    首先感谢几位大哥,可是我想用的是用jndi来查找数据源,也就是
    DataSource ds=(DataSource) ctx.lookup("jndiname");
    Connection conn=ds.getConnection();的方法找到数据源可是为什么就是不行呢、、????
      

  3.   

    那就是你的数据有问题了 ! 代码没有问题。。你用的 是什么服务器啊。。??你用google找一下数据源的配置 网上很多的如果还没有弄好的话,就把配置文件帖出来吧
      

  4.   

    我终于知道是怎么回事了,其实呢我的连接是没错的,只是在第二个Bean调用第一个连接Bean的时候没有main方法,就没有入口,所以才出现那些错误,不过在这里还是十分感谢abest和yeshucheng(叶澍成)的,你们的东西给了我第大的启发谢谢