gurpalrattu gurpalrattu - 5 months ago 48
Java Question

Class.forName error in Java connecting to SQL Database

I'm getting some kind of syntax error and I'm not sure how to resolve it or what I'm doing wrong exactly. I was following a tutorial on youtube, and I followed exactly how it was done in the video. I added the jar file to the project, but its still giving me this error. I'm fairly new to java and just trying to learn how to create a link between a java application and a sql database.

The error is on the line for:

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


Any help or hints is appreciated

package database_console;

import java.sql.*;

public class DBConnect {

String dbURL = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=TestDB1";
String user = "sa";
String pass = "pass";

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

try {
Connection myConn = DriverManager.getConnection(dbURL, user, pass);

Statement myStmt = myConn.createStatement();

ResultSet myRs = myStmt.executeQuery("Select * from Login");



while (myRs.next())
{
System.out.println(myRs.getString("Username"));
System.out.println(myRs.getString("Password"));
}

}


public static void main(String[] args) {
// TODO Auto-generated method stub

}

}


I haven't written anything into the main yet, what I've posted is the complete code I've done. Here are the error messages I'm getting:

Description Resource Path Location Type Syntax error on token ".", @ expected after this token DBConnect.java /database_console/src/database_console line 11 Java Problem

Description Resource Path Location Type Syntax error, insert ")" to complete MethodDeclaration DBConnect.java /database_console/src/database_console line 11 Java Problem


Description Resource Path Location Type Syntax error, insert "Identifier (" to complete MethodHeaderName DBConnect.java /database_console/src/database_console line 11 Java Problem

Description Resource Path Location Type Syntax error, insert "SimpleName" to complete QualifiedName DBConnect.java /database_console/src/database_console line 11 Java Problem

Answer

First, ensure you are getting the jar from the correct place: https://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx

Then, add it to your classpath. If you are using eclipse, press CTRL+SHIFT+T and type SQLServerDriver. It must find the class name.

Lastly, your code won't compile. Add the entire code you wrote inside the main method:

public class DBConnect {

    public static void main(String[] args) {
        String dbURL = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=TestDB1";
        String user = "sa";
        String pass = "pass";

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

        try {
            Connection myConn = DriverManager.getConnection(dbURL, user, pass);
            Statement myStmt = myConn.createStatement();

            ResultSet myRs = myStmt.executeQuery("Select * from Login");

            while (myRs.next())
            {
                System.out.println(myRs.getString("Username"));
                System.out.println(myRs.getString("Password"));
            }
        }
    }
}