2013-10-20 31 views
0

我在嘗試編寫servlet時遇到小的編譯錯誤,並且已檢查了所有{}和;嘗試編寫Servlet以將HTML表單數據插入到數據庫中時出現編譯錯誤

下面是代碼:

//This servlet processes the user's registration and redirects them to the catalog. 

// Load required libraries 
import java.io.*; 
import java.util.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.sql.*; 

public class DatabaseAccess extends HttpServlet{ 

// JDBC driver name and database URL 
private static final String JDBC_DRIVER="com.mysql.jdbc.Driver"; 
private static final String DB_URL="jdbc:mysql://localhost/dvdsite"; 

public void doGet(HttpServletRequest request, 
        HttpServletResponse response) 
      throws ServletException, IOException 

     // Database credentials 
     static final String USER = "user"; 
     static final String PASS = ""; 

     // Get form values from register page 
     String username = request.getParameter("username"); 
     int userID = Integer.parseInt(username); 
     String password = request.getParameter("password"); 
     String email = request.getParameter("email"); 
{ 
     try { 
     // Register JDBC driver 
     Class.forName("com.mysql.jdbc.Driver"); 

     // Open a connection 
     conn = DriverManager.getConnection(DB_URL,USER,PASS); 

     // Execute SQL query 
     stmt = conn.createStatement(); 
     String sql; 
     sql = "INSERT INTO dvdsite values (username, password, email)"; 
     ResultSet rs = stmt.executeQuery(sql); 

     // Clean-up environment 
     rs.close(); 
     stmt.close(); 
     conn.close(); 
     }catch(SQLException se){ 
     //Handle errors for JDBC 
     se.printStackTrace(); 
     }catch(Exception e){ 
     //Handle errors for Class.forName 
     e.printStackTrace(); 
     }finally{ 
     //finally block used to close resources 
     try{ 
      if(stmt!=null) 
       stmt.close(); 
     }catch(SQLException se2){ 
     }// nothing we can do 
     try{ 
      if(conn!=null) 
      conn.close(); 
     }catch(SQLException se){ 
      se.printStackTrace(); 
     }//end finally try 
     } //end try 
    } 
} 

這是我得到的錯誤:1個發現錯誤: 文件:/Applications/MAMP/htdocs/register.java [行:18] 錯誤: /Applications/MAMP/htdocs/register.java:18:';'預計

的錯誤,在這條線出現:

拋出了ServletException,IOException異常

誰能幫助?

+0

方法體必須有大括號。 – SLaks

+0

你可以發佈'register.java'類嗎? – herry

+1

堅持一個{在IOException後 – tom

回答

0

嘗試使用固定語法錯誤的此版本。並調用文件DatabaseAccess.java,因爲文件需要像他們的類一樣命名。

import java.io.IOException; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class DatabaseAccess extends HttpServlet { 

    // JDBC driver name and database URL 
    private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
    private static final String DB_URL = "jdbc:mysql://localhost/dvdsite"; 

    // Database credentials 
    static final String USER = "user"; 
    static final String PASS = ""; 

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // Get form values from register page 
     String username = request.getParameter("username"); 
     int userID = Integer.parseInt(username); 
     String password = request.getParameter("password"); 
     String email = request.getParameter("email"); 

     Connection conn; 
     Statement stmt; 

     try { 
      // Register JDBC driver 
      Class.forName("com.mysql.jdbc.Driver"); 

      // Open a connection 
      conn = DriverManager.getConnection(DB_URL, USER, PASS); 

      // Execute SQL query 
      stmt = conn.createStatement(); 
      String sql; 
      sql = "INSERT INTO dvdsite values (username, password, email)"; 
      ResultSet rs = stmt.executeQuery(sql); 

      // Clean-up environment 
      rs.close(); 
      stmt.close(); 
      conn.close(); 
     } catch (SQLException se) { 
      // Handle errors for JDBC 
      se.printStackTrace(); 
     } catch (Exception e) { 
      // Handle errors for Class.forName 
      e.printStackTrace(); 
     } finally { 
      // finally block used to close resources 
      try { 
       if (stmt != null) 
        stmt.close(); 
      } catch (SQLException se2) { 
      }// nothing we can do 
      try { 
       if (conn != null) 
        conn.close(); 
      } catch (SQLException se) { 
       se.printStackTrace(); 
      }// end finally try 
     } // end try 
    } 
} 
+0

謝謝,但這只是給了我更多的錯誤,這是如此令人沮喪! –

+0

@TonySmith再試一次,我現在完全修復它,兩個變量聲明仍然失蹤。 – noone

相關問題