我在SQL Oracle数据库中有此表
MESSAGES
--------
Name Null? Type
-------------- -------- --------------
ID_MESSAGE NOT NULL NUMBER(38)
CONTENT NOT NULL NVARCHAR2(500)
TIME_STAMP NOT NULL TIMESTAMP(6)
我正试图在此表中插入数据,但是在插入时间戳时遇到问题。
我已经用Java生成了一个时间戳并执行了以下查询:
Timestamp timeStamp = new Timestamp(System.currentTimeMillis());
String query = "INSERT INTO messages(content, time_stamp) VALUES ('"+textAreaSendMessage.getText()+"', "+timeStamp+")";
st.executeQuery(query);
我收到此错误:
Caused by: java.sql.SQLSyntaxErrorException: ORA-00917: missing comma
如何解决此问题,以便将时间戳插入数据库?
答案 0 :(得分:1)
在定义表时使用from tkinter import *
root = Tk()
root.title("Validating user details")
Title = Label(root,text="Welcome, please login below", fg = "blue", bg = "yellow", font = "Verdana 30 bold", bd=1, relief="solid",padx=20)
Title.pack(side = TOP)
usernameFrame = Frame(root)
usernameFrame.pack(side = TOP)
uLab = Label(usernameFrame,text="Enter username: ",fg="light green",bg="green",font = "Calibri 26 italic",bd=1, relief="solid")
uLab.pack(side = LEFT)
username = Entry(usernameFrame)
username.pack(side = LEFT)
passwordFrame = Frame(root)
passwordFrame.pack(side = TOP)
pnLab = Label(passwordFrame,text="Enter password: ",fg="light green",bg="green",font = "Calibri 26 italic",bd=1, relief="solid")
pnLab.pack(side = LEFT,fill = X,expand = 1)
password = Entry(passwordFrame,show="*")
password.pack(side = LEFT)
resultFrame = Frame(root)
resultFrame.pack(side = TOP)
Output = Label(resultFrame,text="Display Result Here",fg="#008080",bg="#00FFFF",font = "Tahoma 30 bold",bd=1, relief="solid")
Output.pack(side = LEFT,fill = X,expand = 1)
def LoginF(username,password):
Login = False
file = open("OCR PPP Python Login List_user.txt","r")
data = file.read()
if username+","+password in data:
Output.configure(text="Successfully logged in")
Login = True
else:
Output.configure(text="Hmm.. Try again")
Login = False
file.close()
logButton = Button(resultFrame,text="Login",fg="#FF8C00",bg="#FF4500",font = "Ariel 28 underline",bd=1, relief="solid",command=LoginF)
logButton.pack(side = LEFT)
root.mainloop()
来调用捕获当前时刻的函数。每当您DEFAULT
新建一行时,此功能将自动运行。这样就不需要任何Java了。
如果您确实想在Java中做类似的事情,请不要使用INSERT
。该类在几年前被 java.time 类所取代。
java.time.Timestamp
使用OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;
占位符构建SQL字符串。不要纠缠于代码示例中看到的字符串操作。
使用?
对象传递OffsetDateTime
对象以填充?
占位符。
PreparedStatement
答案 1 :(得分:0)
我发现我什至不必在Java中创建时间戳,只需将CURRENT_TIMESTAMP添加到时间戳的位置到查询中应该是时间戳的位置。