📄 jdbc_callable.java
字号:
/* Copyright 1996 John Wiley & Sons, Inc. All Rights Reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without the express written permission of the copyright owner is unlawful. Requests for further information should be addressed to Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages, caused by the use of this software or from the use of the information contained herein.*/import java.util.*;import java.net.URL;import java.sql.*;class jdbc_callable { public static void main(String argv[]) { // the url comes in from argv[0], so // error out if there was no url passed // if (argv.length == 0) { System.err.println("Usage:"); System.err.println(""); System.err.println("java jdbc_connect URL"); System.exit(1); } try { // register all of the JDBC classes you might use // you can comment out or remove the ones you // are not using. // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); // JDBC-ODBC bridge Class.forName("com.sybase.jdbc.SybDriver").newInstance(); // Sybase String url = argv[0]; // the user might have passed in a user name or password, // so try to read those in, as well // String user, pwd; if (argv.length > 1) { user = argv[1]; } else { user = ""; } if (argv.length > 2) { pwd = argv[2]; } else { pwd = ""; } // make a connection to the specified URL // Connection con = DriverManager.getConnection(url, user, pwd); // get a Statement object from the Connection // Statement stmt = con.createStatement(); // ignore any exception for DROP PROCEDURE; // it will most assuredly throw one if // the table does not exist // try { stmt.executeUpdate("DROP PROCEDURE test_sp"); } catch (Exception e) { // do nothing } // create a stored proc // stmt.executeUpdate("CREATE PROCEDURE test_sp " + " @id INT, " + " @name VARCHAR(50) OUTPUT " + "AS " + "BEGIN " + " SELECT @name = name FROM test WHERE id = @id " + "END"); // set up a call to that stored proc // String sql = "{call test_sp(?, ?)}"; CallableStatement cstmt = con.prepareCall(sql); cstmt.setInt(1, 1); // get the name for id = 1 // register the output parameter as a VARCHAR // cstmt.registerOutParameter(2, java.sql.Types.VARCHAR); // execute the stored procedure // cstmt.executeUpdate(); // display the output // System.out.println( "Id 1 is: " + cstmt.getString(2) ); cstmt.close(); stmt.close(); con.close(); System.out.println("Operation successful."); } catch( Exception e ) { System.out.println(e.getMessage()); e.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -