Home > Back-end >  Small white use preparestatement parameter is an error, a great god answers
Small white use preparestatement parameter is an error, a great god answers

Time:09-16

With preparestatement transfer is wrong, use the statement is no problem, the code is as follows:

 
Package com. JDBC. PreparedStatment;

Import the Java. SQL. Connection;
Import the Java. SQL. The ResultSet;
import org.junit.Test;

The import com. JDBC. Util. JdbcUtil;
The import com. Mysql. JDBC. PreparedStatement;

Public class app {
/*
* 1. Use preparestatement prevent SQL injection cases
*
*/
@ Test
Public void testLogin1 () {

The Connection Connection=null;
PreparedStatement statement=null;
The ResultSet ResultSet=null;
String name="detective";
Try {
//for a link
The connection=jdbcUtil. Getconnection ();

//to the SQL statement
String SQL="INSERT INTO student (name, age) VALUES (?,?,?,?,? ,?) ";
//String SQL="use jdbc_enhance";
//create the STMT
The statement=(PreparedStatement) connection. PrepareStatement (SQL);

//set the parameter value
Statement. SetInt (1, "detective");
The statement. SetInt (2, 18);

//send SQL
Int count=statement. ExecuteUpdate (SQL);

//output
System. The out. Println (" were affected "+ count +" line ");

//query data in that table
//to SQL
String sql1="select * from student";

//send and executing SQL statements
The resultSet=statement. ExecuteQuery (sql1);

//output returns set
While (resultSet. Next ()) {
String id=the resultSet. Get String (" id ");
String name1=the resultSet. Get String (" name ");
String age=the resultSet. Get String (" age ");

System. The out. Println (id + "-" + name1 + "-" + age);
}
} the catch (Exception e) {
e.printStackTrace();
Throw new RuntimeException ();
} the finally {

JdbcUtil. Close (connection, statement, resultSet);
}

}
}



An error as below
 
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 '?) 'the at line 1
At sun. Reflect. NativeConstructorAccessorImpl. NewInstance0 (Native Method)
At sun. Reflect. NativeConstructorAccessorImpl. NewInstance (NativeConstructorAccessorImpl. Java: 39)
At sun. Reflect. DelegatingConstructorAccessorImpl. NewInstance (DelegatingConstructorAccessorImpl. Java: 27)
The at Java. Lang. Reflect. Constructor. NewInstance (513) Constructor. Java:
At com. Mysql. JDBC. Util. HandleNewInstance (Util. Java: 409)
At com. Mysql. JDBC. Util. GetInstance (Util. Java: 384)
At com. Mysql. JDBC. SQLError. CreateSQLException (SQLError. Java: 1054)
At com. Mysql. JDBC. MysqlIO. CheckErrorPacket (MysqlIO. Java: 3562)
At com. Mysql. JDBC. MysqlIO. CheckErrorPacket (MysqlIO. Java: 3494)
At com. Mysql. JDBC. MysqlIO. SendCommand (MysqlIO. Java: 1960)
At com. Mysql. JDBC. MysqlIO. SqlQueryDirect (MysqlIO. Java: 2114)
At com. Mysql. JDBC. ConnectionImpl. ExecSQL (ConnectionImpl. Java: 2690)
At com. Mysql. JDBC. StatementImpl. ExecuteUpdate (StatementImpl. Java: 1648)
At com. Mysql. JDBC. StatementImpl. ExecuteUpdate (StatementImpl. Java: 1567)
At com. JDBC. PreparedStatment. App. TestLogin1 (app. Java: 93)
At sun. Reflect. NativeMethodAccessorImpl. Invoke0 (Native Method)
At sun. Reflect. NativeMethodAccessorImpl. Invoke (NativeMethodAccessorImpl. Java: 39)
At sun. Reflect. DelegatingMethodAccessorImpl. Invoke (DelegatingMethodAccessorImpl. Java: 25)
The at Java. Lang. Reflect. Method. Invoke (597) Method. The Java:
The at org. Junit. Runners. Model. FrameworkMethod $1. RunReflectiveCall (FrameworkMethod. Java: 50)
The at org. Junit. Internal. Runners. Model. ReflectiveCallable. Run (ReflectiveCallable. Java: 12)
The at org. Junit. Runners. Model. FrameworkMethod. InvokeExplosively (FrameworkMethod. Java: 47)
The at org. Junit. Internal. Runners. Statements. InvokeMethod. Evaluate (InvokeMethod. Java: 17)
The at org. Junit. Runners. ParentRunner. RunLeaf (ParentRunner. Java: 325)
The at org. Junit. Runners. BlockJUnit4ClassRunner. RunChild (BlockJUnit4ClassRunner. Java: 78)
The at org. Junit. Runners. BlockJUnit4ClassRunner. RunChild (BlockJUnit4ClassRunner. Java: 57)
The at org. Junit. Runners. ParentRunner $3. The run (ParentRunner. Java: 290)
The at org. Junit. Runners. ParentRunner $1. The schedule (ParentRunner. Java: 71)
The at org. Junit. Runners. ParentRunner. RunChildren (ParentRunner. Java: 288)
The at org. Junit. Runners. ParentRunner. Access the $000 (ParentRunner. Java: 58)
At org. Junit. Runners. ParentRunner $2. The evaluate (ParentRunner. Java: 268)
The at org. Junit. Runners. ParentRunner. Run (ParentRunner. Java: 363)
At org. Eclipse JDT. Internal. Takeup. Runner. JUnit4TestReference. Run (JUnit4TestReference. Java: 50)
At org. Eclipse JDT. Internal. Junit. Runner. TestExecution. Run (TestExecution. Java: 38)
At org. Eclipse JDT. Internal. Junit. Runner. RemoteTestRunner. RunTests (RemoteTestRunner. Java: 467)
At org. Eclipse JDT. Internal. Junit. Runner. RemoteTestRunner. RunTests (RemoteTestRunner. Java: 683)
At org. Eclipse JDT. Internal. Junit. Runner. RemoteTestRunner. Run (390) RemoteTestRunner. Java:
At org. Eclipse JDT. Internal. Junit. Runner. RemoteTestRunner. Main (197) RemoteTestRunner. Java:

CodePudding user response:

nullnullnullnullnullnull
  • Related