2

I am trying to get my code to work with the SimpleDateFormat class here in the code. Does anybody knows why? Have a nice day from Julie

When I run the code where I have imported: import java.util.Date. I get the error: "The type Date is ambiguous" in the lines:

Date startDate = format.parse(req.getParameter("startDate")); 
Date endDate = format.parse(req.getParameter("endDate"));

When I run the code where I have imported: import java.sql.Date; I get the error:

Date startDate = format.parse(req.getParameter("startDate")); 
Date endDate = format.parse(req.getParameter("endDate"));

"Type mismatch: cannot convert from java.util.Date to java.sql.Date"

import java.sql.Date;

package WorkPackage;

import java.io.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.util.*;
import java.util.Date.*;


    public void doPost(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException{

        String connectionURL = "jdbc:mysql://localhost/NekiWork";
        Connection connection=null;
        //String startDate = req.getParameter("startDate");
        //String endDate= req.getParameter("endDate");

        try {
            //Load database driver
            Class.forName("com.mysql.jdbc.Driver");
            //Connection to the database
            connection = DriverManager.getConnection(connectionURL, "root", ""); 
            //Getting the data from database

            String sql = "SELECT *, (Day_hours + (Day_minutes / 60)) AS Allday_hours FROM Workdata "
                    + "WHERE Date = startdate = ? AND endDate = ? ";
            PreparedStatement pst = connection.prepareStatement(sql);

            //Date startDate;

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = format.parse(req.getParameter("startDate")); 
            Date endDate = format.parse(req.getParameter("endDate"));
            pst.setDate(1,startDate);
            pst.setDate(2, endDate);
0

3 Answers 3

5

java.util.Date and java.sql.Date are different. Database accepts only java.sql.Date.
For that, you need to convert java.util.Date into java.sql.Date.

Try this:

java.util.Date util_StartDate = format.parse( req.getParameter("startDate") );
java.sql.Date sql_StartDate = new java.sql.Date( util_StartDate.getTime() );

Now you can use this sql_StartDate to set parameter values using prepared statement.

pst.setDate( 1, sql_StartDate );

Use the same procedure on other sql specific dates to use with jdbc.

Sign up to request clarification or add additional context in comments.

Comments

1

Where you have both the java.sql.* and java.util.* namespaces imported, the compiler cannot uniquely determine a best type for the Date token. In this instance you can qualify your type with the namespace:

java.util.Date startDate = format.parse(req.getParameter("startDate"));

1 Comment

thanks a lot for the answer. When I do this I get an error in my pst.setDate(1,startDate); where it says: "The method setDate(int, java.sql.Date) in the type PreparedStatement is not applicable for the arguments (int, java.util.Date)" Do you know why this is?
-2
Date fechaNace= new java.sql.Date(persona.getFechaNacimiento().getTime());
ps.setDate(6,(java.sql.Date) fechaNace);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.