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 { private static String driver = "oracle.jdbc.OracleDriver"; private static String url = "jdbc:oracle:thin:@localhost:zhang0519"; private static String user = "scott"; private static String password = "tiger"; //注册数据库驱动 static{ try { Class.forName(driver); } catch (ClassNotFoundException e) { throw new ExceptionInInitializerError(e); } } //获取数据库连接 public static Connection getConnection(){ try { return DriverManager.getConnection(url,user,password); } catch (SQLException e) { e.printStackTrace(); } return null; } //释放数据库的资源 public static void release(Connection conn,Statement st,ResultSet rs){ if(rs != null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); }finally{ rs = null; } } if(st != null){ try { st.close(); } catch (SQLException e) { e.printStackTrace(); }finally{ st = null; } } if(conn != null){ try { conn.close(); } catch (SQLException e) { 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.driver.OracleTypes; public class TestProcedure { @Test public void testProcedure(){ String sql = "{call queryempinf(?,?,?,?)}"; Connection conn = null; CallableStatement call = null; try { //得到一个连接 conn = JDBCUtils.getConnection(); //通过连接创建Statement call = conn.prepareCall(sql); //对于in参数,赋值 call.setInt(1,7839); //对于out参数,申明 call.registerOutParameter(2, OracleTypes.VARCHAR); call.registerOutParameter(3, OracleTypes.NUMBER); call.registerOutParameter(4, OracleTypes.VARCHAR); //执行调用 call.execute(); //取出结果 String name = call.getNString(2); double sal = call.getDouble(3); String job = call.getNString(4); System.out.println(name+"\t"+sal+"\t"+job); } catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.release(conn, call, null); } } }
你的URL设置的有问题:private static String url = "jdbc:oracle:thin:@localhost:zhang0519";
这个是我本地的:private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
注意格式:jdbc:oracle:thin:@localhost:端口号:数据库名 注意这里的汉字部分,且是以冒号隔开的
异常如下: Class not found demo.oracle.TestProcedure java.lang.ClassNotFoundException: demo.oracle.TestProcedure at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:685) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:421) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444) 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)