jEdit Community - Resources for users of the jEdit Text Editor
Database access in macros
Submitted by phade on Wednesday, 12 March, 2008 - 08:45
Hello,

Knowing how powerful BeanShell really is I wanted to see if it can access databases (oracle). So I wrote quick and small script (real servers replaced of course Smiling):

import java.sql.*;

DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());        

Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@my.oracle.server:1521:MYBASE", "user", "pass");

Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select BANNER from SYS.V_$VERSION");
        
while (rset.next())
     print (rset.getString(1));   // Print col 1
stmt.close();

I ran it and got:

java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@my.oracle.server:1521:MYBASE

First idea was that it is a classpath problem (BeanShell can not access jdbc oracle driver) so I wrote another quickie to see what classpath BeanShell actually sees.

import java.net.URL;
import java.net.URLClassLoader;

ClassLoader = sysClassLoader = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();

for(int i=0; i

And yes sure enough it could not see my oracle jdbc in classpath. So I did some testing and discovered that when running jEdit instead of

java -cp oracle.jar -jar jedit.jar
like this
java -cp oracle.jar:jedit.jar org.gjt.sp.jedit.jEdit

then BeanShell finds my oracle jdbc and classpath macro shows also oracle and database macro can connect to oracle server.

So I want to know if it somehow possible to specify classpath when running jEdit like it is usually ran (java -jar jedit.jar) or use some other methods (other than running jedit with org.gjt.sp.jedit.jEdit) to let BeanShell know my new classpath?

Keep up the good work,
Phade
Comment viewing options
Select your preferred way to display the comments and click 'Save settings' to activate your changes.
Database access in macros
by phade on Wed, 12/03/2008 - 08:55

Couple of things:

1. I am using jEdit4.3pre12 with java 1.6.0.
2. Classpath macro got screwed up in posting process:
import java.net.URL;
import java.net.URLClassLoader;

ClassLoader = sysClassLoader = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();

for(int i=0; i<urls.length; i++) {
    print("Class : " + urls[i].getFile());
}

3. Also moderators can delete my other incorrectly formatted duplicate post.

Phade
 
Try the SQL plugin. There's a
by elberry on Wed, 12/03/2008 - 16:31
Try the SQL plugin. There's a place to specify where to find JDBC drivers. The jars defined there will be included in jEdit's classpath once jEdit has started up.

If you don't want to install the SQL plugin. It'll help to know how jEdit loads it's plugins.

Under your installation directory and your settings directory (usually found in your home directory ~/.jedit) you'll see a "jars" directory. Any jars under either of these directories will automatically get included in jEdit's classpath when jEdit starts up.

It's probably better if you use either of these methods as a new installation of jEdit might overwrite your shortcut and thus the classpath changes you made there in.

Hope that helps. Smiling

Also, if you just want to run SQL statements against a database, the SQL plugin will let you do that as well.

Learn from the past. Live in the present. Plan for the future.
11101000
 
Thank you for your answer,
by phade on Thu, 13/03/2008 - 09:12
Thank you for your answer,

I have tried both SQL and DBTerminal plugins and got them to work by putting oracle jdbc jars in ~/.jedit/jars directory and I do know that jars placed in .jedit/jars get loaded in classpath as far as plugins are concerned.

However macros do not seem to see the jars in classpath when you only putting them in ~/.jedit/jars directory. And putting them in jdbc path using SQL plugin does not help also (it only helps for SQL plugin itself but not macros).

You can try for yourself running the classpath macro included in my first post. Enable activity log to see the output that print gives.

NB: I would be interested to know if it acts differently for you.

The way I run it to get it works for macros I found by looking in jedit.jar MANIFEST file so I can always look there when new jEdit version arrives and make proper replacements. But still I would like to know if there is another way.
 
Ah. Right. I missed that you
by elberry on Fri, 14/03/2008 - 17:48
Ah. Right. I missed that you were using the System ClassLoader (sorry). Try using some reflection and the JARClassLoader.

[code]
import org.gjt.sp.jedit.JARClassLoader;
import java.sql.*;

JARClassLoader jarClassLoader = new JARClassLoader();
Class driverClass = jarClassLoader.loadClass("oracle.jdbc.OracleDriver");
if(driverClass != null) {
	Driver driver = driverClass.newInstance();
	if(driver != null) {
		DriverManager.registerDriver(driver);
	}
}

Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@my.oracle.server:1521:MYBASE", "user", "pass");
[/code]

This worked for me, but I'm using SQL plugin to manage the JDBC jars. The SQL plugin injects your jdbc jars into jEdit's plugin JARClassLoader.

Learn from the past. Live in the present. Plan for the future.
11101000
 
Thank you again
by phade on Thu, 20/03/2008 - 09:30
I have to try this out. Looks promising.

Thanks
 
Still does not work
by phade on Thu, 10/04/2008 - 08:51
Tried it out (both commented and outcommented versions), and included some additional debug output. Also SQL plugin under options JDBC I have full path to my oracle.jar (Classpath elements). But it always gives me "Test output and the driver that I tried to load" and none of the debug output I included: ("Can not register driver" or "Can not load driver") and also displays the error I got before. When I include oracle.jar from my classpath (by starting jedit with java command line) everything works fine.
import org.gjt.sp.jedit.JARClassLoader;
import java.sql.*;

JARClassLoader jarClassLoader = new JARClassLoader();
Class driverClass = jarClassLoader.loadClass("oracle.jdbc.driver.OracleDriver");
//jarClassLoader.loadClass("oracle.jdbc.OracleDriver");

print("Test output: " + driverClass);

if(driverClass != null) {
    Driver driver = driverClass.newInstance();
    if(driver != null) {
       //DriverManager.registerDriver(driver); 
       DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    } else {
        print("Can not register driver");
    }
} else {
    print("Can not load driver");
}
//exit();
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());        

Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@my.host.name:1521:MYDB", "myuser", "mypass");

Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select BANNER from SYS.V_$VERSION");
        
