Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Question about Java and connecting to a database  (Read 1405 times)
0 Members and 1 Guest are viewing this topic.
Offline Fukushousha

Junior Newbie





« Posted 2008-01-03 14:23:45 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
    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.
Offline Martin Strand

Junior Member





« Reply #1 - Posted 2008-01-03 16:55:45 »

"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
Offline Fukushousha

Junior Newbie





« Reply #2 - Posted 2008-01-03 17:13:59 »

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  Smiley , hoping to get some more replies and get this problem behind me  Undecided
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Martin Strand

Junior Member





« Reply #3 - Posted 2008-01-03 18:01:37 »

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)
Offline Fukushousha

Junior Newbie





« Reply #4 - Posted 2008-01-04 14:12:16 »

Anyone else knows how to do it in Netbeans? I ask because I can't find it after all Sad
Offline HopeDagger

Junior Newbie




...


« Reply #5 - Posted 2008-01-05 06:17:55 »

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  Smiley , hoping to get some more replies and get this problem behind me  Undecided

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".
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 742
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2008-01-05 10:32:30 »

If you'd actually read the stacktrace, you'd have found that the last line is:
1  
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:
1  
Class.forName("org.postgresql.Driver");


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


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Fukushousha

Junior Newbie





« Reply #7 - Posted 2008-01-07 08:18:13 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (33 views)
2014-07-18 06:55:21

Zero Volt (29 views)
2014-07-17 23:47:54

danieldean (24 views)
2014-07-17 23:41:23

MustardPeter (26 views)
2014-07-16 23:30:00

Cero (41 views)
2014-07-16 00:42:17

Riven (43 views)
2014-07-14 18:02:53

OpenGLShaders (31 views)
2014-07-14 16:23:47

Riven (30 views)
2014-07-14 11:51:35

quew8 (29 views)
2014-07-13 13:57:52

SHC (64 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!