Why cannot I insert a placeholder insert statement into mysql-JDBC?

Source: Internet
Author: User
& Quot ;...... Condition (Stringname, Stringpassword) throwsException {Connectionconnnull; condition; ResultSetrsnull; connDbUtils. getConn (); Stringsql & quot; insertintoUser (name, password) values (?,?) & Quot; st mysqljdbc

...... Public static void insert (String name, String password) throws Exception {Connection conn = null; PreparedStatement st = null; ResultSet rs = null; conn = DbUtils. getConn (); String SQL = "insert into User (name, password) values (?,?) "; St = conn. prepareStatement (SQL); st. setString (1, name); st. setString (2, password); int num = st.exe cuteUpdate (SQL); if (num> 0) {System. out. println ("insert successful !! ");} DbUtils. release (conn, st, rs);} public static void main (String args []) throws Exception {...... IUserDao. insert ("hello", "89790 ");...... }}

+ ---------- + -------------- + ------ + ----- + --------- + ------- +
| Field | Type | Null | Key | Default | Extra |
+ ---------- + -------------- + ------ + ----- + --------- + ------- +
| UserID | int (11) | YES | NULL |
| Name | varchar (255) | YES | NULL |
| Sex | varchar (255) | YES | NULL |
| Birthday | date | YES | NULL |
| Password | varchar (255) | YES | NULL |
+ ---------- + -------------- + ------ + ----- + --------- + ------- +
I keep reporting errors, but I don't know why. Tutorial orz
Exception in thread "main" com. mysql. jdbc. exceptions. jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?) 'At line 1
At sun. reflect. NativeConstructorAccessorImpl. newInstance0 (Native Method)
At sun. reflect. nativeconstruct%cessorimpl. newInstance (nativeconstruct%cessorimpl. java: 57)
At sun. reflect. delegatingconstruct%cessorimpl. newInstance (delegatingconstruct%cessorimpl. java: 45)
At java. lang. reflect. Constructor. newInstance (Constructor. java: 526)
At com. mysql. jdbc. Util. handleNewInstance (Util. java: 404)
At com. mysql. jdbc. Util. getInstance (Util. java: 387)
At com. mysql. jdbc. SQLError. createSQLException (SQLError. java: 939)
At com. mysql. jdbc. MysqlIO. checkErrorPacket (MysqlIO. java: 3878)
At com. mysql. jdbc. MysqlIO. checkErrorPacket (MysqlIO. java: 3814)
At com. mysql. jdbc. MysqlIO. sendCommand (MysqlIO. java: 2478)
At com. mysql. jdbc. MysqlIO. sqlQueryDirect (MysqlIO. java: 2625)
At com.mysql.jdbc.ConnectionImpl.exe cSQL (ConnectionImpl. java: 2547)
At com.mysql.jdbc.StatementImpl.exe cuteUpdateInternal (StatementImpl. java: 1541)
At com.mysql.jdbc.StatementImpl.exe cuteLargeUpdate (StatementImpl. java: 2605)
At com.mysql.jdbc.StatementImpl.exe cuteUpdate (StatementImpl. java: 1469)
At dao. IUserDao. insert (IUserDao. java: 107)
At dao. IUserDao. main (IUserDao. java: 120)

The table name is correct. the id is not auto-incrementing and there is no primarykey. You can insert SQL statements to death, and use "?" No. why?

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.