当我尝试运行代码时,我收到错误消息“ORA-00927:缺少等号”,无法在代码中检测到它并且无法修复它。我的意思是错误在UPDATE
方法中,但我不确定。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;strong text
import javax.swing.border.EmptyBorder;
import net.proteanit.*;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;
import javax.swing.*;
import net.proteanit.sql.DbUtils;
public class Korisnici extends JFrame {
private JPanel contentPane;
private JTable table;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Korisnici frame = new Korisnici();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Connection connection = null;
protected ResultSet rs;
private JTextField textFieldPhoneNumber;
private JTextField textFieldName;
private JTextField textFieldEmail;
private JTextField textFieldID;
public Korisnici() {
connection = sqlConnection.dbConnector();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 777, 512);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnLoadTable = new JButton("Load \"Korisnici\" table");
btnLoadTable.setFont(new Font("Tahoma", Font.BOLD, 14));
btnLoadTable.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String query = "SELECT * FROM KORISNICI";
PreparedStatement pst= connection.prepareStatement(query);
ResultSet ps = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(ps));
} catch (Exception e) {
e.printStackTrace();
}
}
});
btnLoadTable.setBounds(411, 11, 211, 50);
contentPane.add(btnLoadTable);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(305, 83, 446, 311);
contentPane.add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
JLabel lblId = new JLabel("ID");
lblId.setFont(new Font("Tahoma", Font.BOLD, 14));
lblId.setBounds(10, 83, 115, 60);
contentPane.add(lblId);
JLabel lblEmail = new JLabel("Email");
lblEmail.setFont(new Font("Tahoma", Font.BOLD, 14));
lblEmail.setBounds(10, 154, 115, 60);
contentPane.add(lblEmail);
JLabel lblName = new JLabel("Name");
lblName.setFont(new Font("Tahoma", Font.BOLD, 14));
lblName.setBounds(10, 225, 115, 60);
contentPane.add(lblName);
JLabel lblPhonenumber = new JLabel("Phone number");
lblPhonenumber.setFont(new Font("Tahoma", Font.BOLD, 14));
lblPhonenumber.setBounds(10, 296, 120, 60);
contentPane.add(lblPhonenumber);
textFieldPhoneNumber = new JTextField();
textFieldPhoneNumber.setBounds(135, 313, 155, 30);
contentPane.add(textFieldPhoneNumber);
textFieldPhoneNumber.setColumns(10);
textFieldName = new JTextField();
textFieldName.setBounds(135, 242, 155, 30);
contentPane.add(textFieldName);
textFieldName.setColumns(10);
textFieldEmail = new JTextField();
textFieldEmail.setBounds(135, 171, 155, 30);
contentPane.add(textFieldEmail);
textFieldEmail.setColumns(10);
textFieldID = new JTextField();
textFieldID.setBounds(135, 100, 155, 30);
contentPane.add(textFieldID);
textFieldID.setColumns(10);
JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String query = "INSERT INTO korisnici (ID, EMAIL, NAME, PHONE_NUMBER) values (?, ?, ?, ?)";
PreparedStatement pst = connection.prepareStatement(query);
pst.setString(1, textFieldID.getText());
pst.setString(2, textFieldEmail.getText());
pst.setString(3, textFieldName.getText());
pst.setString(4, textFieldPhoneNumber.getText());
ResultSet rs = pst.executeQuery();
JOptionPane.showMessageDialog(null, "Data Saved");
pst.close();
rs.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
btnSave.setFont(new Font("Tahoma", Font.BOLD, 14));
btnSave.setBounds(10, 400, 90, 40);
contentPane.add(btnSave);
JButton btnUpdate = new JButton("Update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String query = "UPDATE korisnici SET ID='"+textFieldID.getText()+"' ,email='"+textFieldEmail.getText()+"' ,name='"+textFieldName.getText()+"' ,Phone number='"+textFieldPhoneNumber.getText()+"' ";
PreparedStatement pst = connection.prepareStatement(query);
pst.execute();
JOptionPane.showMessageDialog(null, "Data Updated");
pst.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
btnUpdate.setFont(new Font("Tahoma", Font.BOLD, 14));
btnUpdate.setBounds(110, 400, 90, 40);
contentPane.add(btnUpdate);
}
}
答案 0 :(得分:3)
更新:
String query = "UPDATE korisnici SET ID='"+textFieldID.getText()
+"' ,email='"+textFieldEmail.getText()+"',name='"+textFieldName.getText()
+"' ,Phone number='"+textFieldPhoneNumber.getText()+"' ";
坚持这个片段:
.... ,Phone number='"+ .....
在Oracle中,非引用标识符不能包含空格,请参阅:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements008.htm
- 不带引号的标识符只能包含来自的字母数字字符 您的数据库字符集和下划线(_),美元符号($), 和英镑符号(#)。数据库链接还可以包含句点(。)和 “at”标志(@)。 Oracle强烈反对使用$和#in 不带引号的标识符。
醇>带引号的标识符可以包含任何字符和标点符号 以及空间。但是,既没有引用也没有引用标识符 可以包含双引号或空字符(\ 0)。
换句话说:Phone number
被视为名为Phone
的列,Oracle在更新语句中的列名后面需要=
,但它得到number
,然后抛出错误。