简体   繁体   English

SQL代码在Java中给出了无效的SQL语句,但在SqlDeveloper中有效

[英]Sql code give invalid SQL statement in Java but it works in SqlDeveloper

I tried to make crud , but insert statement do not work from code 我试图使结局,但插入语句不能从代码

   try {
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());



        conn2 = DriverManager.getConnection(dbURL2, username, password);

        String sql="INSERT INTO Produce(name,description,Produce_Date,Price,CATEGORY_ID,Person_ID)VALUES('"+name+"','"+description+"',TO_DATE('"+sqlDate+"', 'yyyy-mm-dd'),"+price+","+category_Id+","+person_id+")";

        System.out.println(sql);
        stmt = conn2.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString("name"));
        }
        if (conn2 != null && !conn2.isClosed()) {
            conn2.close();
        }

    } catch (SQLException ex) {
        ex.printStackTrace();
    }   

I made a System.out.println to see my sql and to execute it to understand where is the problem, this is my String, and it works in sqlDeveloper: 我制作了一个System.out.println来查看我的sql并执行它以了解问题出在哪里,这是我的String,它可以在sqlDeveloper中使用:

INSERT INTO Produce(name,description,Produce_Date,Price,CATEGORY_ID,Person_ID)VALUES('ew','rrr',TO_DATE('2018-11-14', 'yyyy-mm-dd'),12.0,2,2)

you are using insert SQL query to retrieve result. 您正在使用插入SQL查询来检索结果。

I believe to retrieve name you need to execute select statement 我相信要检索名称,您需要执行select语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM