📄 ddl.java
字号:
/** * * DDL.java - Data Definition Language for Chapter 4 Examples * * 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.net.URL;import java.sql.*;import java.lang.*;class DDL { public static void main (String argv[]) { if (argv.length == 0) { System.out.println("You must supply a URL to a JDBC data source."); System.out.println(""); System.out.println("Example:"); System.out.println("java DDL jdbc:odbc:DATA_SOURCE_NAME;" + "UID=userid;PWD=password"); System.exit(0); } 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 = ""; } 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 Connection con = DriverManager.getConnection(url, user, pwd); Statement stmt = con.createStatement(); // The drops need to be included in these special // try...catch clauses. Since the tables might not // exist, an exception might be thrown which you // want to ignore. try { stmt.executeUpdate("DROP TABLE tree"); } catch (Exception e) { } // create the tables stmt.executeUpdate("CREATE TABLE tree " + "(name CHAR(80), tree_id INT, parent_id INT)"); // insert some sample tree items // I don't feel like coding each and every // insert, so I'm putting it into a two dimensional // array, and generating the insert statements from // that. // String data[][] = { {"Some sort of lobster-like creature", "100", "0"}, {"CEO", "110", "100"}, {"CFO", "120", "100"}, {"Regional Managers", "130", "110"}, {"Accounting Manager", "140", "120"}, {"Belligerent, Drunken Mimes", "150", "240"}, {"Grunts in the Accounting Department", "160", "140"}, {"The crazy guy who brought a gun in " + "and threatened to blow our heads off", "170", "130"}, {"A friend of the crazy guy", "180", "130"}, {"Sporty Mime", "190", "150"}, {"Scary Mime", "200", "150"}, {"Baby Mime", "210", "150"}, {"Ginger Mime", "220", "150"}, {"Posh Mime", "230", "150"}, {"C3PO", "240", "100"}, {"R2D2", "250", "240"} }; for (int i=0; i < data.length; i++) { String insert = "INSERT INTO tree " + " (name, tree_id, parent_id) " + " VALUES (" + "\'" + data[i][0] + "\', " + data[i][1] + ", " + data[i][2] + ") "; stmt.executeUpdate(insert); } stmt.close(); con.close(); System.out.println("Tables were created successfully."); } catch (SQLException e) { while (e != null) { System.out.println("SQLState: " + e.getSQLState()); System.out.println("Message: " + e.getMessage()); System.out.println("Vendor: " + e.getErrorCode()); e = e.getNextException(); System.out.println(""); } } catch (Exception e) { e.printStackTrace (); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -