MySQL connect question

Hey guys, I’m trying to get JRuby to talk to mysql. Can’t beleive how
much of a pain in the butt this is! After searching the web I’m
beginning to believe this is a right of passage!

Satish’s blog post here:
http://rubylearning.com/blog/2007/05/05/connect-jruby-to-mysql-using-jdbc/
has helped…but I’m getting an SQLException. Maybe its how I’m setting
the password?

Anyway heres my code…if someone could eyeball it and tell me where I’m
screwing up I’d appreciated it. Thanx in advance…

require ‘java’
require ‘rubygems’
require ‘jdbc/mysql’

module JavaLang
include_package “java.lang”
end
module JavaSql
include_package ‘java.sql’
end
begin
com.mysql.jdbc.Driver.new
conn =
JavaSql::DriverManager.getConnection(“jdbc:mysql://127.0.0.1:327280/clist”,
“root”, “wintas”);
stmt = conn.createStatement
rs = stmt.executeQuery(“select version();”)
rs.close
stmt.close
conn.close()
rescue JavaLang::ClassNotFoundException
puts “ClassNotFoundException”
rescue JavaSql::SQLException
puts “SQLException”
end

Can you past the full exception and stack trace?

On Wed, 2011-02-16 at 18:39 +0100, Mark Locklear wrote:

screwing up I’d appreciated it. Thanx in advance…
end
rescue JavaLang::ClassNotFoundException
puts “ClassNotFoundException”
rescue JavaSql::SQLException
puts “SQLException”
end


Nick G.
Developer @ Media Service Provider
+44 207 729 4797

…thanx for the quick reply Nick. I commented out the rescue, and get…

[[email protected] ~]$ jruby mysql2.rb
sun/reflect/NativeConstructorAccessorImpl.java:-2:in `newInstance0’:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications
link failure (NativeException)

The last packet sent successfully to the server was 0 milliseconds ago.
The driver has not received any packets from the server.
from sun/reflect/NativeConstructorAccessorImpl.java:57:in
newInstance' from sun/reflect/DelegatingConstructorAccessorImpl.java:45:innewInstance’
from java/lang/reflect/Constructor.java:532:in newInstance' from com/mysql/jdbc/Util.java:409:inhandleNewInstance’
from com/mysql/jdbc/SQLError.java:1118:in
createCommunicationsException' from com/mysql/jdbc/MysqlIO.java:343:in
from com/mysql/jdbc/ConnectionImpl.java:2308:in connectOneTryOnly' from com/mysql/jdbc/ConnectionImpl.java:2122:increateNewIO’
from com/mysql/jdbc/ConnectionImpl.java:774:in <init>' from com/mysql/jdbc/JDBC4Connection.java:49:in
from sun/reflect/NativeConstructorAccessorImpl.java:-2:in
newInstance0' from sun/reflect/NativeConstructorAccessorImpl.java:57:innewInstance’
from sun/reflect/DelegatingConstructorAccessorImpl.java:45:in
newInstance' from java/lang/reflect/Constructor.java:532:innewInstance’
from com/mysql/jdbc/Util.java:409:in handleNewInstance' from com/mysql/jdbc/ConnectionImpl.java:375:ingetInstance’
from com/mysql/jdbc/NonRegisteringDriver.java:289:in connect' from java/sql/DriverManager.java:620:ingetConnection’
from java/sql/DriverManager.java:200:in getConnection' from mysql2.rb:13:infile

Thanx Nick…I had the same suspicion. Changed the port back to 3306,
and everything worked OK. The reason I had it set to 327280 is…

[[email protected] ~]$ netstat -an | grep -i mysql
unix 2 [ ACC ] STREAM LISTENING 327280
/var/lib/mysql/mysql.sock
unix 3 [ ] STREAM CONNECTED 586623
/var/lib/mysql/mysql.sock

Not sure what the heck that ‘Listening’ means. For others who might want
to use the code…his the working version…

require ‘java’
require ‘rubygems’
require ‘jdbc/mysql’

module JavaLang
include_package “java.lang”
end
module JavaSql
include_package ‘java.sql’
end
begin
com.mysql.jdbc.Driver.new
conn =
JavaSql::DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/clist”,
“root”, “wintas”);
stmt = conn.createStatement
rs = stmt.executeQuery(“select * from post”)
while (rs.next) do
puts rs.getString(“post_location”)
end
rs.close
stmt.close
conn.close()
rescue JavaLang::ClassNotFoundException
puts “ClassNotFoundException”
end

I don’t think port numbers go up to 327280 (take a look at
http://www.iana.org/assignments/port-numbers). If that was a typo, check
that you can connect to the host and port that you are using with
something like telnet.

On Wed, 2011-02-16 at 18:48 +0100, Mark Locklear wrote:

newInstance' from sun/reflect/DelegatingConstructorAccessorImpl.java:45:innewInstance’


Nick G.
Developer @ Media Service Provider
+44 207 729 4797

Yeah, that’s a unix socket, not a tcp socket. Try netstat --inet
–inet6 --listen to show listening tcp sockets.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs