-
error NoClassDefFound
this is the message i get:
java.lang.NoClassDefFoundError: com/microsoft/jdbc/sqlserver/SQLServerDriver
at CheckUser.<init>(CheckUser.java:16)
at MyApplet.login(MyApplet.java:138)
at MyApplet.init(MyApplet.java:18)
at sun.applet.AppletPanel.run(AppletPanel.java:373)
at java.lang.Thread.run(Thread.java:595)
when i type in the username and password,click on submit button, it gives this error. it connects to the sql database and it should compare if the username and password match with the one in the database...is this a driver problem? i loaded the driver and it works fine, because it connects to the sqlserver.
-
You should also post the code that gives this exception.
-
CODE:
////////////////////
MyApplet.java
////////////////////
import java.awt.*;
import java.net.*;
import java.io.*;
import java.io.FileNotFoundException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.*;
import java.sql.*;
public class MyApplet extends java.applet.Applet
{
public String strUsername = "";
public String strPassword = "";
public void init() {
if (!login()) {
try {
getAppletContext().showDocument
(new URL(getCodeBase()+"accessdenied.html"),"_top");
}
catch (Exception e) {e.printStackTrace(); }
}
else {
try {
getAppletContext().showDocument
(new URL(getCodeBase()+"applog.html"),"_top");
}
catch (Exception e) {e.printStackTrace(); }
{
Button myButton = new Button (" Zapiši!");
add (myButton);
}
}
}
public boolean login() {
boolean userValid = false;
MyLogin login = new MyLogin (new Frame(""));
//appendtext login = new appendtext (new Frame(""));
requestFocus();
try{
if (login.id)
{
CheckUser check = new CheckUser();
strUsername = login.username.getText();
strPassword = login.password.getText();
userValid = check.isValidLogin(strUsername, strPassword);
System.out.println
("The password for " + strUsername + " is " + (userValid?"valid":"invalid"));
}
else
System.out.println
("Zdej pa nebi vec kliku al ka ???");
}
catch(Exception e) {System.out.println("Napaka!!"); }
login.dispose();
return userValid;
}
}
//////////////////////
CheckUser.java
/////////////////////
import java.awt.*;
import java.net.*;
import java.io.*;
import java.io.FileNotFoundException;
import java.sql.*;
public class CheckUser {
// Get connection
String serverName = "192.168.1.13";
String portNumber = "1433";
String mydatabase = serverName + ":" + portNumber;
String databaseName = "applog";
String url = "jdbc:sqlserver://" + mydatabase +";databaseName="+databaseName+";";
Connection connection = null;
public CheckUser() throws Exception {
DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
connection = DriverManager.getConnection(url,"demo","demo");
if (connection != null) {
System.out.println();
System.out.println("Successfully connected");
System.out.println();
// Meta data
DatabaseMetaData meta = connection.getMetaData();
System.out.println("\nDriver Information");
System.out.println("Driver Name: "
+ meta.getDriverName());
System.out.println("Driver Version: "
+ meta.getDriverVersion());
System.out.println("\nDatabase Information ");
System.out.println("Database Name: "
+ meta.getDatabaseProductName());
System.out.println("Database Version: "+
meta.getDatabaseProductVersion());
};
}
public boolean isValidLogin(String userName, String password) {
boolean isOk = false;
try {
Statement stmt = connection.createStatement();
ResultSet p = stmt.executeQuery("SELECT Login, Password FROM Users WHERE "+
"Login='"+userName+"' AND Password='"+password+"'");
if (p.next())
isOk = true;
}catch (SQLException se) {
se.printStackTrace(); // login fails for db error
}
return isOk;
}
public static void main(String args[]) throws Exception {
CheckUser test = new CheckUser();
}
}
-
Looks more like the sql driver class is not accessible to the code.
Make sure the classpath is set right.
-
try placing making 'static' the main program