persistence.xml如下:<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="JavaApplication1PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/comp/env/ds_1</jta-data-source>
<properties>
<property name="hibernate.connection.username" value="dp"/>
<property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="hibernate.connection.password" value="123456"/>
<property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/dispense"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
</properties>
</persistence-unit>
</persistence>代码如下:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;/**
*
* @author cy
*/
public class Main { /**
* @param args the command line arguments
*/
public static void main(String[] args) throws NamingException {
InitialContext ctx;
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/ds_1");
// TODO code application logic here
}
}就不明白,为什么会出错了?
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="JavaApplication1PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/comp/env/ds_1</jta-data-source>
<properties>
<property name="hibernate.connection.username" value="dp"/>
<property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="hibernate.connection.password" value="123456"/>
<property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/dispense"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
</properties>
</persistence-unit>
</persistence>代码如下:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;/**
*
* @author cy
*/
public class Main { /**
* @param args the command line arguments
*/
public static void main(String[] args) throws NamingException {
InitialContext ctx;
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/ds_1");
// TODO code application logic here
}
}就不明白,为什么会出错了?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;/**
*
* @author cy
*/
public class Main { /**
* @param args the command line arguments
*/
public static void main(String[] args) throws NamingException, SQLException { Main m = new Main();
m.aa();
// TODO code application logic here
}
public void aa() throws NamingException, SQLException{
InitialContext ctx;
Connection conn = null;
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/ds_1");
conn = ds.getConnection();
}
}
异常:
init:
deps-jar:
compile:
run:
Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at javaapplication1.Main.aa(Main.java:34)
at javaapplication1.Main.main(Main.java:25)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)