Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.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的日期_Java_Mysql_Sql_Jdbc - Fatal编程技术网

如何获取java的日期

如何获取java的日期,java,mysql,sql,jdbc,Java,Mysql,Sql,Jdbc,如何修复此代码?在处理日期时,我应该使用什么来代替整数%d public static void entryAdd() { Scanner scan= new Scanner(System.in); System.out.print(" Student Number :"); int newNumber= scan.nextInt(); System.out.print(" Entry no :"); i

如何修复此代码?在处理日期时,我应该使用什么来代替整数%d

public static void entryAdd()
    {
        Scanner scan= new Scanner(System.in);
        System.out.print(" Student Number  :");
        int newNumber= scan.nextInt();
        System.out.print(" Entry no  :");
        int entryNumber=scan.nextInt();
        System.out.print(" Borrow Date :");
        int borrowDate=scan.nextInt();
        System.out.print("Return Date :");
        int returnDate=scan.nextInt();
        try{
            Statement stmt=con.createStatement(); 
            String query=String.format("insert into entry values( %d, %d, %d, %d, %d)", newNumber,entryNumber,borrowDate, returnDate);
            int add = stmt.executeUpdate(query);
            System.out.println("Entry Added");
        }catch(Exception e){ System.out.println(e);}

    }

可以使用字符串代替日期,在MySQL中可以插入字符串日期。MySQL将把它改为RealDate

public static void entryAdd()
{
    Scanner scan= new Scanner(System.in);
    System.out.print(" Student Number  :");
    int newNumber= scan.nextInt();
    System.out.print(" Entry no  :");
    int entryNumber=scan.nextInt();
    System.out.print(" Borrow Date :");
    String borrowDate =scan.next();
    System.out.print("Return Date :");
    String returnDate =scan.next();
    try{
        PreparedStatement stmt=con.prepareStatement("insert into entry values( ?, ?, ?, ?)");
        stmt.setInt(1, neNumber);
        stmt.setInt(2, entryNumber);
        stmt.setString(3, borrowDate);
        stmt.setString(4, returnDate);
        int i=stmt.executeUpdate();   
        System.out.println("Entry Added");
    }catch(Exception e){ 
        System.out.println(e);
    } finally() {
        try {
            stmt.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

您不应该使用
String.format
来构造查询字符串。当与字符串值(或其他对象)一起使用时,它会使您的代码容易受到SQL注入的影响或难以诊断语法错误

正确的解决方案大致如下:

// ...
// You may need to use `DateTimeFormatter` with a specific format, this will use yyyy-MM-dd
LocalData borrowDate = LocalDate.parse(scan.next())
LocalData returnDate = LocalDate.parse(scan.next())
try (Statement stmt = con.prepareStatement(
        "insert into entry (newNumber, entryNumber, borrow, return) values (?, ?, ?, ?)")) {
    stmt.setInt(1, newNumber);
    stmt.setInt(2, entryNumber);
    stmt.setObject(3, borrowDate);
    stmt.setObject(4, returnDate);
    stmt.executeUpdate();
}
这假设您正在插入一个
DATE
列,并且您的驱动程序符合JDBC 4.2


我还显式地指定了要插入的列(我根据变量名猜测了列名)。这可以防止您在插入重新排序的列时遇到问题,或者在添加了(可为空)列的表中发生更改。

谢谢。它起作用了。但此行应为String query=String.format(“插入条目值(%d,%d,%s,%s)”、newNumber、entryNumber、借用日期、返回日期);这是不安全的,正确的解决方案是使用带有参数占位符的准备好的语句。将来自不受信任源的字符串值连接(或插入)到查询字符串中会使代码容易受到SQL注入的攻击。在这种情况下,您的解决方案将导致语法错误。好吧,您是正确的,但这只适用于一个小型java程序来学习语句。如果您希望它完美,还应该关闭finally块中的连接。只是修改了答案使之更好。