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.0k views
in Technique[技术] by (71.8m points)

jdbc - incompatible types java.sql.timestamp cannot be converted to java.security.timestamp

I am trying to fetch Data at what that Details has been added to my Database using Timestamp() but is show error of incompatible types java.sql.timestamp cannot be converted to java.security.timestamp

// Code User

package com.LearnCode.Entities;

import java.security.Timestamp;
import java.sql.*;


public class User {
    private int id;
    private String name;
    private String email;
    private String password;
    private String gender;
    private String about;
    private Timestamp dateTime;
    private String profile;

    public User(int id, String name, String email, String password, String gender, String about, Timestamp dateTime) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.password = password;
        this.gender = gender;
        this.about = about;
        this.dateTime = dateTime;
    }

    public User() {
    }

    public User(String name, String email, String password, String gender, String about) {
        this.name = name;
        this.email = email;
        this.password = password;
        this.gender = gender;
        this.about = about;
        
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

    public String getGender() {
        return gender;
    }

    public String getAbout() {
        return about;
    }

    public Timestamp getDateTime() {
        return dateTime;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public void setDateTime(Timestamp dateTime) {
        this.dateTime = dateTime;
    }

    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }    
}

// Code DAO user //incompatible types java.sql.timestamp cannot be converted to java.security.timestamp

public class UserDao {
    
    private Connection con;

    public UserDao(Connection con) {
        this.con = con;
    }
    
    // method to insert data of user in database
    
    public boolean saveUser(User user)
    {
        boolean f=false;
        try {
            
             String query = "insert into user(name,email,password,gender,about) values(?,?,?,?,?)";
             PreparedStatement pstmt = this.con.prepareStatement(query);
             pstmt.setString(1, user.getName());
             pstmt.setString(2, user.getEmail());
             pstmt.setString(3, user.getPassword());
             pstmt.setString(4, user.getGender());
             pstmt.setString(5, user.getAbout());
             
             pstmt.executeUpdate();
             f=true;
             
        } catch (Exception e) 
        {
            e.printStackTrace();
        }
        return f;
    }
    
    // Get User by User E-Mail & Password
    
    public User getUserByEmailAndPassword(String email,String password)
    {
      User user=null;
      
      
        try {
            String query ="select * from user Where email=?,password=?";
            PreparedStatement pstmt = con.prepareStatement(query);
            pstmt.setString(1, email);
            pstmt.setString(2, password);
            
            ResultSet set =pstmt.executeQuery();
            
            if(set.next())
            {
                user = new User();
             // Data From DB   
            String name = set.getString("name");
            //  Set in User Obj
            user.setName(name);
            
            user.setId(set.getInt("id"));
            user.setEmail(set.getString("email"));
            user.setPassword(set.getString("[password"));
            user.setGender(set.getString("gender"));
            user.setAbout(set.getString("about"));
            user.setDateTime(set.getTimestamp("regdate")); 
            user.setProfile(set.getString("profile"));
            
            }
           } catch (Exception e) {
            e.printStackTrace();
        }
      
      
        return user;
      
    }
    
}
question from:https://stackoverflow.com/questions/65915223/incompatible-types-java-sql-timestamp-cannot-be-converted-to-java-security-times

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

1 Reply

0 votes
by (71.8m points)

If you don't need to use java.security.TimeStamp explicitly, you have to remove that import. If you need to use java.security.TimeStamp you can create one in DAO while setting dateTime. Replace following user.setDateTime(set.getTimestamp("regdate")); with user.setDateTime(new java.sql.TimeStamp(set.getTimestamp("regdate"), YOUR_CERT_PATH));


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

...