JIRA Scriptrunner Sql Server configuration

I am trying to set up a Scriptrunner script to access a sql server database outside of JIRA. I am using the script console to attempt to execute the following code:

import java.sql.*;
import java.util.Date;
import groovy.sql.Sql;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.Issue;

import org.apache.log4j.Logger;
import org.apache.log4j.Level;

def log = Logger.getLogger("com.acme.CreateSubtask");

log.setLevel(Level.DEBUG);

ApplicationUser appUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
User user = appUser.getDirectoryUser();
def dDate = new Date().format('MM/dd/yyy');
def driver = Class.forName('com.microsoft.sqlserver.jdbc.SQLServerDriver').newInstance() as Driver;

def props = new Properties();
props.setProperty("user", "user");
props.setProperty("password", "password");

def conn = driver.connect("jdbc:sqlserver://myserver/mydb", props);
def sql = new Sql(conn);


    sql.close();
    conn.close();

I have put the sqljdbc4.jar file in the JIRA/lib directory as I found on this discussion site, but it is giving me the java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver error. I have tried to find out how to fix this but nothing seems to be working. I have tried putting this file in different locations, and setting the CLASSPATH environment variable but nothing is working. Do i need to install the .jar file somehow? What am I missing that is keeping this from making the sql server connection?

 

2 answers

This widget could not be displayed.

I can see that the driver in question is available in a public maven repo. What if you tried getting the jar file via @Grab?

// https://mvnrepository.com/artifact/com.microsoft.sqlserver/sqljdbc4
@Grapes(
    @Grab(group='com.microsoft.sqlserver', module='sqljdbc4', version='4.0')
)

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import groovy.sql.Sql
import org.apache.log4j.Level
import org.apache.log4j.Logger

import java.sql.Driver

def log = Logger.getLogger("com.acme.CreateSubtask")

log.setLevel(Level.DEBUG)

ApplicationUser appUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
User user = appUser.getDirectoryUser()
def dDate = new Date().format('MM/dd/yyy')
def driver = Class.forName('com.microsoft.sqlserver.jdbc.SQLServerDriver').newInstance() as Driver

def props = new Properties()
props.setProperty("user", "user")
props.setProperty("password", "password")

def conn = driver.connect("jdbc:sqlserver://myserver/mydb", props)
def sql = new Sql(conn)

sql.close()
conn.close()
This widget could not be displayed.

I should have posted this sooner, but I found the solution. First, after doing some trial and error testing, I found that I needed the sqljdbc4.jar file. (I am connecting to SQL Server 2005.) I also found I had to copy it to the C:\Program Files\Atlassian\JIRA\lib directory. I also didn't need to put in the CLASSPATH variable.

So there isn't anything to "install", you just have to know which is the right version of the sqljdbc file you need and that it goes in the JIRA/lib directory where you have JIRA installed on your server.

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted 5 hours ago in Statuspage

What are your best incident management tips and stories? #HugOps

 👋Community members! Downtime happens. And great incident response takes a village. Teams like Support, Dev, SRE, Ops, IT, and Marketing have to come together to resolve the problem while keep...

23 views 0 2
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you