0

I know this has been asked a million times, I looked up 100's of links all point to what I am doing, but I am not able to get this working.

Code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ConnectMSSQLServer
{
public static void main(String[] args) {

        Connection conn = null;
        String dbName = "database1";
        String serverip="dv-bi-olap1";
        String serverport="1433";
        String url = "jdbc:sqlserver://localhost:1433;" + "databaseName=AdventureWorks;user=MyUserName;password=*****;";
        Statement stmt = null;
        ResultSet result = null;
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String databaseUserName = "admin";
        String databasePassword = "root";
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url);
            stmt = conn.createStatement();
            result = null;
            String pa,us;
            result = stmt.executeQuery("select * from table1 ");

            while (result.next()) {
                us=result.getString("uname");
                pa = result.getString("pass");              
                System.out.println(us+"  "+pa);
            }

            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

CMD commands:

C:\Users\blabla\Desktop>javac ConnectMSSQLServer.java

class file is created

set CLASSPATH =.;C:\Users\blabla\Desktop\sqljdbc_4.2\enu\sqljdbc42.jar

C:\Users\blabla\Desktop>java ConnectMSSQLServer

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at ConnectMSSQLServer.main(ConnectMSSQLServer.java:21)

ok that dint work, so i tried below

C:\Users\blabla\Desktop>java -CLASSPATH =.;C:\Users\blabla\Desktop\sqljdbc_4.2\enu\sqljdbc42.jar -cp . ConnectMSSQLServer

then this

C:\Users\blabla\Desktop>java -CLASSPATH =.;C:\Users\blabla\Desktop\sqljdbc_4.2\enu\sqljdbc42.jar ConnectMSSQLServer

all resulted in the below error

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at ConnectMSSQLServer.main(ConnectMSSQLServer.java:21)
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
blablabla
  • 225
  • 1
  • 3
  • 10
  • Open "sqljdbc42.jar" with a tool that can open zip files (like winzip). Does it contain "com.microsoft.sqlserver.jdbc.SQLServerDriver"? – Elliott Frisch Oct 29 '15 at 18:39
  • This is a Q&A site. "HELP!!!!" is not a question. Also, why are you trying to command-line (or edit by hand) the classpath? Use an IDE like netbeans or eclipse; it will get things ready for you, including your configs. – CosmicGiant Oct 29 '15 at 18:45
  • 1
    Occam's Razor: the path to the driver jar is simply wrong. Prove that it isn't. All the tests that you post here assume that the path is correct and that there is something wrong with how Java is invoked. – Gimby Oct 29 '15 at 22:27
  • @Gimby ... and the question is misleading because `-CLASSPATH` simply does not work at all. It's not a matter of the code throwing a ClassNotFoundException, it's a matter of the `java` command failing with "Unrecognized option: -CLASSPATH". (That is, the `java` command doesn't even *begin* to run the code.) – Gord Thompson Oct 29 '15 at 22:32
  • @GordThompson true, but the one test where the classpath is set as a CLASSPATH environmental variable should have worked – Gimby Oct 30 '15 at 08:19
  • Sorry about adding HELP!! I was loosing my mind for something this simple. – blablabla Oct 30 '15 at 20:26
  • I restarted the computer and it worked. FACEPALM – blablabla Oct 30 '15 at 20:32

2 Answers2

2

Try removing the = & using lowercase classpath (or cp) option

java -classpath .;C:\Users\blabla\Desktop\sqljdbc_4.2\enu\sqljdbc42.jar ConnectMSSQLServer
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

You need to download the driver.

https://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx

Then include it in your application. Based on what you are saying I cannot see that you have the driver you need to use JBDC with you application.

Ya Wang
  • 1,758
  • 1
  • 19
  • 41