2015-05-16 66 views
1

我想從數據選取器使用javaFx插入日期到mysql數據庫。 我很累,提交這段代碼。使用JavaFX將日期插入MySql數據庫Datepicker

@FXML 
private DatePicker DPCurrentDate;//fx:id="DPCurrentDate" 

// BrandAdded is a Method that i created for insert data into database 

private void BrandAdded(){ 

    DBConnection newCon = new DBConnection(); 
    con = newCon.geConnection(); 

    try { 
     pst = con.prepareStatement("insert into Brands values(?,?,?,?,?)"); 
     pst.setString(1, null); 
     pst.setString(2, TFBrandName.getText()); 
     pst.setString(3, TABrandDecription.getText()); 
     pst.setString(4, lblUserName.getText()); 
     pst.setDate(5, DPCurrentDate.getValue()); 
     pst.executeUpdate(); 

    } catch (SQLException ex) { 
     Logger.getLogger(AddBrandController.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

當我運行我的程序它給我這個錯誤

error: incompatible types: LocalDate cannot be converted to Date 
     pst.setDate(5, DPCurrentDate.getValue()); 

回答

2

你需要

java.util.Date date = 
    java.util.Date.from(dpCurrentDate.getValue().atStartOfDay(ZoneId.systemDefault()).toInstant()); 
java.sql.Date sqlDate = new java.sql.Date(date.getTime()); 
pst.setDate(5, sqlDate); 

(使用java.sql.Date)。

+0

我如何得到'dpCurrentDate'在那裏我'DPCurrentDate'爲Datapicker。 –

+0

我的意思是同樣的東西......我只是糾正你的變量名稱,以便它符合標準的命名約定:)。雖然我還有其他一些錯誤(當我把它們放在一起時,不知道我在想什麼)。他們現在已經修好了。 –

0
import java.sql.Date; 
Date DPCurrentDate1 = Date.valueOf(DPCurrentDate); 
pst.setDate(5, DPCurrentDate1); 

這將做你的工作

相關問題