Friday, April 15, 2011

Published 4/15/2011 by with 0 comment

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database

Trying to connect to a MySQL server from Java? But seeing the error "Unknown database"?

The reason is that the database does not exist in the MySQL server. You might have accidentally misspelled the database name in the JDBC connection url.

See the code below:
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sales","username","password");

Here sales is the database name. If you misspelled this or the database sales does not exist in the server, you see this error message.

Read More
    email this       edit
Published 4/15/2011 by with 0 comment

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure


This error message may include the following text also:

"The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server."


When we try to connect to a MySQL server "Communications link failure" error may occur for any of the following errors:

i) MySQL server is not running. Check for this and run the sever if it is stopped.

ii) Host name is wrong: If the server is running in the same computer, the correct host name is localhost otherwise it is a valid IP address or computer name.

iii) Wrong port no given : By default MySQL server runs in port 3306. If it is different, check that correct port no is given.


Sample code:

public static Connection getConnection()
{
Connection connection = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","username","password");
System.out.println("Connection successful");
}
catch(ClassNotFoundException cnfe)
{
System.err.println("MySQL driver not found");
cnfe.printStackTrace();
}
catch(SQLException sqle)
{
System.err.println("Database/Connection error");
sqle.printStackTrace();
}

return connection;
}



Read More
    email this       edit
Published 4/15/2011 by with 2 comments

ClassNotFoundException: com.mysql.jdbc.Driver

This error occurs when the required JDBC driver file is not in the CLASSPATH. Also, if the driver name is not correct ClassNotFoundException occurs. To solve this problem, at first check the driver name; if the driver name is correct, do one of the followings as per requirement.

i) Add an entry for the driver file (mysql-connector-java-5.1.10.jar, for example) in the CLASSPATH variable.

or

ii) If you are not familiar with the CLASSPATH variable, place the mysql-connector-java-5.1.10.jar file in \jre\lib\ext folder in the JDK install directory (for example C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext)

or

iii) If you are doing this in NetBeans or other IDEs, add MySQL JDBC Driver Library in the project.
Read More
    email this       edit

Sunday, April 10, 2011

Published 4/10/2011 by with 1 comment

call to super must be first statement in constructor

The error message is very clear actually:

i) We can call super in the constructor of a subclass
ii) If we call super in the constructor of a subclass, it must be the first statement in that constructor; i.e, before writing any other statement we call to super must be made.

It is mentionable that, super(parameters) calls the constructor of the super class.

See the code below:

The super class...


public class Employee
{
private int id;
private String name;
public Employee()
{
}
public Employee(int id,String name)
{
this.id = id;
this.name = name;
}
}

The subclass...


public class SalariedEmployee extends Employee
{
private double salary;
public SalariedEmployee()
{
}
public SalariedEmployee(int id,String name,double salary)
{
this.salary = salary; //this is the first statement now
super(id,name); //error : call to super must be first statement in constructor
}
}

The correct subclass will be...


public class SalariedEmployee extends Employee
{
private double salary;
public SalariedEmployee()
{
}
public SalariedEmployee(int id,String name,double salary)
{
super(id,name); // now call to super is the first statement in the constructor
this.salary = salary; // it is now the second statement
}
}



Read More
    email this       edit

Friday, October 30, 2009

Published 10/30/2009 by with 0 comment

java.lang.ArrayIndexOutOfBoundsException


It's a very common runtime error message or exception we face when we use array wrongly. Very specifically this exception is thrown to indicate that an array has been accessed with an illegal index (location / position / subscript). The index is either negative or greater than or equal to the size of the array.


See the following code:

int data[]={5,7,18,21,27,15,3};

//show the array elements
for(int i=0;i<=7;i++)
{
System.out.println(data[i]);
}

Here the array size is 7 as the array is initialized by assigning 7 elements {5,7,18,21,27,15,3}. So the valid array index is 0 to 6. In the loop condition, we have given i&lt=7 and i is used as the array index inside the loop - so when i is 7 it tries to access the array element 7 but 7 is not a valid index. This causes the exception. Correct loop condition is i<7 or i<=6. The recommended approach is using the array built in variable length instead of the static size. Correct the code as below:


int data[]={5,7,18,21,27,15,3};

//show the array elements
for(int i=0;i<data.length;i++)//data is the array name
{
System.out.println(data[i]);
}

Read More
    email this       edit
Published 10/30/2009 by with 0 comment

array dimension missing

'array dimension missing' is shown when the array size is not specified while allocating memory for the array or creating an array.

See below:

int data[ ]=new int[ ];

See that, blank [ ] is given in new int[ ]. We must give an int as the size of the array. Memory will be allocated for the array according to the given int within the [ ] as below:

