Lỗi trong java swing sql

Em đang tập lập trình JAVA Swing với Sql

MÀ BỊ LỖI: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '? Và Tên, ??a Ch?, ?i?n Tho?i, S? ti?n ?ã thanh toán) VALUES (‘DA’, ‘DA’, ‘D’ at line 1

Đoạn code em kết nối với database:

if( operation =='i')
	{	
		try {
			ps= con.prepareStatement("INSERT INTO lblquanlyktx( MSSV, Họ Và Tên, Địa Chỉ, Điện Thoại, Số tiền đã thanh toán) VALUES (?, ?, ?, ?, ?)");
			ps.setString(1, mssv);
			ps.setString(2, hovaten);
			ps.setString(3, diachi);
			ps.setString(4, dienthoai);
			ps.setString(5, tien);
			
			if(ps.executeUpdate() >0) {
				
				JOptionPane.showMessageDialog(null, "new student added");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

Còn đoạn này, em cho nó vào trong nút Add

public void actionPerformed(ActionEvent e) {

			String mssv= txtmssv.getText();
			String hovaten= txthovaten.getText();
			String diachi= txtdiachi.getText();
			String dienthoai= txtdienthoai.getText();
			String tien= txtthanhtoan.getText();	
			 
			Student ku= new Student();
			ku.themSuaXoa('i', null, mssv, hovaten, diachi, dienthoai, tien);
		
		}
	});

đọc lỗi kìa e, lỗi cú pháp sql
tên các cột trong bảng nên đổi thành tiếng anh

1 Like

sửa cú pháp sql sao anh

cách đơn giản đổi tên cột trong bảng sql ấy

cái chỗ mấy cái dấu chấm hỏi đó. em băng khăn. phải sửa sao cho đúng hả a

dùng eclipse thì thử trong setting project có phần utf-8 xem

EM đã chỉnh rồi nhaaaaaaaaaaaaaa

Xem lại dòng 1 đi em. Google xem đúng cú pháp chưa.

Em đã chuyển hết, database, nhập học sinh bằng tiếng anh hết.
Nhưng nó lại lỗi như này: Parameter index out of range (1 > number of parameters, which is 0).
=> Em đã google nhưng vẫn chưa rõ được mong được giúp đỡ ạ

public class Student {
	
	public void themSuaXoa( char operation,Integer stt, String mssv, String hovaten, String diachi, String dienthoai, String tien) {
		
		Connection con= MyConnection.getConnection();
		PreparedStatement ps;
		
		//i for insert
		if( operation == 'i' )
		{	
			try {
				ps= con.prepareStatement("INSERT INTO lblquanlyktx(Student_Code, first_and_last_name, Address, Phone Number, Paid) VALUES ('?','?','?','?','?')");
				ps.setString(1, mssv);
				ps.setString(2, hovaten);
				ps.setString(3, diachi);
				ps.setString(4, dienthoai);
				ps.setString(5, tien);
				
				if(ps.executeUpdate() >0) {
					
					JOptionPane.showMessageDialog(null, "new student added");
				}
				
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			
		}
		
	}
}

///////////////////////////////
JButton btnNewButton = new JButton("ADD");
		btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 16));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String mssv= txtmssv.getText();
				String hovaten= txthovaten.getText();
				String diachi= txtdiachi.getText();
				String dienthoai= txtdienthoai.getText();
				String tien= txtthanhtoan.getText();	
				 
				Student ku= new Student();
				ku.themSuaXoa('i', null, mssv, hovaten, diachi, dienthoai, tien);
			}
		});
		btnNewButton.setBounds(438, 373, 97, 25);
		contentPane.add(btnNewButton);

Còn đây là cái database

huhuuhuuhuhuh. cứuuuuuuuuuuuu

Query viết vậy máy sẽ hiểu là 5 dấu chấm hỏi, chứ không phải 5 tham số :smiley:

Docs. https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

2 Likes

ở trên truyền param đúng xuống dưới thêm ‘’ chi vậy :v

Em không thêm ‘’ vào thì nó hiện lỗi như này: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘Number, Paid) VALUES (‘18it335’,‘Luong Van Syt’,‘Da Nang’,‘0905020406’,‘600000’)’ at line 1

Còn thêm ‘’ vào thì nó hiện lỗi: Parameter index out of range (1 > number of parameters, which is 0).

Hiccccc!!!

Gần được rồi, giờ bạn đọc kĩ chỗ đó trong câu SQL một lần nữa.

1 Like

là sao anh . giúp em đi. nhiều lúc viết em cũng ko hiểu lắm đâu. vì em mới tập làm. Anh bảo em đọc kĩ, em cũng đọc kĩ, nhưng không biết làm sao để nó ko bị lỗi luôn… hic hic, em ngu quá . helppppp please

cứuuuuuuu emmmmmmmmmmmmmmmmm

thực sự rất cần sự trợ giúp đến từ các vị thương tâm, tình thương đến cho em. cả ngày mà đứng chỉ có nhiêu đó thôi

em thật là ngớ ngẫn. nó sai cú pháp. Trong câu lệnh Insert, “field list” trong database chưa đúng. Nên bị lỗi .Sau khi bình tâm nên Giờ thì mọi thứ oke. hihi

Thanh niên tự kỷ =]]]z

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?