简体   繁体   中英

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.

I have MySQL up and running on localhost. I have a database named "lab" and a table named "lab_user" with 1 record:

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

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. 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.

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

<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

<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.

I don't see a name attribute in the context. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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