gpt4 book ai didi

mysql - Servlet-数据库连接问题

转载 作者:搜寻专家 更新时间:2023-10-30 20:43:44 28 4
gpt4 key购买 nike

我正在尝试将 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");

}

}

最佳答案

我会把

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 block 中。

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) {}
}

}
}

或者类似的东西。

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

关于mysql - Servlet-数据库连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9191567/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com