开发者

Insert table values from a SQL database to an ArrayList

开发者 https://www.devze.com 2023-02-20 08:07 出处:网络
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 co

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) {
            Sys开发者_Go百科tem.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)


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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消