如何将文本字段数据或值保存到Mysql数据库? [英] How Can I Save A Textfield Data Or Values Into Mysql Database?

查看:214
本文介绍了如何将文本字段数据或值保存到Mysql数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

我的代码出了什么问题?请帮助

Here's my code
What's wrong with my code? Please help

public void actionPerformed(ActionEvent e){	
	if(e.getSource().equals(save))	{          //the save button
	
	try {
	    // connection string			
	        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/PizzaOrders");
	        Statement st = con.createStatement();

	        st.executeUpdate("insert into Register VALUES('"
	                + CustomerID.getText() + "','" + fname.getText() + "','"
	                + lname.getText() + "','" + telnum.getText()
	                + "','" + city.getText() + "','" + zcode.getText()
	                + "','" + status.getText() + "','" + creditcard.getText()
	                + "','" + orderdate.getText() + "','" + deliveryfee.getText()
	                + "'," + "'" + quantity.getText() + "','"
	                + itemcost.getText() + "','" + TotalCost.getText() +"')");

	        JOptionPane.showConfirmDialog(null, "Orders saved!",
	                "Result", JOptionPane.DEFAULT_OPTION,
	                JOptionPane.PLAIN_MESSAGE);

	        st.close();
	        con.close(); 
	}catch(SQLException err){
		err.printStackTrace();
	}
}

推荐答案

!!!!不要使用连接来形成SQL语句! !

!!!!!特别是用户输入的文本值!!!!!



了解如何使用参数化在你的框架中查询。
!!!!DO NOT USE CONCATENATION TO FORM AN SQL STATEMENT!!!!
!!!!!ESPECIALLY WITH TEXT VALUES ENTERED BY THE USER!!!!!

Find out how to use a parameterized query in your framework.


这篇关于如何将文本字段数据或值保存到Mysql数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