【问题标题】:SQLException when I run a Java web app运行 Java Web 应用程序时出现 SQLException
【发布时间】:2014-09-16 17:51:25
【问题描述】:

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

详细说明:我有这个类来代表一个联系人: 包 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 Web 应用程序,它不起作用?

注意事项:

1 - mysql-connector 在 WebContent/lib 中。

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

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

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

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

【问题讨论】:

  • 如果我没记错的话,您需要在您的网络服务器上“安装”驱动程序。在 lib 文件夹中包含 dll 是不够的
  • 您能详细说明一下吗?我对 JavaEE 很陌生。
  • 对不起,我也不是很熟悉。我只是让服务员帮我做。您可能想谷歌,看看是否有任何弹出。

标签: java mysql eclipse tomcat jakarta-ee


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多