类型不匹配:无法从 java.util.Date 转换为 java.sql.Date

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22351434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:09:29  来源:igfitidea点击:

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

javamysqlsimpledateformat

提问by Julie24

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

我试图让我的代码与代码中的 SimpleDateFormat 类一起使用。有人知道为什么吗?祝朱莉度过愉快的一天

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:

当我运行导入的代码时:import java.util.Date。我在行中收到错误:“日期类型不明确”:

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:

当我运行导入的代码时: import java.sql.Date; 我收到错误:

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"

“类型不匹配:无法从 java.util.Date 转换为 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);

采纳答案by Ravinder Reddy

java.util.Dateand java.sql.Dateare different. Database accepts only java.sql.Date.
For that, you need to convert java.util.Dateinto java.sql.Date.

java.util.Date并且java.sql.Date是不同的。数据库只接受java.sql.Date.
为此,您需要转换java.util.Datejava.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_StartDateto set parameter values using prepared statement.

现在您可以使用它sql_StartDate来使用准备好的语句设置参数值。

pst.setDate( 1, sql_StartDate );

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

在其他 sql 特定日期上使用相同的过程以与 jdbc 一起使用。

回答by Rich O'Kelly

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

如果您同时导入了java.sql.*java.util.*命名空间,编译器就无法唯一确定Date令牌的最佳类型。在这种情况下,您可以使用命名空间限定您的类型:

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

回答by Edwin Rafael Pimienta Calderon

Date fechaNace= new java.sql.Date(persona.getFechaNacimiento().getTime());
ps.setDate(6,(java.sql.Date) fechaNace);