Skip to main content

Java Web Start and Derby

3 replies [Last post]
wattlee
Offline
Joined: 2006-04-14

Hello,

In my simplest Java Web Start application, I try to call following class DerbyTest.execute() to test Derby worked with Java Web Start or not:

import java.sql.*;

public class DerbyTest {
public String execute() {
// Both the remote and local JDatastore drivers use the same
// driver string:
String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";

// Use this string for the local driver:
String URL = "jdbc:derby:test;create=true";

boolean s_open=false, c_open=false;
Statement stmt = null;
Connection con = null;
String conString = null;
String text = null;

try
{
Class.forName(DRIVER);
con = DriverManager.getConnection(URL, "APP", "");
c_open = true;

stmt = con.createStatement();
s_open = true;

stmt.executeUpdate("create table HelloJDBC" +
"(COLOR varchar(15), " +
" NUMBER int, " +
" PRICE float)");
stmt.executeUpdate("insert into HelloJDBC values('Red', 1, 7.99)");
stmt.executeUpdate("insert into HelloJDBC values('Blue', 2, 8.99)");
stmt.executeUpdate("insert into HelloJDBC values('Green', 3, 9.99)");

ResultSet rs = stmt.executeQuery("select * from HelloJDBC");

text = formatResultSet(rs);
System.out.println(text);

stmt.executeUpdate("drop table HelloJDBC");

return text;
}
catch(Exception e)
{

System.out.println(e);

text = e.toString();
}

try
{
if(s_open)
stmt.close();
}
catch(Exception e2)
{
System.out.println(e2.toString());
text = e2.toString();
}

// This way the connection will be closed even when exceptions are thrown
// earlier. This is important, because you may have trouble reopening
// a JDatastore file after leaving a connection to it open.
try
{
if(c_open)
con.close();
}
catch(Exception e3)
{
System.out.println(e3.toString());
text = e3.toString();
}

return text;
}

public static String formatResultSet(ResultSet rs)
{
try
{
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
StringBuffer ret = new StringBuffer(500);

for (int i = 1; i <= numberOfColumns; i++)
{
String columnName = rsmd.getColumnName(i);
ret.append(columnName + "," );
}
ret.append("\n");
while (rs.next())
{
for (int i = 1; i <= numberOfColumns; i++)
ret.append(rs.getString(i) + "," );
ret.append("\n");
}
return(ret.toString());
}
catch(Exception e)
{
return e.toString();
}
}

}

If the class is executed in a Java application, it's ok.
DerbyTest.execute() return "COLOR, NUMBER, PRICE...".

If deployed in Java Web Start application, DerbyTest.execute() return "Java exception: 'access denied (java.util.PropertyPermission user.dir read): java.security.AccessControlException'."

The jnlp file is as the following:
<?xml version="1.0" encoding="UTF-8"?>

test
test

and there is an exception:
StandardWrapperValve[default]: Servlet.service() for servlet default threw exception

java.net.SocketException: Software caused connection abort: socket write error

java.net.SocketException: Software caused connection abort: socket write error

at java.net.SocketOutputStream.socketWrite0(Native Method)

at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)

at java.net.SocketOutputStream.write(SocketOutputStream.java:136)

at org.apache.catalina.connector.ResponseBase.flushBuffer(ResponseBase.java:674)

at org.apache.catalina.connector.HttpResponseBase.flushBuffer(HttpResponseBase.java:764)

at org.apache.catalina.connector.ResponseBase.write(ResponseBase.java:647)

at org.apache.catalina.connector.ResponseStream.write(ResponseStream.java:312)

at org.apache.catalina.connector.http.HttpResponseStream.write(HttpResponseStream.java:189)

at org.apache.catalina.servlets.DefaultServlet.copyRange(DefaultServlet.java:1921)

at org.apache.catalina.servlets.DefaultServlet.copy(DefaultServlet.java:1670)

at org.apache.catalina.servlets.DefaultServlet.serveResource(DefaultServlet.java:1215)

at org.apache.catalina.servlets.DefaultServlet.doGet(DefaultServlet.java:525)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2347)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027)

at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125)

at java.lang.Thread.run(Thread.java:534)

My question is whether I can create a local Derby DB in local or not.

If yes, what should I do?

Thanks in advance,

Watt

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
darkgreen
Offline
Joined: 2008-06-18

Java 6.0 includes derby.jar, but how do I get my application to find it? I can see it in the Sun directory.

On Windows I get the ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver error.

What's the point of including Derby in Java SE 6.0 if you then have to mess around with CLASSPATH etc on clients machines? It would be easier to host derby.jar myself!

I can't see why this shouldn't just work.

mthornton
Offline
Joined: 2003-06-10

Derby is merely packaged with JDK6. It is not included in JRE6 nor is it on the system classpath in JDK6. If you want WebStart clients to be able to use Derby you have to deliver it too over WebStart.

andyherrick
Offline
Joined: 2005-04-17

Two problems here.
First, you will require all-permissions to run this in a secure environment. For this you must sign the jars, and add to the jnlp file:

Second, Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
will only work if org.apache.derby.jdbc.EmbeddedDriver can be loaded by the SystemClassLoader. Classes and resources in a jar listed in the jnlp file can only be accessed by the JNLPClassLoader, so instead you need the form:
Clas.forName("org.apache.derby.jdbc.EmbeddedDriver", initialize, classloader);
where classloader can be the return from
Thread.currentThread().getContextClassLoader();
(in order to get the JNLPClassLoader. you could also use this.getClass().getClassLoader()).

/Andy