简体   繁体   English

带有Tomcat7的Eclipse Juno Java EE IDE-ClassNotFoundException

[英]Eclipse Juno Java EE IDE with Tomcat7 - ClassNotFoundException

I have a webapp installed and running but everytime I attempt to connect to the MySQL database using JDBC connection I get a CLASSnotFOUNDexception. 我已经安装并正在运行一个Web应用程序,但是每次尝试使用JDBC连接连接到MySQL数据库时,都会得到CLASSnotFOUNDexception。

The mysql-java-connector jar is in my WEB-INF/classes as well as WEB-INF/lib folders. mysql-java-connector jar在我的WEB-INF / classes以及WEB-INF / lib文件夹中。 Its on Eclipse's Build path as well but somehow the jar is not being deployed. 它也位于Eclipse的Build路径上,但是不知为何该jar尚未部署。 I've tried adding the jar separately to the build path as well. 我也尝试过将jar分别添加到构建路径中。 I've been stuck here for quite a well. 我被困在这里已经很好了。

Can someone direct me on how to deploy the jar? 有人可以指导我如何部署jar吗?

Heres part of the code I use to connect with my database. 这是我用来连接数据库的部分代码。

public class JdbcConnection {
    public Connection connection;

    public JdbcConnection(){
        String serverLocation = "";
        String db = "";
        String user = "";
        String password = "";

        try{
            InputStream in = JdbcConnection.class.getResourceAsStream("/props/database.properties") ;
            Properties props = new Properties();
            props.load(in);
            serverLocation = props.getProperty("serverLocation");
            db = props.getProperty("db");
            user = props.getProperty("user");
            password = props.getProperty("password");
            System.out.println(password + user + db + serverLocation);
           } 
        catch(Exception e){
            System.out.println("error" + e);
           }     

        createConnection(serverLocation, db, user, password);
    }

