Class.forName?

Jean-Francois Denise jean-francois.denise at oracle.com
Sat Feb 1 10:15:33 PST 2014


Hi Tom,
some JDBC initialisation code  that works:
Java.type("org.apache.derby.jdbc.ClientDriver");
var connection = java.sql.DriverManager.getConnection("jdbc:derby://localhost:1527/sample;user=app;password=app", null);

JF

On Feb 1, 2014, at 4:07 PM, Tom McGinn <tom.mcginn at oracle.com> wrote:

> Thanks Jim -
> 
> That's what I needed.  Here's the working code:
> 
> var driverClass = Java.type("org.apache.derby.jdbc.ClientDriver");
> var driver = new driverClass();
> var conn = driver.connect("jdbc:derby://localhost:1527/sample;user=app;password=app", null);
> var stmt = conn.createStatement();
> var rs = stmt.executeQuery("SELECT NAME FROM Customer");
> while (rs.next()) {
>    print(rs.getString(1));
> }
> 
> However, this code does not work:
> 
> var driverManager = Java.type("java.sql.DriverManager");
> var conn = driverManager.getConnection("jdbc:derby://localhost:1527/sample;user=app;password=app");
> 
> A SQLException is thrown:  No suitable driver found for jdbc:derby://localhost:1527/sample;user=app;password=app
> 
> I may have missed this, but I guess that static initialization blocks are not executed?  DriverManager would have executed
> this during initialization:
> 
>    static {
>        loadInitialDrivers();
>        println("JDBC DriverManager initialized");
>    }
> 
> Thanks again,
> 
> Tom
> 
> 
> On 1/31/2014 8:20 PM, Jim Laskey (Oracle) wrote:
>> Since I don't have the jar handy, I'm just guessing, but why wouldn't you just;
>> 
>> jjs> var driverClass = Java.type("org.apache.derby.jdbc.ClientDriver");
>> 
>> -- Jim
>> 
>> On Jan 31, 2014, at 7:43 PM, Tom McGinn <tom.mcginn at oracle.com> wrote:
>> 
>>> Hello -
>>> 
>>> I am trying to create a JS file that illustrates using Java classes mixed with JS.
>>> 
>>> I am trying to connect to the sample JavaDB instance, but am not able to load the driver class.
>>> 
>>> jjs -cp /home/tmcginn/jdk1.8.0/db/lib/derbyclient.jar
>>> jjs> var driver = "org.apache.derby.jdbc.ClientDriver";
>>> jjs> Java.type("java.lang.Class").forName(driver);
>>> java.lang.RuntimeException: java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver
>>> 
>>> I think I am missing something obvious....
>>> 
>>> I am running this on Oracle Linux 6 u3 on an OVM.
>>> 
>>> Thanks,
>>> 
>>> Tom
>>> 
>>> -- 
>>> 
>>> 
>>> Tom McGinn | Java Curriculum Developer
>>> OracleServer Technologies Curriculum Development
>>> Phone: +1.781.442.0621
>>> Mail: tom.mcginn at oracle.com
>>> Blog: Java Training Beat <https://blogs.oracle.com/javatraining/>
>>> 
> 
> -- 
> 
> 
> Tom McGinn | Java Curriculum Developer
> OracleServer Technologies Curriculum Development
> Phone: +1.781.442.0621
> Mail: tom.mcginn at oracle.com
> Blog: Java Training Beat <https://blogs.oracle.com/javatraining/>
> 



More information about the nashorn-dev mailing list