Java statements for database operations are prone to errors. java statements are prone to errors.

Source: Internet
Author: User

Java statements for database operations are prone to errors. java statements are prone to errors.

Let's first look at the following statement

String sqlString = "select * from petMessage where petName = '" + pet. getPetName () + "';";

The preceding statement Selects all the information from the specified information. getPetName () respectively added a pair of "" to the left and right of the pair to connect to the string function, but carefully looked at the left and right of the pair, there is a symbol 'is the single quotation mark, the single quotation mark is used to enclose pet. the getPetName () Information keeps the SQL statement syntax accurate!


An error occurred while operating the database in java.

Your SQL statement is incorrect. A comma is added at the end.
SQL can be output in the console to check whether it is correct

Java SQL statement for saving data to the database

I feel that the problem is not very clear. Is a form containing only a group of name id sex information? It is also a table like excel, or csv format, which contains name id sex information of n people, each group is a row. Is there any information in the form not associated with the information in the database? It is purely the nth record in the database corresponding to the nth form or table. However, in any case, only one entry can be read from the form and inserted one by one. However, you can use the database's "batch Update" method and PreparedStatement to slightly improve the efficiency, for example,

String s = "insert into T2 (B, C, name, id, sex) values (?, ?, ?, ?, ?) "
PreparedStatement pst = conn. prepareStatement (s );
Rs_T = executeQuery ("select B, C from T ");
While (rs_T.next ()){
Pst. setString (1, rs_T.getString ("B "));
Pst. setString (2, rs_T.getString ("C "));
Pst. setString (3, name); // The name, id, and sex values are obtained in the form.
Pst. setString (4, id );
Pst. setString (5, sex );
Pst. addBatch ();
}
Pst.exe cuteBatch ();

However, this may not be what the landlord wants.

Related Article

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.