我正在尝试使用数组列表中的值更新数据库。以下是我正在尝试的代码。
String query = "insert into login values(? , ?)";
Connection conn = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
conn = getConnection();
for(int i = 0;i < rowlength; i++)
{
System.out.println(rowList.get(i));
statement = conn.prepareStatement(query);
statement.setInt(1, rowList.get(i));
statement.setString(2, rowsList.get(i+2));
resultSet = statement.executeQuery();
}
}
catch (SQLException e) {
try {
throw new ServletException("DB interaction failed", e);
} catch (ServletException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {}
if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
if (conn != null) try { conn.close(); } catch (SQLException ignore) {}
}
它在控制台中为所有循环值提取跟随错误。
java.lang.ClassNotFoundException: org.postgresql.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass (WebappClassLoader.java:1358)
at org.apache.catalina.loader.WebappClassLoader.loadClass (WebappClassLoader.java:1204)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.aipl.struts.action.ExcelDbAction.getConnection(ExcelDbAction.java:60)
at com.aipl.struts.action.ExcelDbAction.execute(ExcelDbAction.java:126)
at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
任何人都可以指导我做错了什么,或者他们是更新数据库中表格的另一种方式。
谢谢////
答案 0 :(得分:2)
你做错了是PostgreSQL数据库驱动程序不在你的应用程序的类路径上。基于堆栈跟踪,我猜你正在使用Tomcat;所以请查看The PostgreSQL JDBC Interface文档,了解应该放置它的位置。
答案 1 :(得分:2)
当jvm在classpath
上找不到jdbc-driver时,会出现此错误。确保您已正确配置类路径
答案 2 :(得分:2)
您需要加载数据库驱动程序。例如,对于SQL Server
Connection connection = DriverManager.getConnection(connectionUrl);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
此外,您可以使用Java的try-with资源(Oracle Link:http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)功能,而不是关闭finally块中的资源。