    public JdbcConnection(String serverLocation, String db, String user, String password){
        createConnection(serverLocation, db, user, password);
    }

Here is the error that comes up in the console. 这是控制台中出现的错误。 java.lang.ClassNotFoundException: com/mysql/jdbc/Driver java.lang.ClassNotFoundException:com / mysql / jdbc / Driver

Here is the create connection method 这是创建连接方法

public void createConnection (String serverLocation, String db, String user, String password){
    //register driver
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch (InstantiationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IllegalAccessException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (ClassNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    //              hostName = "jdbc:mysql://" + serverLocation+"/"+db+"?autoReconnect=true";
    String hostName = "jdbc:mysql://" + serverLocation+"/"+db+"?autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8";
    try {
        connection = DriverManager.getConnection(hostName,user,password);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}

Tomcat 6.x and higher demands that you put all JDBC driver JARs in its /lib directory. Tomcat 6.x及更高版本要求将所有JDBC驱动程序JAR放在其/ lib目录中。

Remove it from WEB-INF/classes (it does no good there, anyway) and WEB-INF/lib, redeploy your WAR, and you'll be able to move onto your next error. 将其从WEB-INF / classs中删除(无论如何,那里还是没有用)和WEB-INF / lib,重新部署您的WAR,您将可以继续进行下一个错误。

Believe me, you're still doing it wrong. 相信我,您仍然做错了。 It will work if you do it properly. 如果操作正确,它将起作用。

I'd recommend doctoring this class and making a connection to MySQL without Eclipse or Tomcat. 我建议您修改此类并在没有Eclipse或Tomcat的情况下建立与MySQL的连接。 If you can manage that, move up the ladder one step. 如果可以解决,请将梯子上移一级。

package persistence;

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

/**
 * util.DatabaseUtils
 * User: Michael
 * Date: Aug 17, 2010
 * Time: 7:58:02 PM
 */
public class DatabaseUtils {
/*
    private static final String DEFAULT_DRIVER = "oracle.jdbc.driver.OracleDriver";
    private static final String DEFAULT_URL = "jdbc:oracle:thin:@host:1521:database";
    private static final String DEFAULT_USERNAME = "username";
    private static final String DEFAULT_PASSWORD = "password";
*/
/*
    private static final String DEFAULT_DRIVER = "org.postgresql.Driver";
    private static final String DEFAULT_URL = "jdbc:postgresql://localhost:5432/party";
    private static final String DEFAULT_USERNAME = "pgsuper";
    private static final String DEFAULT_PASSWORD = "pgsuper";
*/
    private static final String DEFAULT_DRIVER = "com.mysql.jdbc.Driver";
    private static final String DEFAULT_URL = "jdbc:mysql://localhost:3306/party";
    private static final String DEFAULT_USERNAME = "party";
    private static final String DEFAULT_PASSWORD = "party";

    public static void main(String[] args) {
        long begTime = System.currentTimeMillis();

        String driver = ((args.length > 0) ? args[0] : DEFAULT_DRIVER);
        String url = ((args.length > 1) ? args[1] : DEFAULT_URL);
        String username = ((args.length > 2) ? args[2] : DEFAULT_USERNAME);
        String password = ((args.length > 3) ? args[3] : DEFAULT_PASSWORD);

        Connection connection = null;

        try {
            connection = createConnection(driver, url, username, password);
            DatabaseMetaData meta = connection.getMetaData();
            System.out.println(meta.getDatabaseProductName());
            System.out.println(meta.getDatabaseProductVersion());

            String sqlQuery = "SELECT PERSON_ID, FIRST_NAME, LAST_NAME FROM PERSON ORDER BY LAST_NAME";
            System.out.println("before insert: " + query(connection, sqlQuery, Collections.EMPTY_LIST));

            connection.setAutoCommit(false);
            String sqlUpdate = "INSERT INTO PERSON(FIRST_NAME, LAST_NAME) VALUES(?,?)";
            List parameters = Arrays.asList("Foo", "Bar");
            int numRowsUpdated = update(connection, sqlUpdate, parameters);
            connection.commit();

            System.out.println("# rows inserted: " + numRowsUpdated);
            System.out.println("after insert: " + query(connection, sqlQuery, Collections.EMPTY_LIST));
        } catch (Exception e) {
            rollback(connection);
            e.printStackTrace();
        } finally {
            close(connection);
            long endTime = System.currentTimeMillis();
            System.out.println("wall time: " + (endTime - begTime) + " ms");
        }
    }

    public static Connection createConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        if ((username == null) || (password == null) || (username.trim().length() == 0) || (password.trim().length() == 0)) {
            return DriverManager.getConnection(url);
        } else {
            return DriverManager.getConnection(url, username, password);
        }
    }

    public static void close(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void close(Statement st) {
        try {
            if (st != null) {
                st.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void rollback(Connection connection) {
        try {
            if (connection != null) {
                connection.rollback();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static List<Map<String, Object>> map(ResultSet rs) throws SQLException {
        List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
        try {
            if (rs != null) {
                ResultSetMetaData meta = rs.getMetaData();
                int numColumns = meta.getColumnCount();
                while (rs.next()) {
                    Map<String, Object> row = new HashMap<String, Object>();
                    for (int i = 1; i <= numColumns; ++i) {
                        String name = meta.getColumnName(i);
                        Object value = rs.getObject(i);
                        row.put(name, value);
                    }
                    results.add(row);
                }
            }
        } finally {
            close(rs);
        }
        return results;
    }

    public static List<Map<String, Object>> query(Connection connection, String sql, List<Object> parameters) throws SQLException {
        List<Map<String, Object>> results = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connection.prepareStatement(sql);

            int i = 0;
            for (Object parameter : parameters) {
                ps.setObject(++i, parameter);
            }
            rs = ps.executeQuery();
            results = map(rs);
        } finally {
            close(rs);
            close(ps);
        }
        return results;
    }

    public static int update(Connection connection, String sql, List<Object> parameters) throws SQLException {
        int numRowsUpdated = 0;
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);

            int i = 0;
            for (Object parameter : parameters) {
                ps.setObject(++i, parameter);
            }
            numRowsUpdated = ps.executeUpdate();
        } finally {
            close(ps);
        }
        return numRowsUpdated;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM