1

I'm trying to insert some values from my database into an ArrayList. I guess there is some problem with the method showMeeting(). Hopefully you guys can understand some of the message get from the console.

public ArrayList<String> showMeeting() {

    ArrayList<String> output = new ArrayList<String>();
    try {
        Class.forName(driverName).newInstance();
        con = DriverManager.getConnection(url + dbName, userName, password);
        try {
            Statement st = con.createStatement();
            String meetID = "SELECT meetID FROM Meeting";
            ResultSet rs = st.executeQuery(meetID);
            while(rs.next()){
                output.add(rs.toString());
            }
        } catch (SQLException s) {
            System.out.println("Wrong sql-query");
        }
        con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return output;
}
public static void main(String[] args) {
    InteractWithDatabase2 test = new InteractWithDatabase2();
    ArrayList<String> meetID = test.showMeeting();
    String meetings = "";
    for (int i = 0; i <meetID.size(); i++) {
        meetings += meetID.get(i) + "\n";
    }
    System.out.println(meetings);

}

When I'm trying to run this in Eclipse, i get this message:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:169) at no.ntnu.fp.model.InteractWithDatabase2.visMoter(InteractWithDatabase2.java:107) at no.ntnu.fp.model.InteractWithDatabase2.main(InteractWithDatabase2.java:127)

1
  • Incidental comment but this line: output.add(rs.toString()); is going to add a value that's something like "ResultSet@0x000FF". You probably want output.add(rs.getString("meetID")), which will append the value of the meetID column. Commented Mar 29, 2011 at 13:37

2 Answers 2

2

Nothing seems to be wrong with your code, you are just missing the mysql connector .jar on your classpath.

You can download it from the download page or via Maven:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.15</version>
</dependency>
Sign up to request clarification or add additional context in comments.

2 Comments

Okey, just downloaded it from the download page. I'm quite new to this, but when you say classpath I quess you mean my package in Eclipse where I have the class? And is it just to copy the .jar-file from the folder I just downloaded into my classpath?
If you use Eclipse, you'll usually add it as a User Library. Copy it somewhere where you'll find it, e.g. C:\dev\libs. Right-click your project: "Build Path" > "Add Libraries" > "User Library" > "User Libraries" > "New ..." (Enter a name, e.g 'mysql') > "add Jars ..." ( select the jar you downloaded ) > "OK"
0

Just add your mysqlXXX.jar file into the proper directory .. then it will work..

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.