2016-10-19 158 views
0

我需要插入日期到我的數據庫中,我有一個包含行日期類型日期的表,但我需要插入日期而不使用preparedStatement但它不會工作。這裏是我的代碼:插入日期到oracle數據庫

try{ 

     dbConnection = DriverManager.getConnection(DR_URL, DB_USER,DB_PASSWORD); 
     stmt = dbConnection.createStatement(); 

     for(int i=1; i<3; i++){ 
         String invoiceNumber = JOptionPane.showInputDialog("Invoice Number:"); 
         String customerName = JOptionPane.showInputDialog("Customer Name:"); 
         Date invoiceDate = new Date(System.currentTimeMillis()); 
         java.sql.Date invDate = new java.sql.Date (invoiceDate.getTime()); 

         stmt.executeUpdate("INSERT INTO INVOICEMAIN VALUES ('" + invoiceNumber + "','" + customerName + "','" + setDate(invDate) + "')"); 
        } 

     stmt.close(); 
     dbConnection.close(); 
    } 
+0

'......但它不會工作什麼不工作? –

+1

你應該格式化日期,看看oracle的配置格式是什麼nls oracle34配置 –

+3

爲什麼你不能使用準備好的語句,這將是_correct_方法來做到這一點? –

回答

2

正確的方式做到這一點:

  • 不要把數據庫連接活在等待用戶輸入。先收集輸入,然後連接到數據庫。
    原因:如果用戶速度較慢,連接可能會超時。

  • 使用try-with-resources清理JDBC資源。
    原因是:保證清理,更好的錯誤處理,更乾淨的代碼。

  • 使用PreparedStatement。切勿使用字符串連接和用戶提供的文本來構建SQL語句,因爲這會使您的代碼容易遭受崩潰,但更重要的是,容易受到攻擊,允許黑客竊取您的數據並刪除您的表

由於您需要收集多組值,請創建一個類來保留這些值。

public class Invoice { 
    private final String invoiceNumber; 
    private final String customerName; 
    private final Date invoiceDate; 
    public Invoice(String invoiceNumber, String customerName, Date invoiceDate) { 
     this.invoiceNumber = invoiceNumber; 
     this.customerName = customerName; 
     this.invoiceDate = invoiceDate; 
    } 
    public String getInvoiceNumber() { 
     return this.invoiceNumber; 
    } 
    public String getCustomerName() { 
     return this.customerName; 
    } 
    public Date getInvoiceDate() { 
     return this.invoiceDate; 
    } 
} 
// Prompt user for two invoices 
List<Invoice> invoices = new ArrayList<>(); 
for (int i = 1; i < 3; i++) { 
    String invoiceNumber = JOptionPane.showInputDialog("Invoice Number:"); 
    String customerName = JOptionPane.showInputDialog("Customer Name:"); 
    invoices.add(new Invoice(invoiceNumber, customerName, new Date())); 
} 

// Insert invoices 
try (Connection dbConnection = DriverManager.getConnection(DR_URL, DB_USER, DB_PASSWORD)) { 
    String sql = "INSERT INTO INVOICEMAIN VALUES (?,?,?)"; 
    try (PreparedStatement stmt = dbConnection.prepareStatement(sql)) { 
     for (Invoice invoice : invoices) { 
      stmt.setString(1, invoice.getInvoiceNumber()); 
      stmt.setString(2, invoice.getCustomerName()); 
      stmt.setDate (3, new java.sql.Date(invoice.getInvoiceDate().getTime())); 
      stmt.addBatch(); 
     } 
     stmt.executeBatch(); 
    } 
} 
+0

謝謝安德烈亞斯這是做這件事的最好方法。 –

+0

對我們'setTimestamp()'更好,因爲Oracle'DATE'實際上是:一個時間戳。 'setDate()'(或者更準確地說'java.sql.Date')將時間部分設置爲'00:00:00' –

+0

@a_horse_with_no_name真,儘管發票日期可能應該是僅限日期,因此在'Invoice'類中使用新的Java 8'LocalDate'會更好,然後它會變成'stmt.setDate(3,java.sql.Date.valueOf(invoice.getInvoiceDate()) );' – Andreas

0

如果你只是插入當前日期,你可以使用Oracle的SYSDATE功能:

stmt.executeUpdate("INSERT INTO INVOICEMAIN VALUES ('" + invoiceNumber + "','" + customerName + "',SYSDATE)"); 

使用SYSDATE功能也將防止日期相關的問題取決於代碼執行,其中(客戶機與中間層或數據庫層服務器)。

但是,我同意@Andreas在構建SQL語句時應避免用戶輸入值的字符串連接。這是除非你喜歡與little Bobby Tables快速鬆動。

+0

抱歉,對於遲到的答覆,SYSDATE的工作感謝你... –