Reputation: 279
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);
Upvotes: 2
Views: 23049
Reputation: 1
Date fechaNace= new java.sql.Date(persona.getFechaNacimiento().getTime());
ps.setDate(6,(java.sql.Date) fechaNace);
Upvotes: -2
Reputation: 24002
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.
Upvotes: 5
Reputation: 41757
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"));
Upvotes: 1