Nineth Week Java Learning Summary

Source: Internet
Author: User

20145306 "Java program design" The Nineth Week study summary textbook Learning content Summary
 The 16th Chapter One, Introduction to JDBC 1.JDBC JDBC is a solution for executing SQL, the developer uses the standard interface of JDBC, the database vendor operates on the interface, the developer does not have to touch the difference of the underlying database driver, the database itself is a stand-alone application, and the application you compose is to use the communication protocol to the database into The line instruction exchange, in order to carry on the data deletion search. Typically, your application uses a set of linked libraries that specifically communicate with the database to simplify the process of writing to the database. Sometimes, the need to replace a database is not without, and application cross-platform is often a requirement, and JDBC is basically used to solve these problems. JDBC is the standard specification for Java online databases. Specifically, it defines a set of standard classes and interfaces that the application requires when the online database is called, while the interfaces in the standard API are operated by the database vendor, often referred to as the JDBC driver. The JDBC standard consists of two parts: the JDBC Application developer interface and the JDBC driver developer interface. If your application requires an online database, it is called the JDBC Application developer interface, the API is primarily in java.sql and javax.sql two packages, and the JDBC driver developer Interface is the specification of the database vendor operating driver. If there is a program code, it is the role of the online MySQL database, you need to set the JDBC driver in classpath, specifically, in the classpath to set a jar document, if you want to replace the Oracle database, as long as the replacement of the Oracle driver , specifically, the jar document that was changed to the Oracle driver in Classpath, but the application itself is not modified. What JDBC wants to achieve is that Java programmers can have a unified interface when composing database operations, without relying on the API of a particular database, in order to "write a Java program, manipulate all Databases". When operating the JDBC driver, the manufacturer can be divided into four types: (1) jdbc-odbc Bridge Driver, which is a Microsoft-led database Connectivity Standard, basically JDBC is a reference to ODBC, So ODBC is the most mature on Microsoft systems, but because JDBC does not correspond to ODBC one by one, some calls cannot be converted directly, so the ODBC driver itself has a cross-platform limitation. (2) Native API Driver, this type of driver will be in the native way, call the database provided by the native link library, at a speed of 4 types of the fastest, but not cross-platform. (3) Jdbc-net Driver, this type of JDBCThe driver transforms the JDBC method call into a specific network protocol call, which is highly elastic and has a cross-platform nature. (4) Native Protocol Driver, this way can be cross-platform, performance also has a good performance, when not required as (3) to obtain architectural flexibility, usually use this type of driver, is the most common driver type. 2. Connecting the database in order to connect to the database system, you must have a vendor-operated JDBC driver, and you must set the driver jar documentation in CLASSPATH. 3. Using statement, Resultsetconnection is the database connection representative object, next to execute SQL, you must obtain the Java.sql.Statement operation object, which is the representative object of SQL description. Statement's execute () can be used to execute SQL, and can test whether SQL is executing a query or an update, and returning TRUE indicates that SQL execution will return resultset as the result of the query. Depending on the requirements, statement or resultset can be closed using close () when not in use to release the associated resources. When the statement is closed, the associated resultset is also automatically closed. 4. Use PreparedStatement, callablestatement if some of the operations are just some of the arguments in the SQL statement are different, the rest of the SQL clauses are the same, you can use Java.sql.PreparedStatement. You can use the connection PreparedStatement () method to build a precompiled SQL statement, where the parameters change, specifying the "? "This placeholder character. In JDBC to represent the date, is to use java.sql.Date, its date format is "year, month, day", to indicate the time is the use of java.sql.Time, its time format is "hours, minutes, seconds", if you want to represent "hours, minutes, seconds, microseconds" format, You can use Java.sql.Timestamp. Second, JDBC Advanced 1. Use resultset to scroll, update data at resultset, the default is to use Next () to move the data cursor to the next data, and then use the GetXXX () method to get the data. The result set type can specify 3 settings: resultset.typeforwardonly (default), Resultset.typescrollinsensitive, resultset.typescrollsensitive. Update settings can be specified in two ways: resultset.concurreadonly (default),Resultset.concur_updatable. If you want to use resultset for data modification, there are some limitations: a single table must be selected, a primary key must be selected, and all values that are NOT NULL must be selected. 2. Batch update every execution executeupdate (), in fact, will send to the database once SQL, if a large number of updated SQL has 10,000 pen, it is equal to 10,000 times through the network of information transmission, network transmission information must actually open I/O, Routing and other actions. So it's best to have all of the SQL collected, and then string to a SQL, and then to the database, since the batch update, as the name implies, is only used on the update, so the limit of the batch update is that SQL cannot be a select, otherwise it throws an exception. 3.Blob and Clob If you want to write a document to a database, you can use a BLOB or CLOB data type on a database table field, and blobs are used to store large amounts of binary data, such as drawings, audio files, and so on, CLOB to store large amounts of literal data. 4. The four basic requirements of trading profile trading are atomicity, consistency, segregation behavior and continuity. What are the possible data inconsistencies that can arise when multiple transactions are in parallel? (1) Update lost: basically refers to a transaction to update the field information, due to the intervention of another transaction lost the effectiveness of the update. (2) Dirty read: When two transactions are in progress, one transaction updates the data but is not confirmed, and the other transaction reads the data, and a dirty read problem may occur. (3) cannot read repeatedly: A transaction two times the data in the same field is not consistent. (4) Phantom reading: The number of data strokes that are read is inconsistent during the same transaction. 5.metadata Introduction Metadata is the "data of the reading data", where the database is used to store data, but the database itself is the product name? How many data tables are in the database? What is the name of the table? How many fields are there in the table? These are all metadata. Introduction to 6.RowSet JDBC defines a java.sql.RowSet interface that represents a collection of columns of data, where the data is not necessarily data in the database, but can be spreadsheet data, XML data, or any data source with the concept of a column collection. Rowset defines the basic behavior of the column collection, which has the Jdbcrowset, CachedRowSet, Filteredrowset, Joinrowset, and Webrowset Five standard column set sub-interfaces. Jdbcrowset is an online rowset, that is, during the operation Jdbcrowset, is kept online with the database, can be considered as acquisition, Operation ResultSet behavior encapsulation, can simplify the composition of the JDBC program, or as a JavaBean use. The CachedRowSet is an offline rowset, which is queried andWhen the data is populated, it is disconnected from the data source without taking up the relevant online resources and, if necessary, the data source can be synchronized with data sources online. 17th 1. Reflection When we use a class, if the class has not yet been loaded into memory, the system initializes the class by loading, joining, initializing. Second, understand the class loader 1. Class loader hierarchy class loading refers to reading class files of classes into the JVM and creating a class object for it. Class join refers to merging the binary data of the class into the JRE, which is divided into 3 stages: (1) Check: Checking the correctness of loading class file data. (2) Preparation: Allocates storage space for static variables of the class and initializes it by default. (3) Parsing: Replace the symbolic reference in the binary data of the class with a direct reference. Initialization refers to the initialization of static variables and static initialization blocks of a class. Note: A static property of a final type, if the property value has been obtained at compile time, then calling this property does not cause the class to initialize because it is equivalent to using a constant. B. Using the ClassLoader () method, just load the class, not initialized. The ClassLoader is responsible for loading the. class file into memory and generating the corresponding Java.lang.Class object for it, which is responsible for loading all classes, and once a class is loaded into the JVM, it will not be loaded again. In Java, a class is identified with its fully qualified class name (that is, the package name + class name). The JVM runtime generates 3 ClassLoader: Bootstrapclassloader (Root ClassLoader), extclassloader (extension ClassLoader), and Appclassloader (System ClassLoader). Where Bootstrapclassloader is responsible for loading the JRE's core class library, which is not a subclass of ClassLoader, written in C + +, so we do not see it in Java, and when obtained by its subclass's GetParent () method, returns NULL, Bootstrapclassloader is responsible for loading Java core class libraries such as Rt.jar and Charsets.jar under the JRE target. 18th Chapter I, custom Generics 1. Generics before the JDK1.5 version, if different types of data are added to the collection, the conversion between types needs to be checked during the program's run. For example: List arraylist=new arrayList (); Add a String type Object and an integer object in ArrayList, but we define an array of the following string[] values=new string[ Arraylist.size ()]; If you want to convert ArrayList to an arrayError occurs: Arraylist.toarray (values);//run-time error. This is because the list contains an integer wrapper type object. However, this error can only be found during runtime, the program can be compiled normally, and will not error. For errors in the program, you should notify the programmer as soon as possible. Generics are a good way to solve this problem. Generics are applied in collections: Java provides support for generics after the JDK1.5 version. For example, for a ArrayList list, if you only want to put a string type element in the list, you can do so in the following way: ArrayList list=new ArrayList (); generics enable the compiler to check the objects that are added to the collection during compilation. If you add different types of objects, you will get an error, instead of having to wait for the related type conversions during the run. Compared with the original is not using generics, it will need to be in the runtime to find exceptions early to the compilation period, so that the security of the program is greatly improved, generics are generally used in the collection class. Ii. enumeration 1. Enumeration Type enumeration is also a new feature of Java after the JDK1.5 version. An enumeration can be used to represent a set of constant data. The essence of an enumeration is a class that declares an enumeration type by using the enum keyword, which is declared as follows: [Access control] enum enum type name {value1,value2,......} There are a few things to note about using enum types: Enum types can be defined inside a class or outside of a class. If defined inside a class, the access control can be either public,protected,private or the default control character. If defined outside the class, its access/ask control can only be public and the default control, and the value values defined in the enumeration type default to public static final. Once the value is defined, it cannot be modified. Multiple value values need to be separated by commas, and in addition to declaring a constant in an enumeration type, you can declare a method. However, the method needs to be followed by a constant, and the constants and methods are separated by semicolons; the values in the enumeration type can be accessed directly by enumerating the type names; enumeration types cannot be declared abstract or final type; 2. Common methods for enumeration types public String name () Returns the name of the enumeration constant; public final int ordinal ()//returns the position of the enumeration constant in the enumeration type, the first enumeration value ordinal is 0, and so on; public String toString ()//returns the name of the enumeration constant, you can override this method ; public static valueOf (ENUmtype,string name)//Returns the enumeration constants that correspond to the name names; note 1. Note Types use annotations in the source code, provide additional compilation hints to the compiler, or provide configuration information that can be read during application execution. Comments can be used only for the original code, and the. Class document is compiled to be read only by the compiler or during open execution time. 2. Common standard annotations @override//is a standard comment, the method that is annotated must be a method that is already defined in the parent class or interface, and compile the program to assist in really redefining the method. @Deprecated//If a method originally existed with the API and is not recommended for reuse later, you can comment on the method. If a user subsequently wants to invoke or redefine this method, the compiler will warn you. For APIs that support generics, it is recommended to explicitly specify a generic true type, and if not specified, the compiler warns. @SuppressWarnings//Specify a warning to suppress unchecked: @SuppressWarnings (value={"Unchecked"}) @SafeVarargs//indicates that the developer has determined to avoid the heap Pollution problem. The problem with heap pollution is that the compiler cannot check the execution period for type errors and cannot specifically confirm the argument type. @FunctionalInterface//Let the compiler assist in checking whether interface can be used as the target type of the lambda custom annotation type • Mark comments: Just the comment name itself is the information, for the compiler or application, the main is to check whether there are comments appear, and make the corresponding action. • Related rules: If the comment name itself does not provide enough information, setting the single-value Comment Comment property can also be specified as an array. When defining a comment property, if the property name is value, you can omit the property name and specify the value directly. Set default values for members and use the default keyword. To set the array default, you can add {} after default, and the element value can be placed in {} if necessary. When you define annotations, you can specify Java.lang.annotation.ElementType enumeration values when you use Java.lang.annotation.Target qualification. When you make a Javadoc file, the annotation data is not added to the file by default, and you can use java.lang.annotation.Documented if you want to add the comment data to the file. Annotations that are set by the default parent class are not inherited to subclasses, and when you define a comment, you set the java.lang.annotation.Inherited comment to allow the annotation quilt class to inherit. ElemenAn enumeration member of the Ttype is used to qualify which declaration position can be labeled. In JDK8, two enumeration members of type _parameter, type _use are added. Elementtype.type _ use can be used for labeling in various types, a label if it is set elementtype.type_use, as long as the type name, can be labeled. @Repeatable allows you to repeat the same tag @filters in the same place as a container for collecting duplicate callout information, and each @filters stores the string values that you specify. Read comment information during execution

Nineth Week Java Learning summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.