如何使用Java Servlet将信息插入数据库

时间:2013-10-20 17:24:40

标签: java

帮助!我正在研究一个类项目,我必须使用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 个答案:

答案 0 :(得分: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
  {

答案 1 :(得分: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;

答案 2 :(得分:1)

有几个编译错误:

  1. 静态成员不能在方法中定义,它应该在类
  2. 未声明连接和语句对象
  3. 最后你的sql插入不正确。

答案 3 :(得分:-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
}