简体   繁体   English

MySQL JDBC Web程序:数据库配置中的错误

[英]MySQL JDBC Web Program: error in DB config

I am trying to do a simple select from a MySQL database in a Java Web Project. 我正在尝试从Java Web Project中的MySQL数据库中进行简单选择。

I have MySQL up and running on localhost. 我有MySQL并在localhost上运行。 I have a database named "lab" and a table named "lab_user" with 1 record: 我有一个名为“ lab”的数据库和一个名为“ lab_user”的表,其中有1条记录:

在此处输入图片说明在此处输入图片说明

The issue is that every time that I try to select this 1 row from the program, I get an error saying that the table does not exist. 问题是,每次我尝试从程序中选择此1行时,都会收到一条错误消息,指出该表不存在。 So I believe that I may be doing something wrong with my DB config. 因此,我认为我的数据库配置可能做错了。

Below you can find my code, my web.XML and my context.XML. 在下面,您可以找到我的代码,web.XML和context.XML。

Could you please help me to find out if there is anything wrong in here that is preventing from me to accessing the correct database? 您能否帮助我找出这里是否有什么问题阻止我访问正确的数据库?

THANK YOU 谢谢

code

package DAO;

import java.sql.*;
import javax.naming.*;
import javax.sql.*;

import DTO.UserDTO;

public class UserDAO {

    private Connection connection = null;
    private Statement statement = null;
    private PreparedStatement prepStatement = null;
    private ResultSet results = null;
    private static final String DATASOURCE_NAME = "java:comp/env/jdbc/lab";


    public void getConnection() {
        if (connection == null) {
            try {
                Context initialContext = new InitialContext();
                DataSource ds = (DataSource) initialContext.lookup(DATASOURCE_NAME);
                connection = ds.getConnection();
            } catch (NamingException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public UserDTO selectUser(String userID) {

        //pessoal
        String id = "";
        String name = "";

        try {
            getConnection();
            statement = connection.createStatement();
            results = statement.executeQuery("select * from lab_user where user_id = 1");

            if (results != null) {
                results.next();

                id = results.getString("id");
                name = results.getString("name");
            }
            results = null;
            ///

        } catch (SQLException e) {

            String error = "" + e;
            e.printStackTrace();

        } finally {
            cleanUp();

        }
        results = null;

        UserDTO uDTO = new UserDTO();
        uDTO.setName(name);
        uDTO.setID(id);

        return uDTO;
    }

web.XML web.XML

<description>DB Connection</description>
  <res-ref-name>jdbc/lab</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>

context.XML 上下文XML

<Context>
  <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="30" maxIdle="100" maxWait="10000" name="labDB" password="hacking" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/lab?zeroDateTimeBehavior=convertToNull" username="fabio"/>
</Context>

Error: java.sql.SQLSyntaxErrorException: Table/View 'LAB_USER' does not exist. 错误:java.sql.SQLSyntaxErrorException:表/视图'LAB_USER'不存在。

I don't see a name attribute in the context. 我在上下文中看不到name属性。 It should look like this: 它看起来应该像这样:

<Context>
    <Resource name="jdbc/lab" auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="30" maxIdle="100" maxWait="10000" name="labDB" password="hacking" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/lab?zeroDateTimeBehavior=convertToNull" username="fabio"/>
</Context>

Otherwise the application cannot access the defined data source. 否则,应用程序将无法访问定义的数据源。

If you look to the code: 如果您查看代码:

   id = results.getString("id");
   name = results.getString("name");

The column names are wrong: they are user_id and user_name. 列名称错误:它们是user_id和user_name。 I don't know why the error message was saying about table name. 我不知道为什么错误消息说的是表名。

It is amazing how these little things costs us a day of investigation. 这些小事情使我们花费了一天的调查时间,真是令人惊讶。

Cheers 干杯

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

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