-2

I just installed the JDBC driver from here for Sql Server (http://www.microsoft.com/en-us/download/details.aspx?id=11774) and I tested this sample code to test the connection:

http://msdn.microsoft.com/en-us/library/aa342339.aspx

The error I get seems to be in line:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");


 java.lang.ClassNotFoundException:
 com.microsoft.sqlserver.jdbc.SQLServerDriver   
 at java.net.URLClassLoader$1.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)  
 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)

Two notes:

  • If I remove the try-finally code I get NO error ==> which probably prooves there is nothing wrong with the user & password I created as login information to the database.
  • I created a Person table with Contact as a column in my database.

Side questions:

  • What exactly does this mean: localhost:1433
  • How can I set Eclipse to show me what line triggers the error? Thank you very much!
thar45
  • 3,518
  • 4
  • 31
  • 48
Sam
  • 3,067
  • 19
  • 53
  • 55
  • 3
    the sqljdbc jar must put in classpath, or the jvm won't find it and give you that exception – guido Sep 26 '12 at 12:42
  • Thanks guido, I don't know about classpath, could you detail that? After installing sqljdbc_4.0.2206.100_enu.exe, I found two jar files (sqljdbc.jar and sqljdbc4.jar) in C:\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu, are you referring to one of those two jar files? – Sam Sep 26 '12 at 12:49
  • just read the webpage "Using the JDBC driver" it is the first link in the page where you downloaded the sample code! – guido Sep 26 '12 at 12:51
  • You should have a look at the jtds jdbc driver it's a much better driver than the MS one. http://jtds.sourceforge.net/doc.html – ChadNC Sep 26 '12 at 14:30

4 Answers4

1

What exactly does this mean: localhost:1433

localhost is to refer to your machine. 1433 is the default port number where SQL Server exposes TCP/IP interface for external clients to interact with it.

As guido mentioned, you should add sqljdbc.jar to your classpath. If you are using eclipse, you would do that by Right-click on the project -> Build Path -> Add External Archives -> Select sqljdbc.jar from the directory where you downloaded.

You may want to take a look at one of my responses to a similar question here: Error:The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect

Community
  • 1
  • 1
Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • Thanks a lot Vikdor! At first I added sqljdbc.jar, this gave an error suggesting to add instead sqljdbc4.jar, so I did that and also removed the link to sqljdbc.jar. Since removing that link, I keep getting an **"editor does not contain a main type"** LAUNCH error, although I **do** have a "main" method there (as seen in the example I referenced in the initial question). **What can I do?** Now the program won't even run. – Sam Sep 26 '12 at 13:03
  • can you edit your question and add your program snippet there? – Vikdor Sep 26 '12 at 13:11
  • Vikdor, this is not about particular code in a class anymore, it's something more powerful like Build Path Configuration. (I must have messed up something there.) I know for sure because old console classes of mine that were perfect before, also trigger this error about "no main type". – Sam Sep 26 '12 at 13:58
1

Looks like the JDBC driver is not in the classpath of your running application. Perhaps try your sample using JDK and JRE command line tools only (javac and java) rather than doing it in the IDE.

Not sure why you would get no error message if you remove the try/catch. The uncaught exception should get reported. If you run the program from the command line rather than from Eclipse you will see the uncaught exception reported.

localhost:1433 is part of the JDBC URL. localhost means the database is on the same machine as the running application. Port 1433 is default SQL Server TCP port.

Not sure about the Eclipse question. I mostly use Netbeans.

Jerome
  • 2,059
  • 1
  • 16
  • 19
  • Thanks very much Jerome. Oh, and the explanation for why I don't get the error by removing the try-catch. Before IT, the connection string is declared and defined, like ANY other string. It's within the try-catch that the string is tested and the SQL statement executed. – Sam Sep 27 '12 at 07:23
  • I had removed the build path source folder by mistake, I fixed that now. So now I got rid of the "editor does not contain a main type" error. Thanks everyone! – Sam Sep 27 '12 at 07:24
1

Two notes: the error says that it can not find the driver (jar), see comment by "guido"

Side questions: localhost - is a network name for your local computer, 1433 - is a port number SQL server waits for connections on What is in your catch clause? e.PrintStackTrace(); should show line numbers too

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
1

Main question:

Did you add the sqljdbc4.jar file to your class path? If not, java does not know where to find the classes.

  1. side question: localhost refers to your computer, meaning you try to connect to a sql server, that is installed on the computer you run the programm on.

localhost

gets resolved by your network adapter to your own ip. The number behind the colon(:) is the port number. On a computer (or any other network device for that matter) there can be multiple applications sending and receiving data over the network. An application therefor listens on a specific port for data. And the standard port for SQL server is 1443

  1. side question: use

    try{ code that can throw exceptions }catch(HandledException e){ e.printStacktrace(); }

to print information about the exception on your console.

LuigiEdlCarno
  • 2,410
  • 2
  • 21
  • 37
  • Yes I added **sqljdbc4.jar** and can see it in the left panel. Thank you. Now the big issue is I get "editor does not contain a main type" after I removed **sqljdbc.jar** linkage (or so I think I did), and worse of all, now I can't launch any console class I was able to launch before. – Sam Sep 26 '12 at 13:07
  • Ok, just to make sure: DOES your applications main class contain a method public static void main(String[] args) ? How do you launch your application in eclipse? If nothing helps, clean your project and add sqljdbc4.jar again to your class path – LuigiEdlCarno Sep 26 '12 at 13:14
  • Yes, Luigi, it DOES contain: public static void main(String[] args). I usually go to: Run - Run As - Java application. I think I might have removed something inadvertently from that path. - If I go to Configure Build Path - "Libraries" / "Order and export" is it OK to ONLY have sqljdbc4.jar there as a single listed entry? The cleaning didn't help. – Sam Sep 26 '12 at 13:22
  • The libraries view should also contain JRE System Library [] at the top of the list. Otherwise this should suffice. Try to build a lauch configuration in eclipse by clicking on the little downwards arrow next to the green launch arrow and follow the instructions – LuigiEdlCarno Sep 26 '12 at 13:37