& 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?