JDBC Java Database INSERT statement

Source: Internet
Author: User

Https://zhidao.baidu.com/question/540539955.html?fr=iks&word=jdbc+sql%D3%EF%BE%E4&ie=gbk

JDBC Java Database INSERT statement error
Your sister's, wrote the placeholder you don't have to fill in, SQL of course, the report of the SQL is incomplete
Two scenarios
A. Four question marks after valuses write the parameters directly
B. Using import java.sql.PreparedStatement; PreparedStatement method under Package
A pre-programmed way to populate an example
PreparedStatement PS = null;

sql = "INSERT into record (cid,bid,mid,borrowdate,returndate) VALUES (?,?,?,?,?)";
Ps.setint (1, first value);
Ps.setint (2, second value);
Ps.setint (3, third value);
Ps.setint (4, fourth value);
Submit 1234 must write setint are examples of what types of fields you see yourself
Ps.executeupdate ();

Example:

String sql= "" +
"INSERT INTO Shopping" +
"(User_name,sex,age,birthday,email,mobile," + (note the usage of the "+" sign, I missed a plus sign, resulting in an error syntax error on token VALUES ("", delete this token, syntax error)
"Create_user,create_date,update_user,update_date,isdel)" +
"VALUES (" +
"?,?,?,?,?,?,?, current_date (),?, current_fste (),?)";

Or

String sql= "INSERT into shopping (User_name,sex,age,birthday,email,mobile,create_user,create_date,update_user, Update_date,isdel) VALUES (?,?,?,?,?,?,?, current_date (),?, current_fste (),?) ";

The following SQL statement is populated with parameters:

PreparedStatement ptmt=conn.preparestatement (SQL); Compiling SQL statements
Ptmt.setstring (1, G.getuser_name ()); Passing parameters in
Ptmt.setint (2,g.getsex ());
Ptmt.setint (3, G.getage ());
Ptmt.setdate (4, (Date) g.getbirthday ());
Ptmt.setstring (5,g.getemail ());
Ptmt.setstring (6, G.getmobile ());
Ptmt.setstring (7, G.getcreate_user ());
Ptmt.setstring (8, G.getupdate_user ());
Ptmt.setint (9,g.getisdel ());
Ptmt.execute ();

JDBC Java Database INSERT statement

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.