0

I'm having trouble getting the Class.forname("org.h2.Driver"); to not throw an exception. I added the h2*.jar to the build file, and I even got a main file to access the database (the cold will be under the problem code. I'm trying to use dbValues in a Vaadin application and it's just not taking. I can't seem to import any package org.h2.samples; or org.h2.*;

A note: dbValues is run through a Vaadin project. I don't know if this helps, but it's the only significant difference I can think of between it (doesn't work) and dbTest (does work).

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;
    }
}

Here's the code that seems to work. These two are in the same directory, so that's not the issue.

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();
    }
}
}

So any ideas on the issues? I've got about 24 hours to get this to work.

Shows the paths on the side

Community
  • 1
  • 1

2 Answers2

0

Make sure the library is in the classpath. You can manually include the library on WEB-INF/lib. Alternatively you could include the H2 jar in the Tomcat common lib folder.

FallAndLearn
  • 4,035
  • 1
  • 18
  • 24
0

JDBC drivers are known for (notorious for?) needing to be placed in the Servlet container rather than a specific web app. This avoids a variety of problems. As this Answer by Reichart explains:

JDBC drivers register themselves in the JVM-wide singleton DriverManager which is shared by all web apps.

Unfortunately moving your JDBC driver to the Servlet container means all your web apps on that container must use the same version of the driver and perhaps database.

Not sure yet what this means for H2. But it seems it might be best to put the entire H2 jar into the Servlet container rather than your web app.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154