Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...