有人能帮我解决现在的问题吗?
我有一组问题(调查)。当用户完成调查时,它将插入到我的数据库中。
然而,现在,当我提交调查时,它并没有将结果插入我的数据库。
这是我的代码:
try {
//Process - Query from SQL
String strSqlnsert = "INSERT INTO Customers (MembershipID, Contact, Email, Address, SurveyStatus, Subscription) VALUES"
+ " ('"+ member_ID + "', " + contact_num + ", '" + email_add + "', '" + mail_add + "' , " + radio_group + "," + receive_info + ")";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/customers", "root", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSqlnsert);
我还在库文件夹中添加了JDBC的库。
非数字值似乎没有引用
代替
" + email_add + ",
尝试
'" + email_add + "',
在下面插入声明
INSERT INTO table (field1, field2) VALUES ( 5, 'this is a string');
"this is a string"用引号括起来,因为field2
的类型是VARCHAR。field1
是INT,所以分配给它的值(5)不需要引用。
VALUES
列表也必须用大括号括起来,并且必须引用非数值:
String strSqlnsert = "INSERT INTO Customers (MembershipID, Contact, Email, Address, SurveyStatus, Subscription) VALUES (" + member_ID + ", " + contact_num + ", '" + email_add + "', '" + mail_add + "', '" + radio_group + "', '" + receive_info + "')";
尝试制作一个prepared语句,在其中可以调用preparedStatement类的setInt或setString方法。因此它将自动消除字符串或数字混淆。
BTW preparedStatement是sql查询中传递参数的最佳方式。参数sql查询不要使用Statement类。