Conecting to a microsoft SQL databse?

Discussion in 'Plugin Development' started by belven000, Feb 28, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    belven000

    I've been using SQLibrary to use a MySQL server but it's currently unable to connect to my Windows Azure SQL database. Is there a way I can set this up myself or another plugin that works with this?
     
  2. Offline

    AmShaegar

  3. Offline

    belven000

    So would I just need to add it as a reference into the project and as part of the source?

    AmShaegar Thanks a lot by the way :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    AmShaegar

    I am not sure what you mean by that but you can just add it to you buildpath for compilation and also you need to include it into your jar export. In eclipse this is called something like: 'Include referenced libraries' or something like this. Should be the default setting.
     
  5. Offline

    belven000

    Yea thanks that's what I meant :D

    AmShaegar Now it's just saying I'm missing the driver. I've spooled through loads of sites and guides etc but nothing has said how to solve this straight out.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  6. Offline

    belven000

    Bump, can anyone help, I've gone through hundreds of forums trying to solve this:

    java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

    stuff.PNG
     
  7. Offline

    Quantum64

    The jar has dependencies you also need to include. When working with a large number of dependencies, using Maven is very helpful.
     
  8. Offline

    belven000

  9. Offline

    Quantum64

    OK I was wrong, you have a driver issue perhaps? I found this on Microsoft's website. Should help.

    The CLASSPATH variable is the search string that Java Virtual Machine (JVM) uses to locate the JDBC drivers on your computer. If the drivers are not listed in your CLASSPATH variable, you receive the following error message when you try to load the driver:
    java.lang.ClassNotFoundException: com/microsoft/jdbc/sqlserver/SQLServerDriver

    The JDBC driver is not part of the Java SDK. If you want to use it, you must set the classpath to include the sqljdbc.jar file or the sqljdbc4.jar file. If the classpath is missing an entry for sqljdbc.jar or sqljdbc4.jar, your application will throw the common "Class not found" exception.

    The sqljdbc.jar file and sqljdbc4.jar file are installed in the following location:
    <installation directory>\sqljdbc_<version>\<language>\sqljdbc.jar
    <installation directory>\sqljdbc_<version>\<language>\sqljdbc4.jar

    The following is an example of the CLASSPATH statement that is used for a Windows application:
    CLASSPATH =.;C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc.jar
    The following is an example of the CLASSPATH statement that is used for a Unix/Linux application:
    CLASSPATH =.:/home/usr1/mssqlserverjdbc/Driver/sqljdbc_4.0/enu/sqljdbc.jar
    You must make sure that the CLASSPATH statement contains only one Microsoft JDBC Driver for SQL Server, such as either sqljdbc.jar or sqljdbc4.jar.

    For more information, please see:
    http://support.microsoft.com/kb/313100
    http://msdn.microsoft.com/en-us/library/ms378526.aspx
     
  10. Offline

    belven000

    Quantum64
    Tried all of this but no cigar :(
    This is how eclipse does the classpath:
    <classpathentry exported="true" kind="lib" path="C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc4.jar"/>

    I've gone through, yet again, loads of guides and forums trying to solve this. This is very important to me and needs to be fixed.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  11. Offline

    AmShaegar

    Ah, forgot that. I am always using mysql which is already included in craftbukkit. You either need server owners to include the sqljdbc4.jar in their server root directory or to include the directories com and microsoft from within the sqljdbc4.jar in you plugin.jar.

    The first way is the preferred one I think. The second one is easier for server owners but if other plugins do the same the included classes may conflict afaik.

    I've never done it before but those are my thoughts.

    You could use SQLibrary if you do not want to manage database connection manually. Should have mentioned that earlier. It supports many DBS including microsoft. Although, you still need server owners to put the driver into their server root directory
     
  12. Offline

    belven000

    Where is the Root directory as I'm the server owner :D
     
  13. Offline

    AmShaegar

    the one with your craftbukkit.jar or minecraft.jar
     
  14. Offline

    belven000

    still nothing:
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver.class");
    server.PNG
     
  15. Offline

    AmShaegar

    Okay, I finally got it. But I don't like it. Not sure if this is the proper way to do it.

    1. Create a manifest file MANIFEST.MF. For example in your workspace: D:\Eclipse\TestPlugin\MANIFEST.MF
    2. Add these lines:
      Code:
      Manifest-Version: 1.0
      Class-Path: lib/sqljdbc4.jar
    3. Export your jar using the created manifest file.
      1. Either use expot wizard and select on page 4: Use existing manifest from workspace
      2. Or use something like this on your command line:
        Code:
        D:\Eclipse\TestPlugin>"C:\Program Files\Java\jdk1.7.0_45\bin\jar.exe" cfm D:/Minecraft/Server/plugins/update/TestPlugin.jar MANIFEST.MF plugin.yml -C bin .
    4. Create a directory called lib in your server's plugins directory.
    5. Insert sqljdbc4.jar into it.
    Explaination:
    With the manifest file you tell the Java VM where to search for referenced classes. In this case lib/sqljdbc4.jar. This path is relative to your plugin jar.
     
    CubieX likes this.
  16. Offline

    belven000

    Thanks I'll give it a go :D
     
  17. Offline

    Adriani6

  18. Offline

    belven000

    still nothing:

    server.PNG
    stuff.PNG
     
  19. Offline

    AmShaegar

    Console still gives ClassNotFoundException although sqljdbc4.jar is in plugins/libs/sqljdbc4.jar? Does your exported plugin.jar contain the correct manifest file, when you open it with winrar, 7z or something similar?
     
  20. Offline

    belven000

    Adriani6 I'm not using MySQL, I'm using a Microsoft SQL database
     
  21. Offline

    Adriani6

    Oh sorry. Didn't notice the title ;p
     
  22. Offline

    belven000

    Tried it still breaks:

    stuff.PNG
     
  23. Offline

    AmShaegar

    the manifest.mf inside the META-INF directory is the relevant one. Could you send me your plugin file? I'd like to do some testing with my setup here to see if it's your setup or your plugin file.

    You can upload it here if you put it into a .zip, .rar or .7z file: http://upload.wrenchbox.de
     
  24. Offline

    belven000

    My .jar or the eclipse project?
     
  25. Offline

    AmShaegar

    the jar please.
     
  26. Offline

    belven000

    Attached Files:

  27. Offline

    AmShaegar

    As I expected, the MANIFEST.MF inside the META-INF does not contain the Classpath. I dont know why because your screenshot shows, that you applied the right settings.
    [​IMG]
    You can still try my command line method which you can easily save to a .bat file or edit the MANIFEST.MF file inside your .jar manually.
     
  28. Offline

    belven000

    I'll have a look around, thanks for all you help mate ;D

    Ok so this works now I had to do this, then change it to use the one it created:

    View attachment 18327

    You sir, have made my day :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  29. Offline

    AmShaegar

    That's weird. Anyway, glad I could help.
     
Thread Status:
Not open for further replies.

Share This Page