2015-05-01 84 views

回答

0

我會寫一個新的convert-specified Class,它有這個選項。

import java.text.SimpleDateFormat; 
import java.util.Date; 


public class convertText { 
    public java.sql.Date formatStringToSQLDate(String strDate) throws Exception{ 
     Date utilDate = new Date(); //DateFormat 
     SimpleDateFormat dfFormat = new SimpleDateFormat("dd.MM.yyyy"); // parse string into a DATE format  
     utilDate = dfFormat.parse(strDate); // convert a util.Date to milliseconds via its getTime() method   
     long time = utilDate.getTime(); // get the long value of java.sql.Date 
     java.sql.Date sqlDate = new java.sql.Date(time); 
     return sqlDate; 
    } 
} 

然後,就像這樣使用它。

Connection dp = DriverManager.getConnection("jdbc:ucanaccess://" + Filepath + Filename); 
Statement stat = dp.createStatement(); 
String sql = "INSERT INTO user_table (Name, Birth) VALUES(?, ?)"; //statement as string 
PreparedStatement pstmt = dp.prepareStatement(sql); 
pstmt.setString(1, textFieldVorname.getText()); //replaces the first '?' 
pstmt.setDate(2, Date.formatStringToSQLDate(textFieldGeburtsdatum.getText())); //replaces the second '?' 
pstmt.executeUpdate(); //executes the insert-query