1

帮助!我正在做一个类项目,我必须使用 Java Servlet 将信息插入数据库......是的,它必须是 Java Servlet。我的代码几乎是正确的,但是当我尝试编译它时,我遇到了非法的表达式开始错误。

这是代码:

    //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
     }
     } 

有人可以帮忙吗?这几天我一直在为此苦苦挣扎!

4

4 回答 4

2

静态最终声明应该在方法调用之外——就在类中。

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
  {
于 2013-10-20T17:33:23.700 回答
1

1)我认为这不是编写插入查询的正确方法:-

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

你可以把它改成这样:-

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

假设您的dvdsite 表只有三列,否则您还需要指定列名。

2)static final声明也应该在类中,即在方法之外。

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)正如您所评论的那样,您在更改所有内容后都会收到错误消息。这是因为你错过了semicolon ;这里: -

   throws ServletException, IOException;
于 2013-10-20T17:31:16.707 回答
1

有几个编译错误:

  1. 静态成员不能在方法中定义,它应该在类中
  2. 未声明连接和语句对象

最后你的sql插入不正确。

于 2013-10-20T17:34:36.837 回答
-1

“{}”中的问题

正确的代码:

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
}
于 2018-03-15T08:25:43.973 回答