java - Why does PreparedStatement (JDBC) result in a MySQLSyntaxErrorException in MySQL 5.7.9? -


this question has answer here:

code:

package com.qapro.register;  import java.io.ioexception; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import java.sql.timestamp;  import java.util.arraylist; import java.util.date;  import javax.servlet.requestdispatcher; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpsession; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse;  @webservlet("/registermember") public class registermember extends httpservlet {     private static final long serialversionuid = 1l;      public registermember() {         super();         // todo auto-generated constructor stub     }      protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {     }      protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {          string username = request.getparameter("username");         string name = request.getparameter("name");         string surname = request.getparameter("surname");         string email = request.getparameter("email");         string password = request.getparameter("password");          connection connection = null;         preparedstatement preparedstatement = null;          try {             class.forname("com.mysql.jdbc.driver");              string connectionurl = "jdbc:mysql://localhost:3306/qapro?user=root&password=root";             connection = drivermanager.getconnection(connectionurl);              string userinsertsql = "insert qapro.user (user_name, `password`, is_active, name, surname, email, insert_date) values (?, ?, ?, ?, ?, ?, ?);";             preparedstatement = connection.preparestatement(userinsertsql);              preparedstatement.setstring(1, username);             preparedstatement.setstring(2, password);             preparedstatement.setboolean(3, true);             preparedstatement.setstring(4, name);                        preparedstatement.setstring(5, surname);             preparedstatement.setstring(6, email);             preparedstatement.settimestamp(7, new timestamp(new date().gettime()));              int affectedrowcount = preparedstatement.executeupdate(userinsertsql);             if(affectedrowcount > 0)                 system.out.println("user has been inserted succesfully");             else                 system.out.println("error occurred while inserting user");          } catch (classnotfoundexception e) {             e.printstacktrace();         } catch (sqlexception e) {             e.printstacktrace();         }          finally{             try {                 preparedstatement.close();                 connection.close();             } catch (sqlexception e) {                 e.printstacktrace();             }         }                }  } 

<%@ page language="java" contenttype="text/html; charset=utf-8"      pageencoding="utf-8"%>  <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">    <html>  	<head>  		<meta http-equiv="content-type" content="text/html; charset=utf-8">  		<title>title</title>  	</head>    	<body>  	  		<form action="registermember" method="post">  			<fieldset>  				<legend>log in!</legend>    					username: <input type="text" name="username"></input>  					<br></br>  					name: <input type="text" name="name"></input>  					<br></br>  					surname: <input type="text" name="surname"></input>  					<br></br>  					email: <input type="email" name="email"></input>  					<br></br>  					password: <input type="password" name="password"></input>  					<br></br>  		  					<input type="submit" value="submit"></input>  					  			</fieldset>  		</form>  	  	</body>    </html>

com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: have error in sql syntax; check manual corresponds mysql server version right syntax use near '?, ?, ?)' @ line 1

my database

why error? , eventhough error occurs database updating.

you using

int affectedrowcount = preparedstatement.executeupdate(userinsertsql); 

to try , execute query, incorrect. have used userinsertsql in .preparestatement(userinsertsql) call, need is

int affectedrowcount = preparedstatement.executeupdate(); 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -