2015-12-23 54 views
0

數據插入到MySQL數據庫我想要做簡單的註冊表格,但我不能往前走,我已經嘗試了一切,它仍然無法正常工作,這是我的代碼:我堅持我無法從JDBC

protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException 
    { 
     StringBuilder builder= new StringBuilder(); 
     Reader reader=request.getReader(); 
     int c; 
     while ((c=reader.read())!=-1) 
     { 
      builder.append((char) c); 
     } 
     String signup=builder.toString(); 
     try { 
      JSONObject object= new JSONObject(signup); 
      String name=object.getString("username"); 
      String email=object.getString("email"); 
      String pass=object.getString("pass"); 
      String sql="INSERT INTO user_f VALUES("+email+","+name+","+pass+")"+";"; 

      statement.executeUpdate(sql); 
      //ResultSet set= statement.executeQuery("SELECT ") 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 

我的錯誤堆棧我編輯的代碼,所以很多時候,我有點感到沮喪這是我收到的錯誤:

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 '@gmail.com,dsf,sdfsd)' at line 1 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422) 
    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.execSQL(ConnectionImpl.java:2547) 
    at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1541) 
    at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2605) 
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1469) 
    at com.herda.app.Servlet.doPost(Servlet.java:56) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:745) 
+0

你需要在值附近加引號,或者使用PreparedStatement – sidgate

+0

感謝隊友,現在正在工作 –

回答

2

你的MySQL查詢沒有一個有效的語法錯誤是說。你需要引用字符串文字。嘗試這樣的:

String sql = "INSERT INTO user_f VALUES(\"" + email + "\", \"" + name + "\", \"" + pass + "\");"; 

但是,請注意,此代碼對SQL注入是開放的!要解決此安全問題,您可以使用預準備語句。這也解決了你的報價問題。

+0

謝謝大家,它的作用就像魅力。只是一個問題,斜槓代表什麼? –

+0

@johndoe,\字符在您的字符串內轉義引號。如果您不添加它們,它們將終止您的Java字符串,而不是在查詢中添加爲字符。 –

+0

好的人非常感謝你 –

1

你應該測試你的sql查詢拳頭。執行它在MySQL工作臺

我覺得應該是

String sql="INSERT INTO user_f VALUES('"+email+"','"+name+"','"+pass+"')"+";"; 
+0

也可以。我很感謝你們的幫助 –

+0

你能否將我的答案標記爲答案。你和我需要50聲望做出評論:( –

+0

,我會投你的提問:( –

1

問題是你缺少圍繞你的價值觀的報價,但是你應該從未通過到查詢字符串串聯值構建這樣的查詢。它打開你的代碼到SQL注入(例如,當你插入用戶輸入時),這是一個非常大的安全風險。相反,您應該使用帶有參數佔位符的準備語句。這將處理轉義值,所以SQL注入不會成爲問題。

在某些數據庫系統上準備好的語句也具有提高性能的附加值,因爲語句可以被重用。

舉個例子:

try (PreparedStatement pstmt = connection.prepareStatement(
      "INSERT INTO user_f(email,name,pass) VALUES(?,?,?)")) { 
    pstmt.setString(1, email); 
    pstmt.setString(2, name); 
    pstmt.setString(3, pass); 
    pstmt.executeUpdate(); 
} 

在這個答案我也明確列出要插入的列,這使得你的代碼更長遠的保障(例如,當您添加或刪除列)。

+0

這是精彩,並感謝@Mark –