Solved Error when connecting to MS SQL Server

Discussion in 'Spigot Plugin Development' started by winjw7, Mar 29, 2020.

  1. Hey everyone, I've been attempting to get my code to connect to my MS SQL database for awhile now and can't seem to get it to work. Everything I try to find for resources on help tends to be for MySQL or SQLite. Before I switched everything to SQLite for this reason because I could get it working with the amount of help out there, but now I actually need to use MS SQL due to read locks and such.

    The error I'm getting is that the sql driver class can't be found.
    Code (Text):
     java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
    [14:02:56] [Server thread/WARN]:        at java.net.URLClassLoader.findClass(Unknown Source)
    [14:02:56] [Server thread/WARN]:        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:146

    I created a folder named lib in the server path and put in the mssql-jdbc-8.2.2.jre8 that I downloaded off of Microsoft's site however that doesn't seem to work.

    Here's the code that I'm using:
    Code (Text):
        public Connection getSQLConnection()
        {
              try
              {
                  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    return  connection = (Connection) DriverManager.getConnection("jdbc:sqlserver://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password);
                }
                catch(Exception ex)
                {
                    System.out.println(ex.getMessage());
                    System.out.println("couldn't connect!");
                    throw new RuntimeException(ex);
                }
        }

    If you need any more information let me know, and any help would be appreciated... thanks!
     
  2. Yeah ik that's why I created a folder within the server directory named lib, and then threw in that driver jar... Do I have to do something to make spigot load those classes or is that not possible ?
     
  3. Used a classloader to load in the jar, solved.