![](/img/trans.png)
[英]java.sql.SQLException: ORA-00900: invalid SQL statement in Ecllips
[英]Error in setRepositoryConnection : java.sql.SQLException: ORA-00900: invalid SQL statement
在这里,当我将数据插入数据库时遇到错误。
直到在Oracle中的插入语句或该语句之后,我都不会出现任何错误。
package com.socket;
import java.util.ArrayList;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.*;
import java.sql.*;
import oracle.jdbc.*;
public class Message implements Serializable {
private static final long serialVersionUID = 1L;
public String type, sender, content, recipient;
public Message(String type, String sender, String content, String recipient) {
this.type = type;
this.sender = sender;
this.content = content;
this.recipient = recipient;
}
@Override
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Calendar cal = Calendar.getInstance();
String Current = sdf.format(cal.getTime());
String Content1 = content + Current;
Connection conn = null;
String driverName = "oracle.jdbc.OracleDriver";
String url = "jdbc:oracle:thin:@172.16.0.35:1521:orcl";
String username = "itn"; //SET USERNAME
String password = "itn"; //SET PASSWORD
try {
if (conn == null) {
Class.forName(driverName);
conn = DriverManager.getConnection(url, username, password);
String Query = "INSERT INTO ENTRIES (SERIALNO, TYPE, SENDER, CONTENT, TIMER, RECIPIENT, CREATEDON) VALUES ((Select max(SERIALNO)+1 from ENTRIES), '"
+ type + "', '" + sender + "', '" + content + "', TO_DATE('" + Current
+ "', 'DD/MM/YYYY HH24:MI:SS'), '" + recipient + "', TO_DATE('"
+ Current + "', 'DD/MM/YYYY HH24:MI:SS'))";
System.out.println(Query);
Statement st = conn.createStatement();
st.executeUpdate("Query");
st.close();
}
} catch (Throwable t) {
System.out.println("error in setRepositoryConnection : " + t);
//logger.log("Unable to set connection ", "Repository.java", "Repository", t, Logger.CRITICAL);
}
return "{type='" + type + "', sender='" + sender + "', contentx='" + Content1 + "', recipient='" + recipient + "'}";
}
}
这部分没有任何意义:
System.out.println(Query);
Statement st = conn.createStatement();
st.executeUpdate("Query");
您需要执行查询并使用生成的SQL:
int result = st.executeUpdate(Query);
最好的方法是使用PreparedStatement ,因为它使代码更易读,更易理解,提供了对参数的编译时检查并为SQL注入提供了保护:
PreparedStatement statement = conn.prepareStatement();
statement.setInt(1, intVariable);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.