1

I'm trying to get my head around Java and how it works with JDBC so I've set up a SQL 2012 Express server on my domain controller.

I've successfully connected to it via a domain computer using another SQL 2012 instance but when using Java to pull a simply query down I get a 'No suitable driver found' error, even though I'm using and have imported the SQL4 .JAR extension.

Heres my code:

enter image description here

Is there a reason I'm getting this error?

Stacktrace:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at components.brkview.Doconnect(brkview.java:80)
at components.brkview$doevent.actionPerformed(brkview.java:137)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
obious
  • 609
  • 8
  • 21
  • 33

1 Answers1

1

You have to load driver before connecting using Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); as first instruction.
Also connection string is wrong (is like PostgreSQL one): here how to build corrected connection string.
Should looks like

jdbc:sqlserver://brk:1433;databaseName=Accounts_Table;
Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
  • Thanks but I'm getting a bunch of class loader(Unknown Source) errors upon initialization, even though I surrounded it in a try/catch statement to catch the exception. Ideas? – obious Sep 01 '13 at 18:21
  • 2
    jar with driver is not in classpath or sql4.jar (name is usually sqljdbcX.jar where X is jdbc version) is not the right version. Download driver from [here](http://www.microsoft.com/en-us/download/details.aspx?id=11774) and put in your classpath – Luca Basso Ricci Sep 01 '13 at 19:21
  • That worked but now I'm getting 'Could not find a login matching the name provided' even though the login under 'Permissions' in Management Studio is there and has been granted all permissions. Ideas? – obious Sep 01 '13 at 19:48
  • 1
    this is unrelated with question: start from http://stackoverflow.com/questions/7119602/how-to-make-sql-server-find-a-login-matching-the-name-provided or [here](http://social.technet.microsoft.com/Forums/sqlserver/en-US/29081e5f-7fbe-4c24-b9dc-71420b019612/sql-server-2012-login-failed) – Luca Basso Ricci Sep 01 '13 at 20:02
  • please accepet answer it it close the question – Luca Basso Ricci Sep 01 '13 at 20:02
  • Modern drivers no longer need to be loaded using `Class.forName(..)`, they just need to be on the classpath. – Mark Rotteveel Sep 02 '13 at 10:48
  • @MarkRotteveel, and just to state the obvious, "modern drivers" == JDBC 4. Here's some more details about that: http://stackoverflow.com/a/18297412/227779 – Per Lundberg Mar 26 '14 at 06:44