Help! I'm working on a class project and I have to insert information into a database using a Java Servlet...yes, it HAS to be a Java Servlet. I've got the code almost correct, but I'm getting illegal start of expression errors when I try to compile it.
Here is the code:
//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{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// JDBC driver name and database URL
static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
static final String DB_URL="jdbc:mysql://localhost/dvdsite";
// Database credentials
static final String USER = "user";
static final String PASS = "";
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
}
}
Can someone please help? I have been struggling with this for days!