Author Topic: Plug-in using JDBC  (Read 3776 times)

0 Members and 1 Guest are viewing this topic.

Offline ghostofkc

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Realm of the Knights of Chaos
Plug-in using JDBC
« on: April 17, 2009, 02:06:57 pm »
I started off trying to make my own bot to do some semi specialized things… but I am retarded. Then I found your program.
So I am having issues with making JDBC work in the context of a plug-in for JavaOp2.

Admittedly I suck at java but, I have the connection working and performing queries outside of a plug-in. I looked on the forum and pervious posts about jdbc use in a plugin (there is one from 05) but they do not have any conclusions.


The error I get is “com.mysql.jdbc.Driver” which I understand (I think) to be a class path issue. But, I have included and referenced it just like I did in the standalone script (where it works).


So I guess the question is… what is different about using jdbc in the context of a plug-in / what am I fucking up.
Code: [Select]
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        else if(command.equalsIgnoreCase("playgame"))
        {
        //error catch
        if(args.length == 0)
                throw new CommandUsedImproperly("Error.", user, command);
        //end error catch
       
        Connection conn = null;

            try
            {
                String userName = "USERNAME";
                String password = "PASSWORD";
                //String url = "jdbc:mysql://localhost/ladder";
                String url = "jdbc:mysql://knightsofchaos.org/dschott_ladder";
                Class.forName ("com.mysql.jdbc.Driver").newInstance ();
                conn = DriverManager.getConnection (url, userName, password);
                out.sendTextUserPriority(user, "Database connection established.", loudness, PRIORITY_LOW);
                ///////////////////////////////////////////////////////////////////////////////////////////////////
                //query perform
                Statement stmt = conn.createStatement();
                ResultSet rs;
                rs = stmt.executeQuery("SELECT id FROM users WHERE alias = 'Creed'"); //test line
                while ( rs.next() ) {
                    String id = rs.getString("id");
                    out.sendTextUserPriority(user, id, loudness, PRIORITY_LOW);
                   
                    //converting from sting to int for id so checks can be done
                    int idNum = Integer.valueOf(id).intValue();
                    ///////////////////////////////////////////////////////////////////////////////////////////////////
                    if (idNum == 0)
                    {
                    out.sendTextUserPriority(user, "sorry! username does not exist.", loudness, PRIORITY_LOW);
                    }
                }
                conn.close();
                //end query preform
               
            }
            catch (Exception e)
            {
            /*
            * this is erroring because jdbc is not loading or something. /cry sad panda - 4/17/2009 ghost
            */
            out.sendTextUserPriority(user, "Cannot connect to database server.", loudness, PRIORITY_LOW);
            out.sendTextUserPriority(user, e.getMessage(), loudness, PRIORITY_LOW);
                //System.err.println ("Cannot connect to database server");
                //System.err.println(e.getMessage());
            }
            //this trys to close the conection after the query is done
            finally
            {
                if (conn != null)
                {
                    try
                    {
                        conn.close ();
                       
                        //this is active for debuging will not talkie once it is working.
                        out.sendTextUserPriority(user, "Database connection terminated.", loudness, PRIORITY_LOW);
                    }
                    catch (Exception e) { /* ignore close errors */ }
                }
            }
            //end connection close
        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////


below links are to all of the code if anyone cares
http://www.knightsofchaos.org/bot/plugin.txt -  jdbc connection plugin for javaop2
http://www.knightsofchaos.org/bot/standalone.txt - jdbc connection

I know this is probably a lame question but I cant find an answer to this on google or anything… probably because I don’t know what the problem is so I am looking up the wrong thing but what ever.


Thanks for the help.

« Last Edit: April 17, 2009, 02:08:59 pm by ghostofkc »

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: Plug-in using JDBC
« Reply #1 on: April 17, 2009, 02:30:48 pm »
Use a URLClassLoader pointed at your MySQL JAR to get a Class for com.mysql.jdbc.Driver, instantiate (.newInstance()) that Class, and register it with the DriverManager.

Here's an example of how to use a URLClassLoader: net.bnubot.JARLoader, or you can look at how JavaOp loads plugins.
And for registering the Driver: net.bnubot.db.conf.CayenneConfiguration, circa line 76.
« Last Edit: April 17, 2009, 02:41:11 pm by Camel »

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

Offline ilaggoodly

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Plug-in using JDBC
« Reply #2 on: April 17, 2009, 07:56:25 pm »
coinicidentally my plugin also uses JDBC (with mysql driver)
You need to make sure the driver is installed correctly, this means dropping it in jre6/lib/ext/
or loading it manually as stated above.

http://www.uni-bonn.de/~s6alknau/code/VIPWatch/VIPWatchnew.html

you can see the code for my bot there (third blob down, Database.java)

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: Plug-in using JDBC
« Reply #3 on: April 19, 2009, 12:21:55 am »
Putting the MySQL JAR on the global system classpath is a bad idea; it will override any program-specific JAR you might try to use. If a program depends on a different version of that library, you will probably never figure out why it's not working.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!