Chinaunix首页 | 论坛 | 博客
  • 博客访问: 100089
  • 博文数量: 41
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2018-05-23 12:43
文章分类

全部博文(41)

文章存档

2016年(2)

2015年(3)

2014年(16)

2013年(20)

分类: Java

2014-08-15 12:01:38

作者:tingfengjushi(听风居士)

1.spring+hibernate





myds//jndi名称weblogic11g中的配置




class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

 






true
true

org.hibernate.dialect.Oracle9Dialect


2.hibernate中单独使用


hibernate.dialect = net.sf.hibernate.dialect.MySQLDialect 
hibernate.connection.datasource=java:comp/env/jdbc/SAMPLEDB 
hibernate.show_sql=true 


3.纯java项目中(不使用框架)
public class Connector {  
  public Connection getConnect(){  
      Context ctx = null;    
      Hashtable ht = new Hashtable();    
      ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");  
      ht.put(Context.PROVIDER_URL,"t3://localhost:7001");  
      Connection conn = null;   
      try {      
     new InitialContext();
       ctx = new InitialContext(ht);new InitialContext(ht);      
      javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup ("myds");      
      conn = ds.getConnection();  
      }catch(Exception e){  
          e.printStackTrace();  
      }  
      return conn;  
  }  
}  




4.web项目中
<%@ page language="java" import="java.util.*,java.sql.*,javax.sql.*,javax.naming.*",%>
<%@ page contentType="text/html;charset=utf-8"%>




<%
try{ 
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("myds"); //这里的参数是JNDI的名称,下面的过程很熟悉不过!
Connection con = ds.getConnection(); 
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select sysdate from dual");
while(rs.next())
{
out.println("

" + rs.getString(1)); 

rs.close(); 
stmt.close();
con.close();
}catch(Exception e){ 
e.printStackTrace();
}
%>


阅读(1164) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~