本人刚要入门JAVA,环境eclipse,JDK,1.7版本,下了个oracle 11g R2,里面只有ojdbc6,也按照oracle的建议配置了环境变量
On Windows platforms:
- Add [ORACLE_HOME]\jdbc\lib\ojdbc5.jar to
your CLASSPATH if you use JDK 1.5 or
[ORACLE_HOME]\jdbc\lib\ojdbc6.jar if you use JDK 1.6.
- Add [ORACLE_HOME]\jlib\orai18n.jar to your CLASSPATH if needed.
- Add [ORACLE_HOME]\bin to your PATH if you are using the JDBC OCI
driver.
这个是我的classpath:C:\Program Files\Java\ojdbc6.jar;D:\app\milo\product\11.2.0\dbhome_1\jlib\orai18n.jar;D:\app\milo\product\11.2.0\dbhome_1\oui\jlib\classes12.jar
程序如下:
package com.milo.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
public class OracleConn {
//public static final String DBDRIVER="oracle.jdbc.driver.OracleDriver";
public static final String DBDRIVER="oracle.jdbc.driver.OracleDriver";
public static final String CONSTR="jdbc:oracle:thin:@localhost:1521:orcl";
public static final String USER="milo";
public static final String PWD="123aBc";
public void InsertDb() throws Exception{
Connection conn=null;
Class.forName(DBDRIVER);
//DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn=DriverManager.getConnection(CONSTR,USER,PWD);
PreparedStatement pstmt=null;
String sql="INSERT INTO STUDENT VALUES('s003','jack')";
pstmt=conn.prepareStatement(sql);
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
}
为何每次都报错
java.lang.ClassNotFoundException: oracle.jdbc.driver.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(Unknown Source)
at com.milo.test.OracleConn.InsertDb(OracleConn.java:21)
at com.milo.test.DbConnectTest.doGet(DbConnectTest.java:33)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
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:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)