2012-02-08 94 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