数据库连接大全

来源:互联网 发布:托尼帕克数据 编辑:程序博客网 时间:2024/05/02 01:09
 

*************************************************************************************************************************************************************************************
                                   
                                             数据库连接大全

**************************************************************************************************************************************************************************************

/****************************************************************************************************

连接ORACLE数据库的方法如下,所有数据库都可使用getJdbcConnection() 方法获取数据连接,只要更改其参数(driverName & url & user & password),其他数据库相对应的driverNameurl 参考:

*****************************************************************************************************

-----------------------------------------------------------------------------------------------------
  数据库                 driverName                     url
-----------------------------------------------------------------------------------------------------
  orcale      oracle.jdbc.driver.OracleDriver      
                                        jdbc:oracle:thin:@localhost:1521:DbN
-----------------------------------------------------------------------------------------------------
  sqlServer   com.microsoft.jdbc.sqlserver.SQLServerDriver
                                        jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=DbN
-----------------------------------------------------------------------------------------------------
  DB2         com.ibm.db2.jdbc.app.DB2Driver       
                                        jdbc:db2://localhost:5000/DbN
-----------------------------------------------------------------------------------------------------
  informix    com.informix.jdbc.IfxDriver  
                                        jdbc:informix-sqli://IP:1533/DbN:INformIXSERVER=myserver
-----------------------------------------------------------------------------------------------------
  Sybase      com.sybase.jdbc.SybDriver       
                                        jdbc:sybase:Tds:localhost:5007/DbN
-----------------------------------------------------------------------------------------------------
  MySql       org.gjt.mm.mysql.Driver          
                                        jdbc:mysql://localhost/DbN
-----------------------------------------------------------------------------------------------------
  PostgreSQL  org.postgresql.Driver          
                                        jdbc:postgresql://localhost/DbN
-----------------------------------------------------------------------------------------------------

****************************************************************************************************/

//    文件Database.java代码如下


public class Database
{

 //jdbc数据库参数
 private String driverName ="oracle.jdbc.driver.OracleDriver";
 private String url ="jdbc:oracle:thin:@192.168.1.241:1521:student";
 private String user ="system";
 private String password ="system";

 
 
//数据池的别名
 private String jndiName ="student";
 
//获取数据连接
 public Connection getConnection()throws SQLException
 {
  
//获得jdbc数据库连接
  return this.getJdbcConnection();
  
//获得数据池连接
  //return this.getPoolConnection();
 }
 
 //从数据池获得数据库连接
 private Connection getPolConnection()throws SQLException
 {
  Context ctx =null;
  DataSource ds =null;
  try
  {
   System.out.println("getPoolConnection !");
   ctx =new InitialContext();
   ds =(DataSource)ctx.lookup(this.jndiName);
   return ds.getConnection();
  }catch(NamingException ne)
  {
   throw new SQLException("无法获得数据库连接 !");
  }catch(SQLException se)
  {
   throw new SQLException("无法获得数据库连接 !");
  }finally
  {
   try
   {
    if(ctx!=null)
    {
     ctx.close();
     ctx =null;
    }
    if(ds!=null)
    {
     ds=null;
    }
   }catch(Exception e)
   {
    ctx=null;
    ds=null;
   }
  }
 }
 
 
 //通过JDBC驱动直接获得数据库连接
 private Connection getJdbcConnection()throws SQLException
 {
  try
  {
   System.out.println("getJdbcConnection !");
   Class.forName(this.driverName);
   return DriverManager.getConnection(this.url,this.user,this.password);
  }catch(ClassNotFoundException cnfe)
  {
   throw new SQLException("无法获得数据库连接 !");
  }catch(SQLException se)
  {
   throw new SQLException("无法获得数据库连接 !");
  }
 }
}

如发现有问题请与本人联系:
  MSN:
hiyu2218@hotmail.com
  QQ:147204701
  Email:
hiyu2218@yahoo.com.cn

 
原创粉丝点击