作者: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();
}
%>
阅读(1208) | 评论(0) | 转发(0) |