0

我正在尝试使用数组列表中的值更新我的数据库。以下是我正在尝试的代码。

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)

任何人都可以指导我做错了什么,或者他们是更新数据库中表的另一种方式。

谢谢////

4

3 回答 3

2

您做错的是 PostgreSQL 数据库驱动程序不在应用程序的类路径中。根据堆栈跟踪,我猜您正在使用 Tomcat;因此,请查看The PostgreSQL JDBC Interface文档以了解应将其放置在何处。

于 2013-04-05T12:28:07.447 回答
2

当 jvm 在其classpath. 确保您已正确配置类路径

于 2013-04-05T12:28:32.157 回答
2

您需要加载数据库驱动程序。例如对于 SQL Server Connection connection = DriverManager.getConnection(connectionUrl); Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

此外,您可以使用 Java 的 try-with 资源(Oracle 链接:http ://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html )功能,而不是在 finally 块中关闭资源。

于 2013-04-05T12:34:03.070 回答