0

下面是将值插入到一个表中的示例(MemberRecord)

//CreateUser method

        public boolean createUser(){
            boolean success = false;
            DBController db = new DBController();
            db.getConnection();
            String sql = "INSERT INTO MemberRecord(id, name, email, contactNo, password, gender, nationality, dateOfBirth, address, postalCode, secQues, secAns, userType)";
            sql += "VALUES('" + id + "','" + name + "','" + email + "','" + cNo + "','" + pwd + "','" +gen + "','" + nationality + "','" + dob + "','" + address + "','" + pCode + "','" + secQuestion +"','"+secAnswer +userType + "')";


            if (db.updateRequest(sql)==1) success = true;
            db.terminate();
            return success;

        }

如何将值插入到 2 个表中?当我尝试下面的代码时,我得到了重复的变量。即使我更改变量名称,它仍然会给我错误。

//CreateUser method
        public boolean createUser(){
            boolean success = false;
            DBController db = new DBController();
            db.getConnection();
            String sql = "INSERT INTO MemberRecord(id, name, email, contactNo, password, gender, nationality, dateOfBirth, address, postalCode, secQues, secAns, userType)";
            sql += "VALUES('" + id + "','" + name + "','" + email + "','" + cNo + "','" + pwd + "','" +gen + "','" + nationality + "','" + dob + "','" + address + "','" + pCode + "','" + secQuestion +"','"+secAnswer +userType + "')";

            String sql = "INSERT INTO PaymentDetails(creditCardNo,creditCardType,expiryDate,CVV)";
            sql += "VALUES('" + cCardNo + "','" + cCardType + "','" + expiryDate + "','" + cvv + "')";

            if (db.updateRequest(sql)==1) success = true;
            db.terminate();
            return success;

        }
4

2 回答 2

1

如果出生日期和到期日期的数据类型是日期,则以下代码可能会有所帮助

TO_DATE('"+ str+"','yyyy/mm/dd')

修改后的代码如下

String sql = "INSERT INTO MemberRecord(id, name, email, contactNo, password, gender, nationality, dateOfBirth, address, postalCode, secQues, secAns, userType)";
sql += "VALUES('" + id + "','" + name + "','" + email + "','" + cNo + "','" + pwd + "','" +gen + "','" + nationality + "',TO_DATE('" + dob + "','yyyy/mm/dd'),'" + address + "','" + pCode + "','" + secQuestion +"','"+secAnswer +userType + "');";

sql += "INSERT INTO PaymentDetails(creditCardNo,creditCardType,expiryDate,CVV)";
sql += "VALUES('" + cCardNo + "','" + cCardType + "',TO_DATE('" + expiryDate + "','yyyy/mm/dd'),'" + cvv + "')";
于 2015-05-01T06:20:54.580 回答
1

尝试这个:

String sql = "INSERT INTO MemberRecord(id, name, email, contactNo, password, gender, nationality, dateOfBirth, address, postalCode, secQues, secAns, userType)";
sql += "VALUES('" + id + "','" + name + "','" + email + "','" + cNo + "','" + pwd + "','" +gen + "','" + nationality + "','" + dob + "','" + address + "','" + pCode + "','" + secQuestion +"','"+secAnswer +userType + "');";

sql += "INSERT INTO PaymentDetails(creditCardNo,creditCardType,expiryDate,CVV)";
sql += "VALUES('" + cCardNo + "','" + cCardType + "','" + expiryDate + "','" + cvv + "')";

;在第一个 sql 语句的末尾添加了一个。

于 2013-06-20T03:59:49.337 回答