gpt4 book ai didi

java - 使用 JSP 的数据库并收到错误

转载 作者:行者123 更新时间:2023-11-29 23:28:06 24 4
gpt4 key购买 nike

我在尝试向数据库添加信息时创建的 JSP 文件存在问题。当我尝试执行并且它指向此代码块时,我不断收到 org.apache.jasper.JasperException: Exception in JSP: 错误。我假设它与 setAutoCommit 语句有关,但我不确定,因为它也显示了 nullPointerException。任何见解都会有所帮助。

<%@ page import="java.sql.*" %>

<%
String url = "jdbc:mysql://example:3306/database";
String username = "test";
String password = "test";
Connection conn = null;
Statement stm=null;

String classPath = "sun.jdbc.odbc.jdbc0dbcDriver";
try{
Class.forName(classPath);
conn = DriverManager.getConnection(url,username,password);
}catch(Exception exc){

out.println(exc.toString());

}

%>

<center><a href="example3-1.jsp">View Movies</a></center><br>
<%
String MovieTitle=request.getParameter("MovieTitle");
String MainActor=request.getParameter("MainActor");
String MainActress=request.getParameter("MainActress");
String Rating=request.getParameter("Rating");

String query1 = null;

query1 = "INSERT INTO MovieTitles (MovieTitle,MainActor,MainActress,Rating) VALUES('"+MovieTitle+"','"+MainActor+"','"+MainActress+"','"+Rating+"')";

conn.setAutoCommit(false);
stm = conn.createStatement();
try{
stm.executeUpdate(query1);
conn.commit();
out.println("<center><h2>A new movie has been added.</h2>");
out.println("<a href=example3-2.jsp>Add another Movie</a></center>");
}catch(Exception exc){
conn.rollback();
conn.setAutoCommit(true);
%>

<script>
alert("Entry failed. Please retry.");
</script>
<%}%>
<%
if(stm != null){ stm.close();}
conn.close();
%>

最佳答案

//步骤:注册 JDBC 驱动程序

  Class.forName("com.mysql.jdbc.Driver");

//步骤:打开连接

conn=DriverManager.getConnection(DB_URL, USER, PASS);

关于java - 使用 JSP 的数据库并收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26793671/

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