Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在oracle数据库中插入日期_Java_Sql_Oracle - Fatal编程技术网

Java 在oracle数据库中插入日期

Java 在oracle数据库中插入日期,java,sql,oracle,Java,Sql,Oracle,我需要在我的数据库中插入日期,我有一个表,其中包含日期类型的行日期,但我需要在不使用preparedStatement的情况下插入日期,但它不起作用。这是我的密码: try{ dbConnection = DriverManager.getConnection(DR_URL, DB_USER,DB_PASSWORD); stmt = dbConnection.createStatement(); for(int i=1; i<3; i++

我需要在我的数据库中插入日期,我有一个表,其中包含日期类型的行日期,但我需要在不使用preparedStatement的情况下插入日期,但它不起作用。这是我的密码:

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();
    }
试试看{
dbConnection=DriverManager.getConnection(DR_URL、DB_用户、DB_密码);
stmt=dbConnection.createStatement();

对于(int i=1;i而言,正确的方法是:

  • 在等待用户输入时,不要保持数据库连接处于活动状态。请先收集输入,然后连接到数据库。
    原因:如果用户速度慢,连接可能超时

  • 用于清理JDBC资源。
    原因:保证清理,更好的错误处理,更干净的代码

  • 使用。切勿使用字符串连接和用户提供的文本来构建SQL语句,因为这会使代码容易崩溃,但更重要的是,容易受到攻击,允许黑客窃取您的数据并删除您的表

因为需要收集多组值,所以创建一个类来保留这些值

公共类发票{
私有最终字符串invoiceNumber;
私有最终字符串customerName;
私人最终日期发票日期;
公共发票(字符串invoiceNumber、字符串customerName、日期invoiceDate){
this.invoiceNumber=invoiceNumber;
this.customerName=客户名称;
this.invoiceDate=invoiceDate;
}
公共字符串getInvoiceNumber(){
返回此.invoiceNumber;
}
公共字符串getCustomerName(){
退回这个。客户名称;
}
公共日期getInvoiceDate(){
返回此.invoiceDate;
}
}
//提示用户输入两张发票
列表发票=新的ArrayList();
对于(int i=1;i<3;i++){
字符串invoiceNumber=JOptionPane.showInputDialog(“发票号:”);
字符串customerName=JOptionPane.showInputDialog(“客户名称:”);
发票。添加(新发票(发票编号、客户名称、新日期());
}
//插入发票
try(Connection dbConnection=DriverManager.getConnection(DR_URL、DB_用户、DB_密码)){
字符串sql=“插入发票主值(?,?)”;
try(PreparedStatement stmt=dbConnection.prepareStatement(sql)){
用于(发票:发票){
stmt.setString(1,invoice.getInvoiceNumber());
stmt.setString(2,invoice.getCustomerName());
stmt.setDate(3,新的java.sql.Date(invoice.getInvoiceDate().getTime());
stmt.addBatch();
}
stmt.executeBatch();
}
}

正确的方法是:

  • 在等待用户输入时,不要保持数据库连接处于活动状态。请先收集输入,然后连接到数据库。
    原因:如果用户速度慢,连接可能超时

  • 用于清理JDBC资源。
    原因:保证清理,更好的错误处理,更干净的代码

  • 使用。切勿使用字符串连接和用户提供的文本来构建SQL语句,因为这会使代码容易崩溃,但更重要的是,容易受到攻击,允许黑客窃取您的数据并删除您的表

因为需要收集多组值,所以创建一个类来保留这些值

公共类发票{
私有最终字符串invoiceNumber;
私有最终字符串customerName;
私人最终日期发票日期;
公共发票(字符串invoiceNumber、字符串customerName、日期invoiceDate){
this.invoiceNumber=invoiceNumber;
this.customerName=客户名称;
this.invoiceDate=invoiceDate;
}
公共字符串getInvoiceNumber(){
返回此.invoiceNumber;
}
公共字符串getCustomerName(){
退回这个。客户名称;
}
公共日期getInvoiceDate(){
返回此.invoiceDate;
}
}
//提示用户输入两张发票
列表发票=新的ArrayList();
对于(int i=1;i<3;i++){
字符串invoiceNumber=JOptionPane.showInputDialog(“发票号:”);
字符串customerName=JOptionPane.showInputDialog(“客户名称:”);
发票。添加(新发票(发票编号、客户名称、新日期());
}
//插入发票
try(Connection dbConnection=DriverManager.getConnection(DR_URL、DB_用户、DB_密码)){
字符串sql=“插入发票主值(?,?)”;
try(PreparedStatement stmt=dbConnection.prepareStatement(sql)){
用于(发票:发票){
stmt.setString(1,invoice.getInvoiceNumber());
stmt.setString(2,invoice.getCustomerName());
stmt.setDate(3,新的java.sql.Date(invoice.getInvoiceDate().getTime());
stmt.addBatch();
}
stmt.executeBatch();
}
}

如果只是插入当前日期,可以使用Oracle的SYSDATE函数:

stmt.executeUpdate("INSERT INTO INVOICEMAIN VALUES ('" + invoiceNumber + "','" + customerName + "',SYSDATE)");
使用SYSDATE函数还可以防止与日期相关的问题,这取决于代码的执行位置(客户机与中间层或数据库层服务器)


但是,我同意@Andreas的观点,在构建SQL语句时,您应该避免将用户输入的值串接在一起。当然,除非您喜欢随意使用。如果您只是插入当前日期,您可以使用Oracle的SYSDATE函数:

stmt.executeUpdate("INSERT INTO INVOICEMAIN VALUES ('" + invoiceNumber + "','" + customerName + "',SYSDATE)");
使用SYSDATE函数还可以防止与日期相关的问题,这取决于代码的执行位置(客户机与中间层或数据库层服务器)


但是,我同意@Andreas的观点,在构建SQL语句时,您应该避免将用户输入的值串接在一起。当然,除非您喜欢快速和松散地使用这些语句。

…但是它不起作用。
到底什么不起作用?您应该格式化日期,看看nls oracle配置的格式是什么?为什么您不能使用准备好的语句,哪一种方法是正确的?您可以执行ALTER SESSION SET NLS\U DATE\F