2012-02-08 95 views
2

我试图连接servletmysql数据库......但在servlet代码.. 声明:Servlet的数据库连接问题

Class.forName(driver)显示与tooltip-

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"); 

    } 

} 

回答

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
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); 
-1

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

+0

你的回答应该包含你的代码的解释和它是如何解决问题的说明。 – AbcAeffchen 2014-11-04 05:13:33

0

添加一个MySQL连接器5.1.0 bin jar文件..将jar文件添加到您的项目库中。

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

+0

虽然这可能在理论上回答这个问题,但[这将是更可取的](// meta.stackoverflow.com/q/8259)在这里包括了答案的基本部分,并提供了供参考的链接。 – 2015-08-11 05:30:09