1

Other similar questions on StackOverflow didn't answer my question in this area. I have this script that doesn't work, and I am wondering how to get it to work:

// beanshell script script.bsh
import com.mysql.jdbc.Driver; 
import java.sql.Connection;  
name="com.mysql.jdbc.Driver"; 
c = getClass( name ); 
c = BshClassManager.classForName( name );  // equivalent 

And the error I get is:

// Debug: getResolvedMethod cache MISS: class bsh.BshClassManager - classForName
// Debug: Searching for method: classForName( java.lang.String ) in 'bsh.BshClassManager'
// Debug: Looking for most specific method: classForName
bsh.UtilEvalError: Cannot reach instance method: classForName( java.lang.String ) from static context: bsh.BshClassManager
        at bsh.Reflect.checkFoundStaticMethod(Unknown Source)
        at bsh.Reflect.resolveJavaMethod(Unknown Source)
        at bsh.Reflect.resolveExpectedJavaMethod(Unknown Source)
        at bsh.Reflect.invokeStaticMethod(Unknown Source)
        at bsh.Name.invokeMethod(Unknown Source)
        at bsh.BSHMethodInvocation.eval(Unknown Source)
        at bsh.BSHPrimaryExpression.eval(Unknown Source)
        at bsh.BSHPrimaryExpression.eval(Unknown Source)
        at bsh.BSHAssignment.eval(Unknown Source)
        at bsh.Interpreter.eval(Unknown Source)
        at bsh.Interpreter.source(Unknown Source)
        at bsh.Interpreter.main(Unknown Source)

The documentation says it should exist.

djangofan
  • 28,471
  • 61
  • 196
  • 289
  • BshClassManager is a class name and the method you like to use is an instance method, not a static method. – Jochen Bedersdorfer Aug 18 '11 at 20:03
  • @Jochen: but the documentation tells so: http://www.beanshell.org/manual/classpath.html#Loading_Classes_Explicitly However, the `getClass()` ought to be sufficient according that documentation. Djangofan: what if you just remove that offending line? – BalusC Aug 18 '11 at 20:07
  • @BalusC - ok, removing the last line in the script did work. why didnt the documentation say anything about that? – djangofan Aug 18 '11 at 20:36
  • I have no idea. Perhaps a mistake in documentation? Anyway, I reposted the comment as an answer. – BalusC Aug 18 '11 at 20:48

2 Answers2

1

Thanks to BalusC, this was the answer:

// debug();
// addClassPath("mysql-connector-java-5.1.15.jar"); 
import com.mysql.jdbc.Driver; 
import java.sql.Connection;  
import java.sql.DriverManager; 

System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "mydb";
String driver = "com.mysql.jdbc.Driver";
String userName = "root"; 
String password = "password";
try {
    c = getClass( driver ); 
    conn = DriverManager.getConnection(url+dbName,userName,password);
    System.out.println("Connected to the database");
    conn.close();
    System.out.println("Disconnected from database");
 } catch (Exception e) {
  e.printStackTrace();
 }
Community
  • 1
  • 1
djangofan
  • 28,471
  • 61
  • 196
  • 289
1

According to the http://beanshell.org/manual/classpath.html#Loading_Classes_Explicitly documentation you're free to choose either getClass( name ) or BshClassManager.classForName( name ) to load the driver. Note also the // equivalent comment.

Perhaps the documentation just gave a wrong example of how to use the BshClassManager properly. But as it's just an "equivalent" you could remove that altogether. The getClass() should work just fine.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Ok, so it seems like that implies that BshClassManager is tied to the "default" classloader by default because of the fact that they are considered "equivilant"? If BshClassManager was a 'custom' classloader (not the default classloader), which is what I previously thought, then they wouldn't be equivilant? – djangofan May 16 '12 at 19:51