# ClassNotFoundException: SQLServerDriver

**URL:** <https://forum.inductiveautomation.com/t/classnotfoundexception-sqlserverdriver/12563>\
**Category:** Module Development\
**Created:** [January 13, 2017, 9:22pm UTC](https://forum.inductiveautomation.com/t/classnotfoundexception-sqlserverdriver/12563 "2017-01-13T21:22:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![caitlinf](https://avatars.discourse-cdn.com/v4/letter/c/c68b51/32.png) [@caitlinf](https://forum.inductiveautomation.com/u/caitlinf)\
**Post date:** [January 13, 2017, 9:22pm UTC](https://forum.inductiveautomation.com/t/classnotfoundexception-sqlserverdriver/12563/1 "2017-01-13T21:22:40Z")

</div>

Hi again,

I am trying to communicate with a database in my module. From what I’ve heard, the easiest way to connect is to add the sqljdbc4.jar to the projects build path and then connect in the following way:

`Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			con = DriverManager.getConnection(host, userName, password);
			Statement stmt = con.createStatement();`

However when I load my module into ignition I receive the following error:

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

Searching up the error I discovered it’s an issue with the CLASSPATH environment variable, so I set the classpath to the appropriate value (.;[path to the sqljdbc4.jar]) but I still get the same error.

if I remove the “Class.forName” statement the error becomes:

```auto
 java.lang.Exception: java.sql.SQLException: No suitable driver found for [database connect string]
```

I know my “host” “userName” and “password” variables are correct since I was able to successfully add and connect the database to ignition database connections using the same info. The only reason I can think of for this is that somehow the sqljdbc4.jar is not being discovered properly? Or is there some other setting I need to change in ignition for this to work?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 13, 2017, 11:59pm UTC](https://forum.inductiveautomation.com/t/classnotfoundexception-sqlserverdriver/12563/2 "2017-01-13T23:59:27Z")

</div>

[quote=“caitlinf”]… From what I’ve heard …[/quote]Well, maybe you heard wrong 😲  
Consider just using Ignition’s built-in data sources. In your module, retain a reference to the gateway context, which then allows you to use getDatasourceManager(), which then allows you to use getDatasource() with an Ignition data source name. Which then gives you access to the corresponding JDBC connection pool to use for queries. Don’t hold on to connections long – just let them go back to the pool right after you’re done with them.

---

<div class="post-metadata">

**Author:** ![caitlinf](https://avatars.discourse-cdn.com/v4/letter/c/c68b51/32.png) [@caitlinf](https://forum.inductiveautomation.com/u/caitlinf)\
**Post date:** [January 16, 2017, 4:54pm UTC](https://forum.inductiveautomation.com/t/classnotfoundexception-sqlserverdriver/12563/3 "2017-01-16T16:54:12Z")

</div>

I see, perhaps I heard from the wrong sources. This makes much more sense in the context of developing ignition modules. However using this method I have run into another problem.

When executing the insert statement (by following the example in the manual):

`con = lrscontext.getDatasourceManager().getConnection("test2");
			con.runPrepQuery("INSERT INTO OpenConnections(Connection_Name, IP_Address, Port) VALUES", antenna.getName(), antenna.getIP(), antenna.getPort());`

I get the following error:

```auto
java.lang.Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The index 1 is out of range.
```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 16, 2017, 5:19pm UTC](https://forum.inductiveautomation.com/t/classnotfoundexception-sqlserverdriver/12563/4 "2017-01-16T17:19:37Z")

</div>

You neglected to include placeholders for the arguments you provided. Try this:`con = lrscontext.getDatasourceManager().getConnection("test2");
con.runPrepQuery("INSERT INTO OpenConnections(Connection_Name, IP_Address, Port) VALUES (?, ?, ?)", antenna.getName(), antenna.getIP(), antenna.getPort());`

---

<div class="post-metadata">

**Author:** ![caitlinf](https://avatars.discourse-cdn.com/v4/letter/c/c68b51/32.png) [@caitlinf](https://forum.inductiveautomation.com/u/caitlinf)\
**Post date:** [January 16, 2017, 5:30pm UTC](https://forum.inductiveautomation.com/t/classnotfoundexception-sqlserverdriver/12563/5 "2017-01-16T17:30:01Z")

</div>

Ah I see! Works fine now. I’m still brand new to this, so I have some pretty “obvious” problems. 🙂

Thanks for all your help!
