Import Statements

The import statement is used to make the classes in a package available. A typical import statement appears after any package statement and before any class definitions.

              import java.util.Dictionary;

The preceding code allows us to access the class Dictionary. We can also import all the classes within a package in one statement.

              import javax.swing.event.*;

You might think it is possible to import every core application programming interface(API) package with the following statement:

              import java.*;

This example will compile, and it will try to improt any classes within the directory Java. It will not, however, import any of package located in subdirectories of Java. In the preceding instance, there are no classes located directly in the Java package; therefore, nothing will be imported. 

/********************************************************************/ /* LABADDBATCH.JAVA */ /* This program will demonstrate how to use the addbatch() method */ /* */ /* Last updated = 01/01/2003 */ /********************************************************************/ /* Import statements */ /********************************************************************/ import java.sql.*; import java.io.*; import java.util.*; import java.math.*; public class labaddbatch { /**********************************************************************/ /* Register the class with the db2 Driver */ /**********************************************************************/ static { try { Class.forName ("COM.ibm.db2.jdbc.app.DB2Driver"); } catch (Exception e) { System.out.println ("\n Error loading DB2 Driver...\n"); System.out.println (e); System.exit(1); } } /**********************************************************************/ /* Main routine */ /**********************************************************************/ public static void main( String args[]) throws Exception { int outID = 0; String outname = " "; String outjob = " "; float outsalary = 0; int updaterowcount = 0; try { /* Establish connection and set default context */ System.out.println("Connect statement follows:"); Connection sample = DriverManager.getConnection("jdbc:db2:sample","student","student"); // (1) System.out.println("Connect completed"); sample.setAutoCommit(false); // (2) System.out.println("\nAutocommit set off"); Statement stmt = sample.createStatement(); // (3) System.out.println("\n Batch Statements begin "); statement.executeUpdate("SET FOREIGN_KEY_CHECKS = 0"); stmt.addBatch("INSERT INTO JLU.DEPARTMENT " + "VALUES ('BT6','BATCH6 NEWYORK','BBBBB1','BTT','NEW YORK CITY6')"); // (4) stmt.addBatch("INSERT INTO JLU.DEPARTMENT " + "VALUES ('BT7','BATCH7 NEWYORK','BBBBB2','BT2','NEW YORK CITY7')"); // (5) stmt.addBatch("INSERT INTO JLU.DEPARTMENT " + "VALUES ('BT8','BATCH8 NEWYORK','BBBBB3','BT3','NEW YORK CITY8')"); // (6) stmt.addBatch("INSERT INTO JLU.DEPARTMENT " + "VALUES ('BT9','BATCH9 NEWYORK','BBBBB4','BT4','NEW YORK CITY9')"); // (7) stmt.addBatch("INSERT INTO JLU.DEPARTMENT " + "VALUES ('BTA','BATCH10 NEWYORK','BBBBB5','BT5','NEW YORK CITY10')"); // (8) statement.executeUpdate("SET FOREIGN_KEY_CHECKS = 1"); System.out.println("\n Batch statements completed executeBatch follows"); int [] updateCounts = stmt.executeBatch(); // (9) for (int i = 0; i < updateCounts.length; i++) { System.out.println("\nUpdate row count " + updateCounts[i] ); } sample.commit(); // (10) } // end of try catch (SQLException e) { System.out.println("\n SQLState: " + e.getSQLState() + " SQLCode: " + e.getErrorCode()); System.out.println("\n Message " + e); System.out.println("\n Get Error Message: " + e.getMessage()); } } // End Main } // End Program 判断代码中正确性
12-04
The import statement import_stmt ::= "import" module ["as" name] ( "," module ["as" name] )* | "from" relative_module "import" identifier ["as" name] ( "," identifier ["as" name] )* | "from" relative_module "import" "(" identifier ["as" name] ( "," identifier ["as" name] )* [","] ")" | "from" module "import" "*" module ::= (identifier ".")* identifier relative_module ::= "."* module | "."+ name ::= identifier The basic import statement (no from clause) is executed in two steps: find a module, loading and initializing it if necessary define a name or names in the local namespace for the scope where the import statement occurs. When the statement contains multiple clauses (separated by commas) the two steps are carried out separately for each clause, just as though the clauses had been separated out into individiual import statements. The details of the first step, finding and loading modules are described in greater detail in the section on the import system, which also describes the various types of packages and modules that can be imported, as well as all the hooks that can be used to customize the import system. Note that failures in this step may indicate either that the module could not be located, or that an error occurred while initializing the module, which includes execution of the module’s code. If the requested module is retrieved successfully, it will be made available in the local namespace in one of three ways: If the module name is followed by as, then the name following as is bound directly to the imported module. If no other name is specified, and the module being imported is a top level module, the module’s name is bound in the local namespace as a reference to the imported module If the module being imported is not a top level module, then the name of the top level package that contains the module is bound in the local namespace as a reference to the top level package. The imported module must be accessed using its full qualified name rather than directly The from form uses a slightly more complex process: find the module specified in the from clause, loading and initializing it if necessary; for each of the identifiers specified in the import clauses: check if the imported module has an attribute by that name if not, attempt to import a submodule with that name and then check the imported module again for that attribute if the attribute is not found, ImportError is raised. otherwise, a reference to that value is stored in the local namespace, using the name in the as clause if it is present, otherwise using the attribute name Examples: import foo # foo imported and bound locally import foo.bar.baz # foo.bar.baz imported, foo bound locally import foo.bar.baz as fbb # foo.bar.baz imported and bound as fbb from foo.bar import baz # foo.bar.baz imported and bound as baz from foo import attr # foo imported and foo.attr bound as attr If the list of identifiers is replaced by a star ('*'), all public names defined in the module are bound in the local namespace for the scope where the import statement occurs. The public names defined by a module are determined by checking the module’s namespace for a variable named __all__; if defined, it must be a sequence of strings which are names defined or imported by that module. The names given in __all__ are all considered public and are required to exist. If __all__ is not defined, the set of public names includes all names found in the module’s namespace which do not begin with an underscore character ('_'). __all__ should contain the entire public API. It is intended to avoid accidentally exporting items that are not part of the API (such as library modules which were imported and used within the module). The wild card form of import — from module import * — is only allowed at the module level. Attempting to use it in class or function definitions will raise a SyntaxError. When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after from you can specify how high to traverse up the current package hierarchy without specifying exact names. One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute from . import mod from a module in the pkg package then you will end up importing pkg.mod. If you execute from ..subpkg2 import mod from within pkg.subpkg1 you will import pkg.subpkg2.mod. The specification for relative imports is contained within PEP 328 . importlib.import_module() is provided to support applications that determine dynamically the modules to be loaded.
05-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值