Question about Java and connecting to a database

I am making a program which communicated with a database via jdbc with Java. The problem is that, while in another pc the same code, having everything the same worked like a charm, now I get an error. And I can’t understand what that error means or how to fix it. I did not know in which forum to post this topic so I put it in here. If it is considered the wrong forum, feel free to move it to the apropriate one.

So here is the code, some very basic and simple code, conncting to a database made in my pc, the local host.

    import java.sql.*;  //import all the JDBC classes

 

public class Ergastirio5 {

 

    static String[] SQL = {

        "create table Department ("+

            "d_id integer,"+

            "d_name varchar (40),"+

            "d_tel varchar(40),"+
            "d_fax varchar(40) );",

    "insert into Department values (1, 'Lefteris', '2100049583' ,'210948495 ');",

    "insert into Department values (2, 'Kostas', '2134045673' , '210463643' );",

    "insert into Department values (3, 'Takis', '2310267803' , '2310987343');",

    "insert into Department values (4, 'Makis',' 2310267803' ,' 2310987343');",

    "insert into Department values (5, 'Sakis',' 2310484731 ', '2310748349');",

       

    };

 

    public static void main(String[] args) {

        String URL = "jdbc:postgresql://localhost:5432/postgres";

        String username = "postgres";

        String password = "mypass";

 

        try {

           Class.forName("org.postgresql.Driver");

        } catch (Exception e) {

            System.out.println("Failed to load JDBC/ODBC driver.");

            return;

        }

 

        Statement stmt = null;

        Connection con=null;

        try {

            con = DriverManager.getConnection (

                URL,

                username,

                password);

            stmt = con.createStatement();

        } catch (Exception e) {

            System.err.println("problems connecting to "+URL);

        }

 

        try {

            // execute SQL commands to create table, insert data

            for (int i=0; i<SQL.length; i++) {

                stmt.execute(SQL[i]);

            }

            con.close();

        } catch (Exception e) {

            System.err.println("problems with SQL sent to "+URL+

                ": "+e.getMessage());

        }

    }

}


That’s it. And the error is:

Exception in thread “main” java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at Ergastirio5.main(Ergastirio5.java:48)
Java Result: 1

I haven’t posted in this forum for ages but if there is something I remember about the forum is that I always got help here, so please anyone got any clue as to what might be the problem here?

Thanks in advance.

“java.lang.UnsupportedClassVersionError: Bad version number in .class file” means you compiled the class for one version of Java, but you’re trying to run it on a lower version.
You probably wrote it with Java 6 and now you’re running it with Java 5. You can set the compiler’s “target” parameter to 1.5 or 1.4 if you want to run the class on older JVMs

I actually don’t know what JDK version the machine I took it from was running. I run JDK 1.5 . I tried changing the project’s source/binary format to an older JDK from netbeans(the IDE I use) but nothing happened.It was already set in 1.5 anyway. Unless it is not what you meant. I have not been programming in a while so I am really rusty in quite a lot of things right now so I could have misunderstood you.

Also thanks a lot for the reply :slight_smile: , hoping to get some more replies and get this problem behind me :-\

I don’t use Netbeans so I’m not sure but the “binary format” is probably what you should change so it targets an older JDK (older than 1.5)

Anyone else knows how to do it in Netbeans? I ask because I can’t find it after all :frowning:

Assuming it’s all source code that you have on your computer (ie. you’re not working with external .class files outside of the Java base classes), doing a “Build->Clean and Build Main Project” tends to do the trick. This happens often to me as well when I’m switching development between computers with different JDK versions. If that doesn’t do it, you can change the Java Platform version you want to compile against by right-clicking on your project in the sidebar, selecting Properties, then Libraries, and selecting the platform version. To add a new one, go to “Tools->Java Platform Manager”.

If you’d actually read the stacktrace, you’d have found that the last line is:

at Ergastirio5.main(Ergastirio5.java:48)

So compiling his project with a lower version-target won’t help here, because:
His code it already executed!

The following line results in the crash:

Class.forName("org.postgresql.Driver");

So it’s not YOUR code that is ‘too new’, it’s the PostgreSQL-driver library!

Thanks Riven and everyone else. But I had read the error, and tried an older version of postgresql but still did not work. Will search for an even older one when I come back home I guess.