gpt4 book ai didi

java - ORA-00936 : missing expression oracle 11g

转载 作者:行者123 更新时间:2023-12-01 21:33:57 32 4
gpt4 key购买 nike

这是我在 Oracle 中的表:

CREATE TABLE  "STUDENT" 
( "STU_ID" NUMBER(10,0),
"NAME" VARCHAR2(20),
"MOBILENO" NUMBER(10,0),
"EMAIL" VARCHAR2(50),
"ADDRESS" VARCHAR2(100),
"STREAM" CHAR(50),
"SEMESTER" CHAR(50),
"DATE_OF_BIRTH" DATE,
CONSTRAINT "PK_STU_ID" PRIMARY KEY ("STU_ID") ENABLE
) ;

我想通过按 eclipse 中的添加按钮将学生信息存储在上表中。尝试执行此行“st.executeUpdate(sql);”时出现缺少表达式错误,代码如下:

JLabel lblAdd = new JLabel(" Add");
lblAdd.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
Clock=new Timer(5000,null);
Clock.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try {
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE","ADMIN","admin");
Statement st=con.createStatement();
ResultSet rslt=st.executeQuery("select * from student");
ResultSet stu_seq=st.executeQuery("select student_sequence.nextval from DUAL");
System.out.println("getting resultset");
if(stu_seq.next())
{ System.out.println(stu_seq.getInt(1));

String name=txtNm.getText().trim();
String email=txtEmailid.getText().trim();
String mobile=txtMobileNo.getText().trim();


String sql="INSERT INTO student(STU_ID,NAME,MOBILENO,EMAIL,ADDRESS,DATE_OF_BIRTH,STREAM,SEMESTER) VALUES ("+stu_seq.getInt(1)+",'"+name+"','"+mobile+"','"+email+"',";

sql+="'"+txtPrmntAdd.getText().trim()+"',";

sql+="'"+cmbDay.getSelectedItem()+"'";
sql+="'"+cmbMnth.getSelectedItem()+"'";
sql+="'"+cmbYr.getSelectedItem()+"',";

sql+="'"+cmbRgstrStrm.getSelectedItem()+"',";

sql+="'"+cmbRgstrSem.getSelectedItem()+"',";


sql+=")";

st.executeUpdate(sql);
// System.out.println("after executing");
}

System.out.println("Inserted into database");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frameReset();
//lblStatus.setText("data saved successfully");
//lblAni.setVisible(false);
Clock.stop();
}
});
Clock.start();
//lblStatus.setVisible(true);
//lblAni.setVisible(true);
}
});
lblAdd.setBounds(172, 340, 60, 18);
panelRegister.add(lblAdd);
lblAdd.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
lblAdd.setForeground(new Color(0, 51, 102));
lblAdd.setFont(new Font("Tw Cen MT", Font.PLAIN, 16));
lblAdd.setIcon(new ImageIcon(Student.class.getResource("/img/Add-New-16.png")));

想要将学生信息存储在表中。我哪里做错了,导致出现缺少表达式错误?

最佳答案

您的代码片段是错误的:

 sql+="'"+cmbRgstrSem.getSelectedItem()+"',";   
sql+=")";

因为它在 INSERT 语句末尾给出以下结果:

INSERT ....VALUES( ...... , 'someValue',)

数据库期望最后一个逗号后的下一个值,但那里什么也没有。

<小时/>

顺便说一句,使用PreparedStatement来完成这样的任务,
有关详细信息,请参阅此链接 - 这是来自 Oracle 的教程:“使用准备好的语句”:https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

还可以研究这个答案,了解为什么应该使用PreparedStatement:
When should we use a PreparedStatement instead of a Statement?

关于java - ORA-00936 : missing expression oracle 11g,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37124373/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com