Tony Smith
Tony Smith

Reputation: 73

How to Insert Information into a Database Using a Java Servlet

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!

Upvotes: 1

Views: 1066

Answers (4)

shrikant joshi
shrikant joshi

Reputation: 564

the issue in "{}"

correct code:

import java.sql.*;

public class Connectivity 

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

       //  Database credentials
       static final String USER = "username";
       static final String PASS = "password";

       public static void main(String[] args) {
       Connection conn = null;//create object of Connection and define it null
      try //try block
      {
      //STEP 2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");
      //STEP 3: Open a connection
      System.out.println("Connecting to a selected database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
                        //print on console
      System.out.println("Connected database successfully...");      
                }
      catch(SQLException se) //catch block
      {
      //Handle errors for JDBC
      se.printStackTrace();
      }
      catch(Exception e) //catch block
      {
      //Handle errors for Class.forName
      e.printStackTrace();
      }
      finally  //finally block
      {
      //finally block used to close resources
      try  //try block
      {
      if(conn!=null)//condition
      conn.close(); //close connection
      }
      catch(SQLException se)//Handle errors
      {
      se.printStackTrace();
      }//end finally try
      }//end try
      System.out.println("Goodbye!"); //print on console
     }//end main
}

Upvotes: -1

Rahul Tripathi
Rahul Tripathi

Reputation: 172618

1) I think this is not the proper way to write the insert query:-

sql = "INSERT username, password, email INTO dvdsite";

You may change it to like this:-

sql = "INSERT INTO dvdsite values(username, password, email)";

Assuming your dvdsite table has just three columns else you need to specify the column names as well.

2) Also the static final declaration should be in the class ie, just make it outside the method.

public class DatabaseAccess extends HttpServlet{

      private static final String JDBC_DRIVER="com.mysql.jdbc.Driver";  
      private static final String DB_URL="jdbc:mysql://localhost/dvdsite";
    private static final String USER = "user";
    private static final String PASS = "";
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {

3) As you commented that you are getting the error after changing everything. This is because you are missing the semicolon ; here:-

   throws ServletException, IOException;

Upvotes: 1

Frederic Close
Frederic Close

Reputation: 9649

There are several compilation errors:

  1. static members cannot be defined within a method, it should be in the class
  2. the connection and statement object are not declared

and finally you sql insert is not correct.

Upvotes: 1

Tom
Tom

Reputation: 44881

The static final declarations should be outside the method call - just in the class.

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
  {

Upvotes: 2

Related Questions