?? jdbc_query.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_query { 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("textFileDriver").newInstance(); // the tinySQL textFile driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); // JDBC-ODBC bridge Class.forName("com.sybase.jdbc.SybDriver").newInstance(); // Sybase Class.forName("com.imaginary.sql.msql.MsqlDriver").newInstance(); // mSQL 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(); // Execute a query and get the result set // //ResultSet rs = stmt.executeQuery("SELECT name, id FROM test"); ResultSet rs = stmt.executeQuery("SELECT * FROM test"); // Display column headers // System.out.println("Name Id "); System.out.println("========================= ==="); // process each row // while(rs.next()) { // retrieve each column by name // String name = rs.getString("name"); int id = rs.getInt("id"); // display each row // System.out.println(name + " " + id); } int cnt = stmt.getUpdateCount(); System.out.println(cnt + " row(s) affected."); stmt.close(); con.close(); } catch( Exception e ) { System.out.println(e.getMessage()); e.printStackTrace(); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -