Servlet 数据库连接问题
Posted
技术标签:
【中文标题】Servlet 数据库连接问题【英文标题】:Servlet-database connection issue 【发布时间】:2012-02-29 18:50:00 【问题描述】:我正在尝试将 servlet
与 mysql
数据库连接...但在 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");
【问题讨论】:
【参考方案1】:我会放
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)
或者类似的东西。
最后一点,最好使用“连接池”。
【讨论】:
【参考方案2】: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】:Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/rental","root","root");
【讨论】:
你的答案应该包含对你的代码的解释和它如何解决问题的描述。【参考方案4】:添加一个 mysql 连接器 5.1.0 bin jar 文件.. 在您的项目库中添加 jar 文件。
链接http://www.java2s.com/Code/Jar/m/Downloadmysqldatabase1610jar.htm
【讨论】:
虽然这在理论上可以回答问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。以上是关于Servlet 数据库连接问题的主要内容,如果未能解决你的问题,请参考以下文章