2

Yes, it's that newbie to Vaadin, again. This time, I'm trying to see if I can do one of the most basic of tasks: connect to a database.

We use MS SQL Server here (version 2012, I believe) and we've been able to connect to it fine in two other Java programs that I've written. When attempting to do the same thing using a newly-created Vaadin project, however, I am told that No suitable driver found for jdbc:sqlserver://192.168.0.248;databaseName=job_orders_2014. I have checked and made sure that all three .jars from Microsoft are in the build path: sqljdbc.jar, sqljdbc4.jar, and sqljdbc41.jar.

Here's the ConnectionManager class that I've written which only tests whether or not it can get a connection:

package info.chrismcgee.sky.vaadinsqltest.dbutil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Logger;

public class ConnectionManager {

Logger logger = Logger.getLogger(ConnectionManager.class.getName());

private static final String USERNAME = "web";
private static final String PASSWORD = "web";
private static final String CONN_STRING = "jdbc:sqlserver://192.168.0.248;databaseName=job_orders_2014";

public ConnectionManager() throws SQLException, ClassNotFoundException {

//      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection conn = null;

        try {
            conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
            System.out.println("Connected!");
        } catch (SQLException e) {
            System.err.println(e);
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
    }

}

The result is the SQLException message I mentioned earlier. I've tried it both with and without that Class.forName... line, which is apparently only necessary for Java versions below 7 (and we're using version 8). When that line is enabled, I get a ClassNotFoundException instead.

What gives?

EDIT 04/01/2015: To help clarify how this ConnectionManager class is called, I am simply creating an instance of it from the main class, thusly:

package info.chrismcgee.sky.vaadinsqltest;

import java.sql.SQLException;

import info.chrismcgee.sky.vaadinsqltest.dbutil.ConnectionManager;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
@Theme("vaadinsqltest")
public class VaadinsqltestUI extends UI {

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = VaadinsqltestUI.class)
    public static class Servlet extends VaadinServlet {
    }

    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        setContent(layout);

        Button button = new Button("Click Me");
        button.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                try {
                    ConnectionManager connMan = new ConnectionManager();
                } catch (SQLException | ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                layout.addComponent(new Label("Thank you for clicking"));
            }
        });
        layout.addComponent(button);
    }

}
Sturm
  • 689
  • 2
  • 23
  • 52
  • having them in "build path" is not needed. you need to have them at runtime in your classpath. – cfrick Mar 25 '15 at 18:02
  • And how do I make sure of that? (Sorry for being clueless.) – Sturm Mar 25 '15 at 18:15
  • you could tell us, how you run all of this? – cfrick Mar 25 '15 at 18:21
  • I've edited my post above, so you can see how the `ConnectionManager` class is called: It is simply instantiated when a button is pressed. That's when the error occurs. – Sturm Apr 01 '15 at 18:53

1 Answers1

1

You need your dependencies in your runtime environment.

Please have a look at this answer here at stackoverflow:

https://stackoverflow.com/a/19630339

Community
  • 1
  • 1
nexus
  • 2,937
  • 3
  • 17
  • 22
  • I tried copying all three of those .jar files to the `WEB-INF/lib` folder, as that link suggests, but I am still receiving the same error. – Sturm Apr 01 '15 at 18:49
  • Solution discovered! It wasn't *copying* the files to that folder that I needed to do; it was simply adding an entry to the project's Properties that did the trick! Just sort of 'linking' up the existing JDBC .jar file(s) to the WEB-INF/lib folder in the Deployment Assembly tab of that Properties window is what was needed. Yes, this is an über-late reply, @nexus, but I thank you for pointing me in the correct direction. – Sturm Mar 03 '16 at 19:13