作者:tingfengjushi(听风居士)
1.spring+hibernate
<!-- java:comp/env/jdbc/myds -->
myds//jndi名称weblogic11g中的配置
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- /com/env/jdbc/myds -->
true
true
<!-- create-->
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项目中
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.close();
stmt.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
%>
1.spring+hibernate
<!-- java:comp/env/jdbc/myds -->
myds//jndi名称weblogic11g中的配置
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- /com/env/jdbc/myds -->
true
true
<!-- create-->
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项目中
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();
}
%>
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26400547/viewspace-1251747/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/26400547/viewspace-1251747/