Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Java Programming > Java Forum > ช่วยด้วยค่ะหาสาเหตุการ update ของmsql ไม่ได้ทั้งที่เขียนคำสั้งครบไม่แจ้ง error รันผ่านแต่ข้อมูลไม่ขึ้นใน sql แต่ insert ได้(java Eclipse)



 

ช่วยด้วยค่ะหาสาเหตุการ update ของmsql ไม่ได้ทั้งที่เขียนคำสั้งครบไม่แจ้ง error รันผ่านแต่ข้อมูลไม่ขึ้นใน sql แต่ insert ได้(java Eclipse)

 



Topic : 131388



โพสกระทู้ ( 1 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook



รัน insert ได้มีข้อมูลเข้า delete ได้ แต่ update รันผ่านแต่ไม่มีข้อมูลแสดงใน mysql ไม่มีการแจ้ง error เขียนคำสั่งโครงสร้างถูก แต่ทำไมข้อมูลไม่แก้ให้ในฐานข้อมูล งงค่ะ รบกวนท่านผู้รู้ช่วยหน่อยค่ะ จนปัญญาจริงๆ[/font]

Code (Java)
public class mainFrame extends JFrame{

	private JPanel contentPane;
	private JScrollPane scrollPane;
	private JTable table;
	private JTextField textName;
	private JTextField textAge;
	private JTextField textSearch;
	JButton btnOk,btnDel,btnUpdate;
	Connection conn = Connect.getConnection();
	private DefaultTableModel model;
	public static void main(String[] args) {
		mainFrame f = new mainFrame();
				f.setVisible(true);
	}
	
private void loadData(){
		try{ int totalRow = table.getRowCount()-1;
		while (totalRow> -1){
			model.removeRow(totalRow);//ลบข้อมูลใน model จากแถวในตาราง โดยใช้คำสั่ง removeRow จะลบจากท้ายตารางมาก่อน
			totalRow--;
		}
		String search = textSearch.getText().trim();//ให้ตัดช่องว่าทิ้ง ใช้คำสั่ง trim
		//3. sql & execute --> result set
		String sql = "SELECT * FROM tb_test1 "
			+"WHERE NAME LIKE'%"+search+"%'"
			+"OR AGE LIKE'%"+search+"%'";

		ResultSet rs = conn.createStatement().executeQuery(sql);
		//4. looping result set to model
		int row = 0;
		while(rs.next())// .next เลือนลำดับไปข้างหน้าจนกว่าจะครบแถว
		{
		model.addRow(new Object[0]);
		//(aValue(ค่าอะไร), totalRow(แถวไหน), column(คอลัมน์ที่เท่าไหร่))
		model.setValueAt(rs.getString("NAME"), row, 0);
		model.setValueAt(rs.getString("AGE"), row, 1);
			row++;
	}
		table.setModel(model);
		} catch (Exception e) {
			e.printStackTrace();
		}}
private void clearData() {
		textName.setText("");
		textAge.setText("");
	}
	public mainFrame() {
		addWindowListener(new WindowAdapter() {
			@SuppressWarnings("static-access")
			public void windowOpened(WindowEvent arg0) {
				conn = new Connect().getConnection();
				model = (DefaultTableModel)table.getModel();
			
				loadData();
				clearData();
			}	
			public void windowClosing(WindowEvent e) {//แจ้งเตือนกดปิดโปรแกรม
				 int confirmed = JOptionPane.showConfirmDialog(null, 
					        "คุณต้องการออกจากโปรแกรมใช่หรือไม่", "ปิดโปรแกรม",
					        JOptionPane.YES_NO_OPTION);

				 if(confirmed == JOptionPane.YES_OPTION) { dispose(); } else { setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); }	
			}
		});
		Container c = this.getContentPane();
		c.setLayout(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 512, 360);
		setLocationRelativeTo(null);//อยู่กึ่งกลางหน้าจอ
		setTitle("myfriend");
		setResizable(false);//ไม่ให้ขยาย
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		this.add(contentPane);
		
		
		scrollPane = new JScrollPane();
		scrollPane.setViewportBorder(null);
		scrollPane.setBounds(20, 192, 424, 118);
		this.add(scrollPane);
		
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
		public void mouseClicked(MouseEvent e) {
			int row = table.getSelectedRow();
			//textName.setEditable(false);//ไม่ให้กล่องข้อความโชว์หรือแก้ไข
			textName.setText(table.getValueAt(row, 0).toString());
			textAge.setText(table.getValueAt(row, 1).toString());
			}
		});
		table.setBackground(Color.LIGHT_GRAY);
		table.setToolTipText("");
		table.setFont(new Font("Tahoma", Font.BOLD, 12));
		table.setBorder(null);
		table.setForeground(Color.BLUE);//กำหนดสีอักษรในตาราง
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{ null,null},
				{ null,null},
			},
			new String[] {
				"name", "Age"
			}
		));
		table.getColumnModel().getColumn(0).setPreferredWidth(57);
		table.getColumnModel().getColumn(1).setPreferredWidth(87);
		//กำหนดสีและขนาดตัวอักษรชองหัวเรื่องในตาราง Header Font & Color 	
		table.getTableHeader().setFont(new Font("Tahoma", Font.BOLD, 12));
		table.getTableHeader().setForeground(Color.red);
		//กำหนดขนาดความกว้างของคอลัม Column Width
		table.getColumnModel().getColumn(1).setPreferredWidth(2);
		//กำหนดขนาดความสูงของแถวนในตาราง Row Height
		table.setRowHeight(20);
		scrollPane.setViewportView(table);
		// กำหนดให้ข้อความของคอลัมที่ (1)อยู่กลาง  Column Center
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment(JLabel.CENTER);
		table.getColumnModel().getColumn(1).setCellRenderer(centerRenderer);
		
		JLabel lblNewLabel = new JLabel("Nam");
		lblNewLabel.setBounds(21, 23, 89, 17);
		c.add(lblNewLabel);
		
		textName = new JTextField();
		textName.setBounds(138, 23, 181, 20);
		this.add(textName);
		textName.setColumns(10);
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			try { 
				String sql = ""
						+"INSERT INTO tb_test1("
						+"name, "
						+"age"
						+")VALUES(?,?)";
				PreparedStatement pre = conn.prepareStatement(sql);
				pre.setString(1, textName.getText().trim());
				pre.setString(2, textAge.getText().trim());
			if(pre.executeUpdate()!= -1)
				{
					JOptionPane.showMessageDialog(null, "Data Insert"); 
								
					loadData();
					clearData();
					textName.setEditable(true);
					pre.close();
					}
				} catch(SQLException ex)
					{
						ex.printStackTrace();
					}
				}
			});
		btnOk.setBounds(21, 98, 89, 23);
		c.add(btnOk);
		
		JLabel lblNewLabel_1 = new JLabel("Age");
		lblNewLabel_1.setBounds(21, 51, 89, 23);
		this.add(lblNewLabel_1);
		
		textAge = new JTextField();
		textAge.setBounds(138, 54, 181, 20);
		this.add(textAge);
		textAge.setColumns(10);
		
		JButton btnUpdate = new JButton("Update");
		btnUpdate.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {	
			try{
				String sql = "UPDATE tb_test1 SET "
						+"AGE=?"
						+"WHERE name=?";
				PreparedStatement pre = conn.prepareStatement(sql);
				pre.setString(1, textName.getText().trim());
				pre.setString(2, textAge.getText().trim());
				pre.executeUpdate();
				
				JOptionPane.showMessageDialog(null, "Data Update"); 
									
				loadData();
				clearData();
				textName.setEditable(true);
				pre.close();
		} catch(SQLException ex)
				{
					ex.printStackTrace();
					}
				}
			});
		btnUpdate.setBounds(126, 98, 89, 23);
		this.add(btnUpdate);
		
		JButton btnDel = new JButton("Delete");
		btnDel.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			int action = JOptionPane.showConfirmDialog(null, "คุณต้องการลบใช่ไหม?", "ผลการแจ้ง", JOptionPane.YES_NO_OPTION);
			if(action==0);
			try{
				String sql = "DELETE FROM tb_test1 WHERE NAME=?";
				PreparedStatement pre = conn.prepareStatement(sql);
				pre.setString(1, textName.getText().trim());

			if(pre.executeUpdate()!= -1)
				{
					JOptionPane.showMessageDialog(null, "Data Delete"); 
								
					loadData();
					clearData();
					textName.setEditable(true);
					pre.close();
				}
			} catch(SQLException ex)
				{
					ex.printStackTrace();
				}
			}
		});
		btnDel.setBounds(245, 98, 89, 23);
		this.add(btnDel);
		
		JLabel lblNewLabel_2 = new JLabel("ค้นหา");
		lblNewLabel_2.setBounds(44, 155, 79, 26);
		this.add(lblNewLabel_2);
		
		textSearch = new JTextField();
		textSearch.addKeyListener(new KeyAdapter(){
		public void keyReleased(KeyEvent e) {
			loadData();
			clearData();
			}
		});
		textSearch.setBounds(113, 161, 138, 20);
		this.add(textSearch);
		textSearch.setColumns(10);
		
		JButton btClear= new JButton ("Clear");
		btClear.setBackground(Color.green);
		btClear.setBounds(346, 98, 79,23);
		btClear.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			textName.setText("");
			textAge.setText("");
			textSearch.setText("");
			loadData();
			}
		});
		c.add(btClear);
		JButton btExit= new JButton ("ออกจากโปรแกรม");
		btExit.setBackground(Color.cyan);
		btExit.setFont(new Font("Tahoma",Font.PLAIN ,12));
		btExit.setForeground(Color.red);
		btExit.setBounds(280,155, 150,26);
		btExit.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			//System.exit(0);//หรือจะออกจากโปรแกรมโดยมีข้อความแจ้งเตือน
			//ออกจากโปรแกรมโดยใช้ปุ่มมีข้อความแจ้ง
			int selectedButton=JOptionPane.showConfirmDialog(null,
				"ต้องการออกจากโปรแกรม ใช่หรือไม่ ?", "EXIT PROGRAM", JOptionPane.YES_NO_OPTION);
			if ( selectedButton == JOptionPane.YES_OPTION )System.exit(DISPOSE_ON_CLOSE);
			}
		});
		c.add(btExit);
	}
}


udate not run sql

udate 2

text Mysql
===Database exsam

== Table structure for table tb_test1

|------
|Column|Type|Null|Default
|------
|name|varchar(50)|No|
|age|int(3)|No|
== Dumping data for table tb_test1

|khumme|12
|samit|69
|pop|17
|สมชาย|58



Tag : Java, MySQL, JAVA, Appserv, Windows









ประวัติการแก้ไข
2018-06-29 13:20:20
2018-06-29 13:20:44
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2018-06-29 13:14:34 By : platoona View : 1067 Reply : 1
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Debug ดูค่า SQL Statement ครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2018-06-29 14:33:16 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ช่วยด้วยค่ะหาสาเหตุการ update ของmsql ไม่ได้ทั้งที่เขียนคำสั้งครบไม่แจ้ง error รันผ่านแต่ข้อมูลไม่ขึ้นใน sql แต่ insert ได้(java Eclipse)
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่