h2数据库class.forname的ClassNotFound异常(" org.h2.Driver")

时间:2016-02-28 20:22:52

标签: java database vaadin h2

我无法获得Class.forname(" org.h2.Driver");不要抛出异常。我将h2 * .jar添加到构建文件中,我甚至有一个主文件来访问数据库(冷却将在问题代码下。我试图在Vaadin应用程序中使用dbValues并且它'我只是没有服用。我似乎无法导入任何包org.h2.samples;或org.h2。*;

注意:dbValues通过Vaadin项目运行。我不知道这是否有帮助,但它是我能想到的(没有工作)和dbTest(确实有效)之间的唯一重要区别。

package com.example.assignment3;

import java.sql.*;
import java.util.ArrayList;

public class dbValues {

    ArrayList<business> a = new ArrayList<business>();

    public dbValues(){

        business b = new business();
        ArrayList<business> a = new ArrayList<business>();

        try {
            Class.forName("org.h2.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {

            Connection conn = DriverManager.getConnection("jdbc:h2:~/test","Lucas","");
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM COMPANIES");
            while(rs.next()){
                b.setID(rs.getInt("ID"));
                b.setName(rs.getString("NAME"));
                b.setSector(rs.getString("SECTOR"));
                b.setAddress(rs.getString("ADDRESS"));
                b.setProvince(rs.getString("PROVINCE"));
                a.add(b);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public ArrayList<business> returnBusinesses(){
        return a;
    }
}

这里的代码似乎有效。这两个位于同一目录中,因此不是问题。

import java.sql.*;
import java.util.ArrayList;

import com.example.assignment3.business;
public class dbtest {
public static void main(String[] a)
throws Exception {

    /**
     * Create variables to iterate through when adding to database.
     */
    String dbname = "COMPANIES";
    int id = 0;
    String name = "";
    String sector = "";
    String address = "";
    String province = "";
    String query = "";

    Class.forName("org.h2.Driver");
    Connection conn = DriverManager.getConnection("jdbc:h2:~/test","Lucas","");

    /**
     * Create the business objects.
     */
    business boeing = new business(900162738, "Boeing Canada", "Aerospace", "123 Planes St.", "BC");
    business odysseyMoon = new business(900687789, "Odyssey Moon", "Aerospace", "687 The Moon", "NS");
    business vantage = new business(900278382, "Vantage Airport Group", "Aerospace", "77 Smith St.", "NB");
    business canadaBank = new business(900789213, "Bank of Canada", "Financial Services", "2325 Canada blvd.", "ON");
    business montrealBank = new business(900890876, "Bank of Montreal", "Financial Services", "2132 Bonjour Rd.", "QC");
    business rbc = new business(900564738, "Royal Bank of Canada", "Financial Services", "132 Clifton St.", "NB");
    business ubisoft = new business(900547967, "Ubisoft Halifax", "Interactive Media", "2000 Barrington St.", "NS");
    business scotiaBank = new business(900345273, "Scotia Bank", "Financial Services", "Yahmon Rd.", "NS");
    business propaganda = new business(900101928, "Propaganda Games", "Interactive Media", "25 Queen St.", "NT");
    business ea = new business(900162739, "EA Montreal", "Interactive Media", "54 Gagnon St.", "QC");

    /**
     * Create the arraylist that will be iterated through when adding
     * to the database and then add them to that list.
     */
    ArrayList<business> businesses = new ArrayList<business>();
    businesses.add(boeing);
    businesses.add(odysseyMoon);
    businesses.add(vantage);
    businesses.add(canadaBank);
    businesses.add(montrealBank);
    businesses.add(rbc);
    businesses.add(ubisoft);
    businesses.add(scotiaBank);
    businesses.add(propaganda);
    businesses.add(ea);

    /**
     * Perform the statement that populates the database.
     */
    try {
        Statement stmt = conn.createStatement();
        for(int i = 0; i < businesses.size(); i++){
            id = businesses.get(i).getID();
            name = businesses.get(i).getName();
            sector = businesses.get(i).getSector();
            address = businesses.get(i).getAddress();
            province = businesses.get(i).getProvince();
            query = "INSERT INTO " + dbname + " VALUES("+ id +", '"+ name +"', '"+ sector +"', '"+ address +"', '"+ province +"');";
            System.out.println(query);
            stmt.execute(query);
        }
        //stmt.execute("DELETE FROM COMPANIES;");

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

关于这个问题的任何想法?我有大约24小时的工作时间。

Shows the paths on the side

2 个答案:

答案 0 :(得分:0)

确保库位于类路径中。 您可以在WEB-INF / lib上手动包含该库。或者,您可以在Tomcat common lib文件夹中包含H2 jar。

答案 1 :(得分:0)

JDBC驱动程序(臭名昭着?)需要放在Servlet容器而不是特定的Web应用程序中。这避免了各种问题。正如this Answer by Reichart所解释的那样:

  

JDBC驱动程序在JVM范围的单例DriverManager中注册自己,该驱动程序由所有Web应用程序共享。

不幸的是,将JDBC驱动程序移动到Servlet容器意味着该容器上的所有Web应用程序必须使用相同版本的驱动程序和数据库。

尚不确定这对H2意味着什么。但似乎最好将整个H2 jar放入Servlet容器而不是Web应用程序。