inserting data into MS SQL server 2000/2005 - JEE application

Hello everyone,

Looking at the Tutorial, roster, under ejb, it uses EntityManager and persistence to insert and access data from the, what i presume the Java DB. So i tried it with my applications. I get no errors when I insert the data. Only that it is not going into the DB, which is an external database, MS SQL server. Does this mean I can only insert and access data from the database through sql statements?

Help!

Thanks

eve

[462 byte] By [evepokuaa] at [2007-11-26 15:37:42]
# 1

Hello everyone,

I have made progress on this. And if anyone requires more details check this website:

http://www.webagesolutions.com/knowledgebase/javakb/jkb006/index.html

Also please check your firewalls when dealing with external DB. For example, the windows firewall blocks the server from access MS server DB. So in an actual of fact, my above problem was working, only that it was probably directing data to the Java DB included in the application server 9.

The progress so far, is good. The persistence xml seems to create the Tables into the database. But it does not enter data into the database. Below is the error I get:

java.lang.ClassCastException: com.sun.gjc.spi.DataSource

at productinfor.AppClientMachines$saveListener.actionPerformed(AppClientMachines.java:138)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

at java.awt.Component.processMouseEvent(Component.java:5501)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)

at java.awt.Component.processEvent(Component.java:5266)

at java.awt.Container.processEvent(Container.java:1966)

at java.awt.Component.dispatchEventImpl(Component.java:3968)

at java.awt.Container.dispatchEventImpl(Container.java:2024)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)

at java.awt.Container.dispatchEventImpl(Container.java:2010)

at java.awt.Window.dispatchEventImpl(Window.java:1778)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Aslo the error is point to this in my client:

Machinesbean sebean=(Machinesbean)ctx.lookup("jdbc/ProductDetailsDB");

And below is the xml file:

<?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="PRODUCTINFOR-ejbPU" transaction-type="JTA">

<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>

<jta-data-source>jdbc/ProductDetailsDB</jta-data-source>

<properties>

<property name="toplink.ddl-generation" value="create-tables"/>

</properties>

</persistence-unit>

</persistence>

Somebody also suggestion I make my persistence xml file as follows to use MS server 2000/2005:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

<persistence-unit name="PRODUCTDETAILS3-ejbPU">

<jta-data-source>jdbc/ProductDetailsDB</jta-data-source>

<properties>

<!--Use the java2db feature -->

<property name="toplink.ddl-generation" value="drop-and-create-tables"/>

<!-- Generate the sql specific to MS SQL database -->

<property name="toplink.platform.class.name"

value="oracle.toplink.essentials.platform.database.SQLServerPlatform"/>

</properties>

</persistence-unit>

</persistence>

Any suggestions will help. If not I hope this helps somebody else. Thanks for all your help

eve

Message was edited by:

evepokua

evepokuaa at 2007-7-8 21:55:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

You also have to specify your datbase's URL, username and password into persistence.xml, for example:

<property name="toplink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>

<property name="toplink.jdbc.url" value="jdbc:derby://localhost:1527/testdb;retrieveMess

agesFromServerOnGetMessage=true;"/>

<property name="toplink.jdbc.user" value="USER"/>

<property name="toplink.jdbc.password" value="PASSWORD"/>

An explanation of all the configuration settings can be found at:

http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-extensions.html#TopLinkJDBC

m_fuchsa at 2007-7-8 21:55:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Hello everyone,

Thanking you to everybody. I have resolved the issue and now able to create the database and also insert data into it. The problems I was having was not much to do with the persistence xml file:

<?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="PRODUCTINFOR-ejbPU" transaction-type="JTA">

<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>

<jta-data-source>jdbc/ProductDetailsDB</jta-data-source>

- <properties>

<property name="toplink.ddl-generation" value="create-tables" />

</properties>

</persistence-unit>

</persistence>

The problems were:

first of all, in my session bean implementation class, I was meant to make a reference to the global JNDI as for example:

@Stateful(name="stabean", mappedName="ejb/MachinesbeanJNDI")

Then upon calling it in my client as in:

Machinesrem sebean=(Machinesrem)ctx.lookup("ejb/MachinesbeanJNDI");

sebean.createMachine();

It looks for the JNDI global reference which I stated above in my session bean.

And also I made a mistake by calling my entity bean class instead of my remote interface. Now I should have known this as I already done some basics of J2ee before.

Now, with regards to the Database, I was able to ping successfully within my admin console, and since the properties were already set within the JDBC resources, there was no requirement of stating the username and password in the persistence.xml file.

I also got a lot of help from:

http://forum.java.sun.com/post!reply.jspa?messageID=9461960

Thanks.

eve

evepokuaa at 2007-7-8 21:55:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...