0

我安装并运行了一个 webapp,但每次尝试使用 JDBC 连接连接到 MySQL 数据库时,我都会收到 CLASSnotFOUNDexception。

mysql-java-connector jar 位于我的 WEB-INF/classes 以及 WEB-INF/lib 文件夹中。它也在 Eclipse 的构建路径上,但不知何故,jar 没有被部署。我也尝试将 jar 单独添加到构建路径中。我被困在这里好久了。

有人可以指导我如何部署 jar 吗?

这是我用来连接数据库的部分代码。

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

这是控制台中出现的错误。java.lang.ClassNotFoundException: com/mysql/jdbc/Driver

这是创建连接方法

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

1 回答 1

0

Tomcat 6.x 及更高版本要求您将所有 JDBC 驱动程序 JAR 放在其 /lib 目录中。

从 WEB-INF/classes 中删除它(无论如何,它在那里没有好处)和 WEB-INF/lib,重新部署你的 WAR,你将能够进入下一个错误。

相信我,你仍然做错了。如果你做得好,它会起作用。

我建议修改这个类并在没有 Eclipse 或 Tomcat 的情况下连接到 MySQL。如果你能做到这一点,就向上移动一步。

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;
    }
}
于 2013-04-23T20:32:04.667 回答