Questions to note about writing SQL statements in Java code

Source: Internet
Author: User

Recently, you need to manually write SQL statements directly into Java code and write SQL statements, and there are a few minor issues to be aware of.

First of all I wrote a few simple SQL statements, self-thought no problem, but the compilation of direct error.

1 String str = "INSERT into XXX (a,b,c) VALUES ('" A.geta () "', '" A.getb () "', '" a.getc () "');";

The study of a half-day discovery should be a connection string problem, the first modification after the assignment field before and after the "+" sign to complete the SQL statement. After correcting the code as follows

1 String str = "INSERT into XXX (a,b,c) VALUES ('" +a.geta () + "', '" +A.GETB () + "', '" +a.getc () + "');";

The original assignment to the field in the database needs to be ' "+ + "' The way to finish. OK, after compiling successfully, will run the result value of STR into SQL database test, no problem, think everything OK, results run again error. This self-obsessed, repeated testing, in the database with SQL statements to compare, no problem ah, now my last successful code put up, we see if there is any difference.

1 String str = "INSERT into XXX (a,b,c) VALUES ('" +a.geta () + "', '" +A.GETB () + "', '" +a.getc () + "')";

Yes, it is the last semicolon, the original in the Java statement can not be added to ordinary SQL statements, although there is no error in the database, but in Java must pay attention to this small problem.

Questions to note about writing SQL statements in Java code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.