package demo.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCUtils { public static String driver ="oracle.jdbc.oracleDriver"; public static String url="jdbc:oracle:thin:@localhost:ocrl"; public static String user="scott"; public static String passworld="tiger"; //注册数据库驱动 static { try { Class.forName(driver); // DriverManager.registerDriver(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new ExceptionInInitializerError(e); } } //获取数据库连接 public static Connection getConnection(){ try { return DriverManager.getConnection(url, user, passworld); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } //释放数据库资源 public static void release(Connection conn,Statement call,ResultSet rt){ if(rt!=null){ try { rt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ rt=null; } if (call!=null){ try { ((Connection) call).close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ call=null; } } if (conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ conn=null; } } } } } package demo.oracle; import java.sql.CallableStatement; import java.sql.Connection; import org.junit.Test; import demo.utils.JDBCUtils; import oracle.jdbc.internal.OracleTypes; public class TestFunction { @Test public void testFunction(){ String sql="?call qureyempincome(?)"; Connection conn=null; CallableStatement call=null; try { //得到数据库链接 conn=JDBCUtils.getConnection(); //基于链接创建statement call=conn.prepareCall(sql); //对于输出参数 申明 call.registerOutParameter(1,OracleTypes.NUMBER); // 对于输入参数 赋值 call.setInt(2, 7839); //执行调用 call.execute(); //取年收入的结果输出 double income =call.getDouble(1); System.out.println("该员工的年收入是:"+income); }catch(Exception e){ e.printStackTrace(); }finally{ JDBCUtils.release(conn, call, null); } } }
java.lang.ClassNotFoundException: oracle.jdbc.oracleDriver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at demo.utils.JDBCUtils.<clinit>(JDBCUtils.java:20)
at demo.oracle.TestFunction.testFunction(TestFunction.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
driver类是Oracle.jdbc.driver.OracleDriver注意区别大小写,然后就是环境变量以及驱动、数据库版本问题。
好的,谢谢