JDBC development steps:
- Import driver package (the package itself Baidu download the ojdbc14.jar)
- Registration drive
- Get connected
- Gets the SQL statement executed
- Packaging Parameters
- SQL execution
- Getting Results
- Release resources
Import java.sql.CallableStatement Sets; Import the java.sql.Connection; Import the java.sql.DriverManager; Import the java.sql.ResultSet; Import org.junit.Test; Import oracle.jdbc.OracleCallableStatement; Import oracle.jdbc.driver.OracleTypes ; / * 1. import driver package 2. Register drive 3 acquires the connection 4. Get executing SQL Statement 5. the package parameters 6. execute SQL 7. the results acquired 8. the release resources * / public class TestProcedure { @Test / * Create Procedure proc_gettotalsal the replace or (in Number The vempno, vtotalsal Number The OUT) IS begin select sal*12 + nvl(comm,0) into vtotalsal from emp where empno = vempno; end; * */ public void test1() throws Exception{ //注册驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); //2.获取连接 String url = "jdbc:oracle:thin:@192.168.80.100:1521:orcl"; String username = "dakang"; String password = "dakang"; Connection conn = DriverManager.getConnection(url, username, password); //3. execute the SQL statement to obtain a fixed format proc_gettotalsal which is a function of memory. String SQL = "{Call proc_gettotalsal (,??)}" ; CallableStatement State = conn.prepareCall (SQL); // set of input parameters state. setInt (. 1, 7788); // set the employee number // Register output parameter type state.registerOutParameter (2 , OracleTypes.NUMBER); // 4. execution Statement state.execute (); // 5. The execution result acquired int totalsal state.getInt = (2 ); // output System.out.println ( "salary:" + totalsal); // 6. The release resources state.close (); conn.Close (); } // call the stored function / * Create or Replace function func_getsal (vempno Number) Number return IS - declare variables saved salary. vtotalsal Number; the begin SELECT * SAL 12 is + NVL (COMM, 0) INTO vtotalsal EMP WHERE EMPNO = vempno from; return vtotalsal; End; * / @Test public void test2 () throws Exception { // Register drive the Class.forName ( "oracle.jdbc.driver.OracleDriver" ); // 2. Get the connection String = URL "JDBC: Oracle: Thin: @ 192.168.80.100: 1521: ORCL" ; Username String = "Dakang" ; String password = "Dakang" ; Connection conn = DriverManager.getConnection (url, username, password); // 3. obtain execution of SQL statement String sql = "{= call func_getsal ? (?)} " ; CallableStatement State = conn.prepareCall (SQL); // 4. package parameters // Register return argument type state.registerOutParameter (. 1 , OracleTypes.NUMBER); // set the second parameter state.setInt (2, 7788 ) ; // 5. SQL execution state.execute (); //6. The acquisition result int totalsal state.getInt = (. 1 ); System.out.println ( "salary: ====" + totalsal); // 7. The release resources state.close (); conn.Close (); } / * Create or Replace Procedure proc_getemps (vrows SYS_REFCURSOR OUT) IS the begin --1 open cursor, the cursor to the assignment. open vrows for SELECT * from EMP; End; * * / @Test public void Test3 () throws Exception { // registration drive Class.forName("oracle.jdbc.driver.OracleDriver"); //2.获取连接 String url = "jdbc:oracle:thin:@192.168.80.100:1521:orcl"; String username = "dakang"; String password = "dakang"; Connection conn = DriverManager.getConnection(url, username,password); //3.获取执行SQL的statement String sql = "{call proc_getemps(?)}"; CallableStatement call = conn.prepareCall(sql); //接口 --- > 对象 -->Implementation class name = System.out.println (call.getClass ().getName()); OracleCallableStatement oracleCall (OracleCallableStatement) Call; // 4. registered output types of parameters call.registerOutParameter (. 1 , OracleTypes.CURSOR); // 5. The execute the SQL call.execute (); // 6. The acquired execution result ResultSet resultSet = oracleCall.getCursor (. 1 ); the while (ResultSet.next ()) { int EMPNO = ResultSet.getInt ( "EMPNO" ); String name = resultSet.getString ( "ename" ); System.out.println (EMPNO + "= === "+ name); } // 7. The release of resources ResultSet.close (); call.close (); conn.close(); } }