?? jdbc_prepared_statement.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_prepared_statement { 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 TABLE; // it will most assuredly throw one if // the table does not exist // try { stmt.executeUpdate("DROP TABLE test"); } catch (Exception e) { // do nothing } // create a table // stmt.executeUpdate("CREATE TABLE test (name CHAR(25), id INT)"); // here's some sample data // String data[][] = { {"Ford", "100"}, {"Arthur", "110"}, {"Trillian", "120"}, {"Zaphod", "130"}, {"Marvin", "140"}, {"Benjy", "150"}, {"Frankie", "160"}, {"Slartibartfast", "170"} }; // create a prepared statement to execute the update // PreparedStatement pstmt = con.prepareStatement( "INSERT INTO test (name, id) VALUES(?, ?)"); // for each row in the data[][] array, // add a row to the table using executeUpdate() // for (int i=0; i < data.length; i++) { // set the first parameter to a String, // the name of the person // pstmt.setString(1, data[i][0]); // set the second parameter to an int, // the id of the person // pstmt.setInt(2, Integer.parseInt(data[i][1])); // execute the prepared statement. The parameters will // be inserted each time it runs. // pstmt.executeUpdate(); } 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 + -