无法将记录插入数据库

时间:2015-12-28 10:15:48

标签: java jdbc prepared-statement

当我尝试使用预准备语句插入记录时出现错误:

Registration Servlet INSIDE try block:
Printing USER Values
====================
john
smith
john@gmail.com
john
Connection is: null
username: rth
password: rth
driver: oracle.jdbc.OracleDriver
url is: jdbc:oracle:thin:@localhost:1521:orcl
@@@@Record insertion error in Registration DAO@@@@
java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.RTH.commons.DBConnection.getConnection(DBConnection.java:42)
    at com.RTH.DAO.RegistrationDAO.insert(RegistrationDAO.java:29)
    at com.RTH.Registration.RegistrationServlet.doPost(RegistrationServlet.java:43)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:1521:orcl
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.RTH.commons.DBConnection.getConnection(DBConnection.java:49)
    at com.RTH.DAO.RegistrationDAO.insert(RegistrationDAO.java:29)
    at com.RTH.Registration.RegistrationServlet.doPost(RegistrationServlet.java:43)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
java.lang.NullPointerException
    at com.RTH.DAO.RegistrationDAO.insert(RegistrationDAO.java:31)
    at com.RTH.Registration.RegistrationServlet.doPost(RegistrationServlet.java:43)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我可以打印连接详细信息以及属性文件:

public class DBConnection {

    public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException
    {
        Properties props = new Properties();
        props.load(DBConnection.class.getResourceAsStream("DBDetails.properties"));
        String username = props.getProperty("dbusername");
        String password = props.getProperty("dbpassword");
        String driver = props.getProperty("driver");
        String url = props.getProperty("url");
        Class.forName(driver);
        Connection con = DriverManager.getConnection(url, username, password);
        System.out.println("Connection is: " + con);
        System.out.println("username: " + username);;
        System.out.println("password: " + password);;
        System.out.println("driver: "+ driver);;
        System.out.println("url is: " + url);;
        return con;
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        DBConnection.getConnection();
    }
}

RegistrationDao文件:

public class RegistrationDAO {

    public void insert(User user) {

        try {
            System.out.println("Printing USER Values");
            System.out.println("====================");
            System.out.println(user.getFirstName());
            System.out.println(user.getLastName());
            System.out.println(user.getEmail());
            System.out.println(user.getPassword());

            Connection con = DBConnection.getConnection();
            String query = "insert into TBL_USER(USR_FIRST_NAME,USR_LST_NAME,USR_PRIMARY_EMAIL,USR_PASSWORD) values(?,?,?,?)";
            PreparedStatement pst = con.prepareStatement(query);

            pst.setString(1, user.getFirstName()); //prints value
            pst.setString(2, user.getLastName());  //prints value
            pst.setString(3, user.getEmail());     //prints value
            pst.setString(4, user.getPassword());  //prints value
            pst.executeUpdate();
        } catch (Exception e) {
            System.out.println("@@@@Record insertion error in Registration DAO@@@@");
            e.printStackTrace();
        }
    }
}

属性文件:

dbusername=rth
dbpassword=rth
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl

我已检查过数据库用户名和密码是否已过期且仍在运行。连接测试成功。所有列类型都是varchar2 200字节。我不确定为什么它还在抛出错误。

3 个答案:

答案 0 :(得分:1)

问题出现在你的例外的第一行:

props.load(DBConnection.class.getResourceAsStream("DBDetails.properties"));

这意味着:文件"DBDetails.properties"显然不在当前目录中,也不在其中一个类路径设置中。

检查文件的位置并相应地设置类路径或将其移动到正确的位置。

更新Q后编辑

与oracle驱动程序相同。 oracle驱动程序类没有被加载,可能它也不在类路径上 - 甚至根本不存在(我会打赌,因为这需要从Oracle明确下载/安装)

java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver

答案 1 :(得分:1)

您可以看到sysout语句,因为您必须在IDE中运行程序,其中属性文件位于类路径中。部署应用程序时,您的属性文件不在类路径中。

您可以将代码更改为:

props.load(DBConnection.class.getResourceAsStream("../conf/DBDetails.properties"));

创建目录'conf'||到包含你的代码(jar)的lib文件夹并尝试运行它。

答案 2 :(得分:1)

按照这些步骤解决了我的问题:

  1. 从类路径中删除ojdbc6-11.1.0.7.0.jar并添加 ojdbc6.jar
  2. 更改了驱动程序名称:

    driver = oracle.jdbc.OracleDriver to driver = oracle.jdbc.driver.OracleDriver

  3. ojdbc6.jar个文件粘贴到 apache-tomcat-7.0.35-windows-x86\apache-tomcat-7.0.35\lib文件夹
  4. 它有效!!