Java DB - Iniciar Servidor NetWork dentro do codigo

4 respostas
P

alguem ae sabe como ligar o servidor java db de dentro do codigo? ja olhei a doc mais nao consigo de jeito nenhum :shock:
so consigo de dentro do netbeans, se compilar fora dele o banco nao liga =/

importei o derbyrun.jar para o meu projeto e tentei com esse codigo
try {

            Runtime rt = Runtime.getRuntime();
            
            Process proc = rt.exec("java -jar \"lib\\derbyrun.jar\" server start");

        } catch (Throwable t) {
            t.printStackTrace();
        }

mas se eu executo fora do netbeans ele da esse erro

java.net.ConnectException : Erro ao conectar no servidor 'localhost' porta '1527'. Recebida a mensagem: 'connection refused: connect'

alguem sabe o que pode ser? ou outro jeito de ligar o java db fora do netbeans?

4 Respostas

W

para iniciar diretórioDeInstalaçãoDoJavaDB\bin\startNetworkServer.bat. e para parar diretórioDeInstalaçãoDoJavaDB\bin\stopNetworkServer.bat.

se você não possui os arquivos, segue o conteúdo:

startNetworkServer.bat
@echo off

@REM Licensed to the Apache Software Foundation (ASF) under one
@REM or more contributor license agreements.  See the NOTICE file
@REM distributed with this work for additional information
@REM regarding copyright ownership.  The ASF licenses this file
@REM to you under the Apache License, Version 2.0 (the
@REM "License"); you may not use this file except in compliance
@REM with the License.  You may obtain a copy of the License at

@REM   http://www.apache.org/licenses/LICENSE-2.0

@REM Unless required by applicable law or agreed to in writing,
@REM software distributed under the License is distributed on an
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@REM KIND, either express or implied.  See the License for the
@REM specific language governing permissions and limitations
@REM under the License.

CALL "%~dp0derby_common.bat" %*

if "%_JAVACMD%"=="" goto end

if "%_USE_CLASSPATH%"=="no" goto runNoClasspath
if not "%CLASSPATH%"=="" goto runWithClasspath

:runNoClasspath
"%_JAVACMD%" %DERBY_OPTS% -classpath "%LOCALCLASSPATH%" org.apache.derby.drda.NetworkServerControl start %DERBY_CMD_LINE_ARGS%
goto end

:runWithClasspath
"%_JAVACMD%" %DERBY_OPTS% -classpath "%CLASSPATH%;%LOCALCLASSPATH%" org.apache.derby.drda.NetworkServerControl start %DERBY_CMD_LINE_ARGS%
goto end

:end
set _JAVACMD=
set DERBY_CMD_LINE_ARGS=
stopNetworkServer.bat
@echo off

@REM Licensed to the Apache Software Foundation (ASF) under one
@REM or more contributor license agreements.  See the NOTICE file
@REM distributed with this work for additional information
@REM regarding copyright ownership.  The ASF licenses this file
@REM to you under the Apache License, Version 2.0 (the
@REM "License"); you may not use this file except in compliance
@REM with the License.  You may obtain a copy of the License at

@REM   http://www.apache.org/licenses/LICENSE-2.0

@REM Unless required by applicable law or agreed to in writing,
@REM software distributed under the License is distributed on an
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@REM KIND, either express or implied.  See the License for the
@REM specific language governing permissions and limitations
@REM under the License.

CALL "%~dp0derby_common.bat" %*

if "%_JAVACMD%"=="" goto end

if "%_USE_CLASSPATH%"=="no" goto runNoClasspath
if not "%CLASSPATH%"=="" goto runWithClasspath

:runNoClasspath
"%_JAVACMD%" %DERBY_OPTS% -classpath "%LOCALCLASSPATH%" org.apache.derby.drda.NetworkServerControl shutdown %DERBY_CMD_LINE_ARGS%
goto end

:runWithClasspath
"%_JAVACMD%" %DERBY_OPTS% -classpath "%CLASSPATH%;%LOCALCLASSPATH%" org.apache.derby.drda.NetworkServerControl shutdown %DERBY_CMD_LINE_ARGS%
goto end

:end
set _JAVACMD=
set DERBY_CMD_LINE_ARGS=
P

no caso entao ficaria assim

try {

            Runtime rt = Runtime.getRuntime();
            
            Process proc = rt.exec("cmd /c start derby\\bin\\startNetworkServer.bat");

        } catch (Throwable t) {
            t.printStackTrace();
        }

porem continua dando o mesmo erro, e ele nao encontra o diretori odo java db q esta dentro da pasta do projeto =/

W
paulomn:
no caso entao ficaria assim
try {

            Runtime rt = Runtime.getRuntime();
            
            Process proc = rt.exec("cmd /c start derby\\bin\\startNetworkServer.bat");

        } catch (Throwable t) {
            t.printStackTrace();
        }
porem continua dando o mesmo erro, e ele nao encontra o diretori odo java db q esta dentro da pasta do projeto =/
não. ficaria apenas:
Process proc = rt.exec("C:\\Arquivos de programas\\Sun\\JavaDB\\bin\\startNetworkServer.bat");
mas não foi isso que sugeri.

a idéia é que você iniciei manualmente (ou crie um serviço do Windows, para que o servidor seja iniciado mesmo que você não faça logon), dando duplo clique no arquivo batch, para que o servidor de banco de dados fique independente da aplicação.

P

sei… entendi agora o que voce quis dizer…
vou ver se coloco o comando dentro do meu atalho pra ver se roda direito
vlw

Criado 8 de março de 2011
Ultima resposta 9 de mar. de 2011
Respostas 4
Participantes 2