int data[]=new int[5];
Read More
    email this       edit
Published 10/30/2009 by with 7 comments

illegal initializer

You may see this error for writing wrong expression to initialize an array. See the code below:

int data={42,15,27,20,19};

If you compile this, you will see the following error message:

illegal initializer for int
int data={42,15,27,20,19};
^

Here the array is not declared properly as we have not put [ ] for declaring the array. The correct code is:

int data[ ]={42,15,27,20,19};

or

int[ ] data={42,15,27,20,19};
Read More
    email this       edit
Published 10/30/2009 by with 1 comment

incompatible types

1. Sometimes we may get the following error message in an 'if' (selection) structure.

incompatible types
found : int
required: boolean

This is because, we have not specified a boolean expression as the condition of the 'if'; instead we might have written such an expression which is may be int, float, String or other types but not boolean.

See the code below:

int a=10,b=10;

if(a=b)
{
System.out.println("a and b are equal");
}

Check the condition is a=b. Is it a boolean expression? By using a single = (equal sign), value of b is assigned to a which is an integer. That's not correct. We must use == (double equal sign) for comparing two variables for equality.

Correct code is the below:

int a=10,b=10;

if(a==b)
{
System.out.println("a and b are equal");
}


2. Sometimes it happens because of assigning one type of value to a variable of other types. See the code below:

String text='a';
char ch="a";
In this case the error message will be:

incompatible types
found : char
required: java.lang.String
String text='a';
^
incompatible types
found : java.lang.String
required: char
char ch="a";
^
Here we have not assigned correct value for the variables text and ch. In the first line, we have assigned a char value to a String variable. 'a' is a char value because it is written within ' ' (single quotation mark) . In the second line, we have assigned String value "a" to a char type variable. Note that String value is written within " " (double quotation mark). So we tried to assign char value to a String variable and a String value to a char variable. The correct code is:

String text="a";
char ch='a';

If you see this error, check that the correct type of value or variable is assigned.

Read More
    email this       edit

Friday, August 28, 2009

Published 8/28/2009 by with 0 comment

cyclic inheritance involving ...

public class Person extends Employee
{
private String firstName;
private String lastName;

public Person(String firstName,String lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}

}

Person is a class which inherits Employee and see the code below where Employee also inherit the Class Person.


public class Employee extends Person
{
private double salary;

public Employee(String firstName,String lastName,double salary)
{
super(firstName,lastName);
this.salary=salary;
}
}

In the above code we will see the error message "cyclic inheritance involving Person

public class Person extends Employee"

Actually Person should be the super class here and Employee is the subclass. But here the relationsship is such that Employee is the subclass of Person, again Person is the subclass of Employee. It's a cyclic inheritance which is not possible.

The solution of this problem is identifying the proper relationship i.e, finding out the super class and the subclass. In our case Person is the super class, so it should not try to inherit Employee and Employee is the subclass which will inherit Person. Correction is ommiting "extends Employee" from the Person class signature as below:

public class Person
{
private String firstName;
private String lastName;

public Person(String firstName,String lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}

}

public class Employee extends Person
{
private double salary;

public Employee(String firstName,String lastName,double salary)
{
super(firstName,lastName);
this.salary=salary;
}

}
Read More
    email this       edit

Wednesday, August 19, 2009

Published 8/19/2009 by with 5 comments

class, interface, or enum expected

You see this error message because of any one of the followings:

1. You misspelled the keyword class (fully lower case) or you did not write it at all for writing your class.

public Class Test //actual keyword is class
{
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}

In the above example Class (C is in upper case) is written instead of class (fully lower case).

2. Remember you cannot declare variables outside of class body like below.

int variable; //variable must be declared inside the class body
public class Test
{
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}

To solve this declare variables inside the class body as below.


public class Test
{
int variable; //variable must be declared inside the class body
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}


3. All methods are also defined within the method body. If you define any method outside of the class body you will see this error message.


public class Test
{
public void myMethod()
{
System.out.println("method inside the class body");
}
} //end of class

public void anotherMethod()
{
System.out.println("method outside of the class body");
}

In the above code, the method anotherMethod is defined after the end of the class. So this error occurred. Most often it occurs just because of incorrect use of {} (curly braces). Check all { are closed with } in the proper place.

4. If you are defining an interface, check you spelled it correctly in fully lowercase.

public Interface Test //actual keyword is interface
{
public void myMethod();

}

In the above code, keyword interface is misspelled with upper I.

5. Same way if you define an enum spell the keyword enum correctly.

public Enum Day //actual keyword is enum
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}

In the above example Enum is incorrect as the E is in upper case, it will be lower case e.
Read More
    email this       edit