2016-10-19 157 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的工作感谢你... –