Help with Java Database Connectivity

T

Tom

Hey guys(or girls),

I was wondering if i could get some help with Connecting to my
Microsoft SQL server which is on my laptop.
I have my program written and it compiles but im having problems with
my connection to my database, or more accurately my
Microsoft SQL Server 2000 Driver for JDBC.
I have set my classpath so im not really sure what my problem is.

My error is somewhere within this code.

String url = "jdbc:microsoft:sqlserver://local:1433;User=me;Password=tom";
connection = DriverManager.getConnection(url,User,Password);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();


Below is my actual error:

java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:532)
at java.sql.DriverManager.getConnection(DriverManager.java:172)
at BookProject.CloudscapeDataAccess.connect(CloudscapeDataAccess.java:133)
at BookProject.CloudscapeDataAccess.<init>(CloudscapeDataAccess.java:44)
at BookProject.AddressBook.<init>(AddressBook.java:35)
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Class.java:232)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:582)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:511)
at sun.applet.AppletPanel.run(AppletPanel.java:289)
at java.lang.Thread.run(Thread.java:479)
java.security.AccessControlException: access denied
(java.lang.RuntimePermission exitVM)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:267)
at java.security.AccessController.checkPermission(AccessController.java:394)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:540)
at java.lang.SecurityManager.checkExit(SecurityManager.java:760)
at java.lang.Runtime.exit(Runtime.java:86)
at java.lang.System.exit(System.java:696)
at BookProject.AddressBook.<init>(AddressBook.java:43)
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Class.java:232)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:582)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:511)
at sun.applet.AppletPanel.run(AppletPanel.java:289)
at java.lang.Thread.run(Thread.java:479)

If anyone could give me a hand it would be greatly appreciated.
Thanks for your time!
 
J

Joe

connection = DriverManager.getConnection(url,User,Password);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();


Reverse the order of these two lines. I think you need to make a new
instance before you create a connection. If that's not it, check your
database logs and see if it's getting the data you think it should be
getting.
 
L

Lee Fesperman

Roedy said:
It seems sort of strange you would create this object then not keep a
handle to it anywhere. Are you sure you are not supposed to save to
pass it somewhere?

By convention, JDBC drivers are supposed to have a static initializer that registers
themselves with java.sql.DriverManager. The newInstance() is required on some JVMs
(notably, Microsoft) to ensure the static initializer is run.
 
J

Jason

A couple of things, you have to get the Class instance before you try
to access the driver manager to fetch the connection. Also, wrap this
code in a a try/catch/finally block so that when something goes wrong
the code will gracefully exit and close the connection (and any other
db resources) before it bails out.

Luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top