java - 运行 Java Web 应用程序时出现 SQLException

标签 java mysql eclipse tomcat jakarta-ee

我开始使用 Java EE,并在 Eclipse 中为 Java EE 设置了一个简单的项目。结构是传统的Java Web项目默认:Tomcat集成了Eclipse和一些基本的servlets。

详细说明:我有这个类来代表联系人: 包 br.myagenda.data;

import java.util.Calendar;

public class Contato {
    private long _id;
    private final String _nome;
    private final String _endereco;
    private final String _email;
    private final Calendar _dataNascimento;

    public Contato(String nome, String endereco, String email, Calendar dataNascimento)    
    {
        _nome = nome;
        _endereco = endereco;
        _email = email;
        _dataNascimento = dataNascimento;
    }

    public void setId(long id) {
        _id = id;
    }

    public long getId() {
        return _id;
    }

    public String getNome() {
        return _nome;
    }

    public String getEndereco() {
        return _endereco;
    }

    public String getEmail() {
        return _email;
    }

    public Calendar getDataNascimento() {
        return _dataNascimento;
    }

这是另一个类的一部分,用于在 SQL 中翻译上述类的实例: 导入 br.myagenda.data.Contato; 导入 br.myagenda.util.SqlConnectionFactory;

public class ContatoDAO {
private final Connection _connection;

public ContatoDAO() {
    _connection = SqlConnectionFactory.getConnection();
}

public void add(Contato contato) {
    String sqlStatement = "INSERT INTO contatos (nome, email, endereco, data_nascimento)"
            + "VALUES (?,?,?,?)";

    PreparedStatement statemant = null;
    try {           
        statemant = _connection.prepareStatement(sqlStatement);         
        statemant.setString(1, contato.getNome());
        statemant.setString(2, contato.getEmail());
        statemant.setString(3, contato.getEndereco());

        Date dataParaGravar = new Date(Calendar.getInstance().getTimeInMillis());
        statemant.setDate(4, dataParaGravar);
        statemant.execute();
        statemant.close();          
    } catch(SQLException e) {
        throw new RuntimeException(e);
    }
}
...

还有一个名为“ContatoDaoTest”的主要方法的类,出于显而易见的原因调用上述类方法。

有趣的事情和问题是:如果我运行测试类(作为普通 Java 应用程序运行),该类可以正常访问数据库。

但是,我有 HTML 页面来显示一个页面来填写表单和一个 Servlet 来存储用户输入。基本上,它是页面访问者注册联系人的页面,因此,在内部,servlet 将实例化一个 Contato 和一个 ContatoDAO 以存储在数据库中。

<!DOCTYPE html SYSTEM "html.dtd"><html>
<head>
    <title>MyAgenda --- Adicionar um contato</title>
</head>
    <body>
        <form action="addContact">
            Nome: <input type="text" name="nome"/><br />
            E-Mail: <input type="text" name="email"/><br />
            Endereço: <input type="text" name="address" /><br />
            Data Nascimento: <input type="text" name="dataNascimento" /><br />
            <input type="submit" value="Gravar" />
        </form>
    </body>
</html>

但是,当我按下“Gravar”按钮时,Tomcat 会显示一个错误页面,指出存在 java.sql:SQLException:找不到合适的驱动程序!

问题是:为什么作为一个普通的 Java 应用程序,数据库访问有效,但作为一个 Java 网络应用程序,它却不起作用?

注意事项:

1 - mysql-connector 位于 WebContent/lib 中。

2 - mysql-connector 已添加到 eclipse 构建路径中。

3 - 我可以通过“导入”访问 mysql 连接器内部类。

补充:其实我找到了解决办法,但是感觉这样不是很方便:

古老的 SqlConnectorFactory:

public class SqlConnectionFactory {
    public static Connection getConnection() {
        try {
            return DriverManager.getConnection("jdbc:mysql://localhost/my_agenda",    "root",     "senhadomysql");
        } catch(SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

新:

public class SqlConnectionFactory {
    public static Connection getConnection() {
        try {
            DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //this line made the diference.
            return DriverManager.getConnection("jdbc:mysql://localhost/my_agenda",    "root",     "senhadomysql");
        } catch(SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

最佳答案

Why as a normal Java application, the database access works, but as a Java web app, it doesn't work?

Tomcat 找不到驱动com.mysql.jdbc.Driver。带有 mysql-connector-java 的 jar 应该在 (your_tomcat)/lib 目录 Tomcat

你的 SqlConnectionFactory 应该看起来像这样

Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL,USER,PASS);

更多详细信息,您可以查看链接: http://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm

关于java - 运行 Java Web 应用程序时出现 SQLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25875396/

相关文章:

php - 如何在 mysql 中检索表 id

java - 文本到语音转换器

java - 根据执行结果返回替代数据结构的方法

java - 运行时为 "No suitable driver",但不在 IDE 中

php - 如何根据相同的外键将 SQL 数据库中的多行显示到复选框中

mysql - 查找字符串 mySQL 中匹配表达式的数量

java - Eclipse 导出可运行的 jar,其中主类位于库 jar 中

java - 集成 Maven、Tycho 和 Eclipse 时处理非 OSGi 依赖项

java - 如何在扩展 Activity 中显示 Admob 插页式广告

java - 什么是NullPointerException,我该如何解决?