gpt4 book ai didi

java - 我的数据库 servlet 中的连接出错

转载 作者:行者123 更新时间:2023-12-02 08:33:03 24 4
gpt4 key购买 nike

我正在编写一个数据库 servlet,除了我的连接中似乎有错误之外,一切似乎都很好

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DBServlet3 extends HttpServlet
{
private static final long serialVersionUID = 1L;

@Override
public void init() throws ServletException
{
super.init();
try
{
String jdbcDriverClass=
getServletContext().getInitParameter( "jdbcDriverClass" );
if (jdbcDriverClass == null)
throw new ServletException( "Could not find jdbcDriverClass initialization parameter" );
Class.forName( jdbcDriverClass );
}
catch (ClassNotFoundException e)
{
throw new ServletException( "Could not load JDBC driver class", e );
}
}

@Override
protected void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
RequestDispatcher dispatcher=
request.getRequestDispatcher( "/db.jsp" );

ServletContext application= getServletContext();

ArrayList<String> names= new ArrayList<String>();

try
{

Connection connection= null;
Statement statement= null;
ResultSet results= null;

try
{
String jdbcUrl= application.getInitParameter( "jdbcUrl" );
String jdbcUser= application.getInitParameter( "jdbcUser" );
String jdbcPassword= application.getInitParameter( "jdbcPassword" );

connection=
DriverManager.getConnection( jdbcUrl, jdbcUser, jdbcPassword );

statement= connection.createStatement();

results= statement.executeQuery( "SELECT * FROM students" );

while (results.next())
{
String name= results.getString( "name" );
names.add( name );
}
}
finally
{
if (results != null)
results.close();
if (statement != null)
statement.close();
if (connection != null)
connection.close();
}
}
catch (SQLException e)
{
throw new ServletException( e );
}

request.setAttribute( "names", names );

dispatcher.forward( request, response );
}

@Override
protected void doPost( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
String sql= "INSERT INTO students VALUES (" +
request.getParameter( "id" ) + ", '" + request.getParameter( "name" ) + "')";

sql= "INSERT INTO students VALUES (?, ?, ?, ?)";

PreparedStatement statement= connection.prepareStatement( sql ); //error on this line

statement.setString( 1, request.getParameter( "id" ) );
statement.setString( 2, request.getParameter( "name" ) );
}

}

最佳答案

我根本不清楚具体问题是什么,我会编辑以添加堆栈跟踪。然而,一些观察。

  1. 您正在 doGet() 方法中获取并关闭连接,但 doPost() 方法中没有连接。所以这会导致错误。您应该按照每个请求以一致的方式获取连接,也许(进一步)检查连接池框架,例如 C3P0Apache DBCP .
  2. 不要通过构建字符串来形成 SQL,而是查看 PreparedStatements。它们将减少容易出错的代码并保护您免受 SQL 注入(inject)攻击。
  3. 您的 servlet 中有一个 serialVersionUid。您真的需要序列化您的 servlet(我怀疑不需要)吗?
  4. Apache DbUtils将为您做很多繁重的工作。 vanilla JDBC(例如,它将处理结果集/语句/连接关闭序列)。

关于java - 我的数据库 servlet 中的连接出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2760959/

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