OVH Cloud OVH Cloud

java et JRuby

2 réponses
Avatar
yvon.thoravalNO-SPAM
je souhaite utiliser JRuby pour me connecter à une bdd PostgreSQL en
java ça utilise :

Class.forName("org.postgresql.Driver");

qui "marche" bien, j'essaie de faire la même chose avec JRuby :

require 'java'
module JavaLang
include_package "java.lang"
end
module JavaSQL
include_package "java.sql"
end

JavaLang::Class.forName("org.postgresql.Driver")

et cette dernière ligne me retourne une erreur :

connect2macave.rb:9: Undefined method 'forName' for Class:Class
(NameError)
from connect2macave.rb:9
from :0

bien sûr j'ai vérifié que la syntaxe est ok, dans un autre petit script
JRuby :

require 'java'

module JavaUtil
include_package 'java.util'
end

r = JavaUtil::Random.new
puts r.nextInt

qui lui marche impec...
--
yt

2 réponses

Avatar
lucsky
Yvon Thoraval wrote:

JavaLang::Class.forName("org.postgresql.Driver")

et cette dernière ligne me retourne une erreur :

connect2macave.rb:9: Undefined method 'forName' for Class:Class
(NameError)
from connect2macave.rb:9
from :0


require 'java'
Java::JavaClass.for_name( "org.postgresql.Driver" )

--
Luc Heinrich -

Avatar
yvon.thoravalNO-SPAM
Luc Heinrich wrote:

require 'java'
Java::JavaClass.for_name( "org.postgresql.Driver" )


ouais, merci beaucoup, j'avais trouvé entre-temps, mais je bloque
maintenant sur un pb au run time.

mon petit script d'essai :

require 'java'
module JavaLang
include_package "java.lang"
end
module JavaSQL
include_package "java.sql"
end

Java::JavaClass.for_name("org.postgresql.Driver")
pSQL = JavaSQL::DriverManager.getConnection("jdbc:postgresql:macave",
"postgresql", "topsecret")
[...]

qui me donne toutes ces injures (que je ne sais pas lire) :

(eval):4:in 'getConnection': Native Exception: 'class
java.sql.SQLException'; Message: No suitable driver; StackTrace:
java.sql.SQLException: No suitable driver (RuntimeError)
at java.sql.DriverManager.getConnection(DriverManager.java:532)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
a:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
Impl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at
org.jruby.javasupport.JavaMethod.invokeWithExceptionHandling(JavaMethod.
java:148)
at
org.jruby.javasupport.JavaMethod.invoke_static(JavaMethod.java:135)
at
org.jruby.javasupport.JavaMethod.callIndexed(JavaMethod.java:197)
at
org.jruby.runtime.IndexedCallback.execute(IndexedCallback.java:72)
at
org.jruby.internal.runtime.methods.CallbackMethod.call(CallbackMethod.ja
va:63)
at org.jruby.RubyModule.call0(RubyModule.java:758)
at org.jruby.RubyModule.call(RubyModule.java:719)
at
org.jruby.evaluator.EvaluateVisitor.visitCallNode(EvaluateVisitor.java:4
27)
at org.jruby.ast.CallNode.accept(CallNode.java:64)
at
org.jruby.evaluator.EvaluateVisitor.eval(EvaluateVisitor.java:218)
at
org.jruby.evaluator.EvaluateVisitor.visitLocalAsgnNode(EvaluateVisitor.j
ava:934)
at org.jruby.ast.LocalAsgnNode.accept(LocalAsgnNode.java:57)
at
org.jruby.evaluator.EvaluateVisitor.eval(EvaluateVisitor.java:218)
at
org.jruby.evaluator.EvaluateVisitor.visitNewlineNode(EvaluateVisitor.jav
a:1020)
at org.jruby.ast.NewlineNode.accept(NewlineNode.java:65)
at
org.jruby.evaluator.EvaluateVisitor.eval(EvaluateVisitor.java:218)
at
org.jruby.evaluator.EvaluateVisitor.visitBlockNode(EvaluateVisitor.java:
323)
at org.jruby.ast.BlockNode.accept(BlockNode.java:89)
at
org.jruby.evaluator.EvaluateVisitor.eval(EvaluateVisitor.java:218)
at org.jruby.RubyObject.eval(RubyObject.java:362)
at
org.jruby.internal.runtime.methods.DefaultMethod.call(DefaultMethod.java
:73)
at org.jruby.RubyModule.call0(RubyModule.java:758)
at org.jruby.RubyModule.call(RubyModule.java:719)
at
org.jruby.evaluator.EvaluateVisitor.visitCallNode(EvaluateVisitor.java:4
27)
at org.jruby.ast.CallNode.accept(CallNode.java:64)
at
org.jruby.evaluator.EvaluateVisitor.eval(EvaluateVisitor.java:218)
at
org.jruby.evaluator.EvaluateVisitor.visitLocalAsgnNode(EvaluateVisitor.j
ava:934)
at org.jruby.ast.LocalAsgnNode.accept(LocalAsgnNode.java:57)
at
org.jruby.evaluator.EvaluateVisitor.eval(EvaluateVisitor.java:218)
at
org.jruby.evaluator.EvaluateVisitor.visitNewlineNode(EvaluateVisitor.jav
a:1020)
at org.jruby.ast.NewlineNode.accept(NewlineNode.java:65)
at
org.jruby.evaluator.EvaluateVisitor.eval(EvaluateVisitor.java:218)
at
org.jruby.evaluator.EvaluateVisitor.visitBlockNode(EvaluateVisitor.java:
323)
at org.jruby.ast.BlockNode.accept(BlockNode.java:89)
at
org.jruby.evaluator.EvaluateVisitor.eval(EvaluateVisitor.java:218)
at org.jruby.runtime.ThreadContext.eval(ThreadContext.java:116)
at org.jruby.Ruby.eval(Ruby.java:243)
at org.jruby.Main.runInterpreter(Main.java:122)
at org.jruby.Main.main(Main.java:89)

from (eval):4:in 'getConnection'
from connect2macave.rb:11
from :0

--
yt