簡體   English   中英

將日期插入到Oracle數據庫中

[英]Insert a Date into oracle database

我需要在數據庫中插入日期,我有一個表,其中包含日期類型的行日期,但是我需要插入日期而不使用prepareStatement,但是它無法正常工作。 這是我的代碼:

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();
    }

正確的方法是:

  • 等待用戶輸入時不要保持數據庫連接處於活動狀態。 首先收集輸入, 然后連接到數據庫。
    原因:如果用戶運行緩慢,則連接可能會超時。

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

  • 使用PreparedStatement 切勿使用字符串連接與用戶提供的文本來構建SQL語句,因為這會使您的代碼容易崩潰,但更重要的是,它容易受到SQL Injection攻擊, 從而使黑客能夠竊取您的數據並刪除您的表

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

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();
    }
}

如果您只是插入當前日期,則可以使用Oracle的SYSDATE函數:

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

使用SYSDATE函數還將防止與日期相關的問題,這取決於代碼的執行位置(客戶端計算機還是中間層或DB層服務器)。

但是,我同意@Andreas,您在構建SQL語句時應避免用戶輸入值的字符串連接。 那當然是除非您喜歡和小鮑比桌快速而隨意地玩。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM