Servlet-database连接问题

时间:2012-02-08 10:31:43

标签: mysql database servlets jdbc

我正在尝试将servletmysql数据库连接...但是在servlet代码中... 声明:

Class.forName(driver)在工具提示 -

上显示红色下划线错误
 Syntax error on token-"driver",VariableDeclaratorId expected after this token.

我无法理解为什么会发生这种情况。

这是servlet代码:

package Servlets;

import java.io.IOException;
import java.sql.*;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet  
{

    private static final long serialVersionUID = 1L;

    public LoginServlet() 
    {
        super();
    }
   Connection con = null;

   String url = "jdbc:mysql://localhost:3306/";

   String db = "abc";

   String driver = "com.mysql.jdbc.Driver";

   Class.forName(driver);


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

    {
        String username= request.getParameter("username");
        String password= request.getParameter("password");
        con = DriverManager.getConnection("url+db","root","root");
        Statement st = con.createStatement();
        int val = st.executeUpdate("INSERT login values("+username+","+password+")");
        System.out.println("1 row affected");

        response.sendRedirect("login.jsp");

    }

}

4 个答案:

答案 0 :(得分:3)

我把

Class.forName("com.mysql.jdbc.Driver").newInstance();

就在

之上
con = DriverManager.getConnection("url+db","root","root");

请记住,servlet需要是多线程的,以便

Connection con = null;

当两个用户同时尝试登录时,可能会导致问题。

此外,您需要正确关闭连接

try {
    if(con != null)
      con.close();
      } catch(SQLException e) {}

最后你将需要处理数据库访问可能导致的任何异常,所以将它包装在try catch块中。

package Servlets;

import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet  
{

private static final long serialVersionUID = 1L;

public LoginServlet() 
{
    super();
}

String url = "jdbc:mysql://localhost:3306/";
String db = "abc";
String driver = "com.mysql.jdbc.Driver";

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{
   try{
    Connection con = null;
    String username= request.getParameter("username");
    String password= request.getParameter("password");
    Class.forName(driver).newInstance();
    con = DriverManager.getConnection("url+db","root","root");
    Statement st = con.createStatement();
    int val = st.executeUpdate("INSERT login values("+username+","+password+")");
    System.out.println("1 row affected");

    response.sendRedirect("login.jsp");
  }catch(SQLException e){}
  finally{
    try {
      if(con != null)
       con.close();con=null;
       } catch(SQLException e) {}
  }

}
}

或类似的东西。

最后要注意,使用“连接池”要好得多。

答案 1 :(得分:0)

添加一个mysql连接器5.1.0 bin jar文件..在项目库中添加jar文件。

链接http://www.java2s.com/Code/Jar/m/Downloadmysqldatabase1610jar.htm

答案 2 :(得分:-1)

Class.forName( "com.mysql.jdbc.Driver" );
Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/rental","root","root" ) ;
Statement st = conn.createStatement();
String sql = "select * from login";
ResultSet rs = st.executeQuery(sql);

答案 3 :(得分:-1)

Class.forName(" com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection(" jdbc:mysql:// localhost:3306 / rental"," root"," root");