while (rset.next())
     print (rset.getString(1));   // Print col 1
stmt.close();
 
Hmm. Think you should use a DataSource instead.
by elberry on Sun, 13/04/2008 - 02:22
Phade,
I think you should use a DataSource instead. In a lot of ways, it's preferred over the Driver and DriverManager anyway. You would instantiate it as you would the Driver, and I think it's easier to use.

Here's an example using MySQL (since I don't want to install Oracle Express on my Mac).

[code]
import org.gjt.sp.jedit.JARClassLoader;
import java.sql.*;
import javax.sql.*;

JARClassLoader jarClassLoader = new JARClassLoader();
String dataSourceClassName = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource";
String username = "myUsername";
String password = "myPassword";
String databaseUrl = "jdbc:mysql://localhost:3306/mysql";
Class dataSourceClass = jarClassLoader.loadClass(dataSourceClassName);

Macros.message(view, "DataSource class: " + dataSourceClass);

if(dataSourceClass != null) {
   DataSource dataSource = dataSourceClass.newInstance();
   if(dataSource != null) {
      Macros.message(view, "DataSource is not null, using it.");
      dataSource.setUrl(databaseUrl);
      Connection conn = null;
      try {
         conn = dataSource.getConnection(username, password);
      } catch(Exception e) {
         e.printStackTrace();
      }
      if(conn != null) {
         Macros.message(view, "Connection established - trying a query.");
         PreparedStatement statement = null;
         try {
            statement = conn.prepareStatement("SHOW DATABASES");
         } catch(Exception e) {
            e.printStackTrace();
         }
         if(statement != null) {
            Macros.message(view, "Statement created - executing.");
            ResultSet resultSet = null;
            try {
               resultSet = statement.executeQuery();
            } catch(Exception e) {
               e.printStackTrace();
            }
            if(resultSet != null) {
               Macros.message(view, "Statement executed successfully");
               resultSet.close();
            }
            statement.close();
         } else {
            Macros.message(view, "Statement could not be created, check Activity Log for errors.");
         }
         conn.close();
      } else {
         Macros.message(view, "Connection could not be made, check Activity Log for errors.");
      }
   } else {
      Macros.message(view, "DataSource was null.");
   }
}
[/code]

There's a lot of debug statements in there, which you can remove.

Hope that works for you.

Learn from the past. Live in the present. Plan for the future.
11101000
User login
Browse archives
« April 2024  
MoTuWeThFrSaSu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
Poll
Are you interested in language packs for jEdit?
Yes, and I could help maintain translations
26%
Yes, I'd like to have translations
32%
Indifferent
35%
No, that'd be bad (please comment)
7%
Total votes: 1093
Syndication
file   ver   dls
German Localization light   4.4.2.1   82348
Context Free Art (*.cfdg)   0.31   46055
JBuilder scheme   .001   18495
BBEdit scheme   1.0   18116
ColdFusion scheme   1.0   18024
R Edit Mode - extensive version   0.1   17473
Advanced HTML edit mode   1.0   16206
Matlab Edit Mode   1.0   16068
jEdit XP icons   1.0   15229
XP icons for jEdit   1.1   14293