20145207 Java Programming 9th Week of study summary

Source: Internet
Author: User

Summary of learning contents of textbook

16th Chapter Integration Database

JDBC is a solution for executing SQL, the developer uses the standard interface of JDBC, the database vendor operates on the interface, and the developer does not have to touch the differences of the underlying database driver.
When the manufacturer operates the JDBC driver, the driver can be divided into 4 types by way:

1:jdbc-odbc Bridge Driver
2:native API Driver
3:jdbc-net Driver
4:native Protocol Driver

Get online and other database source-related behavior specifications in the Javax.sql.DataSource interface, the actual how to obtain connection is the object of the Operation interface is responsible.
Connection is the representative object of the database connection, and then the SQL is executed, the Java.sql.Statement object must be obtained, which is the representative object of the SQL description. You can use connection's createstatement () to create a statement object.

package cc.openhome;
 
Import  Static java.lang.System.out;
Import java.sql.*;
 
Public  Class ConnectionDemo {
Public  static  void  main( String[] args)
                              throws ClassNotFoundException, SQLException {
        Class.forName("Com.mysql.jdbc.Driver");
String  "Jdbc:mysql://localhost:3306/demo";
String  "Root";
String  "OpenHome";
Try
                DriverManager.getConnection(jdbcUrl, user, passwd)) {
            out.printf("%s Database online%n"
"Off" "  open");
    }
}

When using connection, statement, or resultset, turn it off to release the associated resources.

If some of the operations are just some of the arguments in the SQL statement will be different, the rest of the SQL clauses are the same, you can use Java.sql.PreparedStatement. You can use the connection PreparedStatement () method to establish a precompiled SQL statement, where the parameters will change, first specify the "? "This placeholder character. When you need to really specify the parameters to execute, then use the corresponding Setint (), setString (), and other methods, specify "? "Where the parameters are really supposed to be.

The 17th chapter reflection and Class loader

Java actually requires a class to load the corresponding. class document, rather than loading all classes at program startup. An instance of Java.lang.Class represents a. class document that is loaded when the Java application runs. The class object corresponding to each object can be obtained through the GetClass () method of object, or through a. Class constant, and if it is a base type, the class object can also be obtained by using the corresponding package classes plus. Type. For example: Integer.type can get a class object representing int.

If you do not know the class name beforehand, you can use Class.forName () to dynamically load the. class document, and then use its newInstance() method to build the class instance after the class object is obtained. java.lang.reflect.MethodAn instance is a representative object of a method that you can use invoke() to dynamically invoke the specified method.

package cc.openhome;
 
Static out  java.lang.System.;
 
Public  class  ClassInfo {
{
        Class clz = String.class;
Out .println( "category name:" + clz.getName());
Out .println( "is the interface:" + clz.isInterface());
Out .println( "is the basic form:" + clz.isPrimitive());
Out .println( "is an array of objects:" + clz.isArray());
Out .println( "Parent name:" + clz.getSuperclass().getName());
    }
}

A. Class document loaded by the same class loader will have only one class instance. If the same. class document is loaded by two different classloader, there will be two different class instances.

package cc.openhome;
 
Import static java.lang.System.out;
Import java.net.MalformedURLException;
Import java.net.URL;
Import java.net.URLClassLoader;
 
Public{
Public  static  void main( String[] args) {
Try {
String  path = args[ 0  //test path
            String  clzName = args[ 1  //test category
Class clz1 = loadClassFrom(path, clzName);
            out.println(clz1);
Class clz2 = loadClassFrom(path, clzName);
            out.println(clz2);
            out.printf("Clz1 and CLZ2 for%s",
"Same"  "different");
Catch  ( arrayindexoutofboundsexception e) {
            out.println ( "does not have the specified type to download in the path and the name");
Catch  ( malformedurlexception e) {
            out.println ( "Loading path wrong");
Catch  ( classnotfoundexception e) {
            out.println ( "The specified category was not found");
        }
    }
 
Private  static  Class  loadClassFrom( string  string clzName)
Throws  classnotfoundexception  malformedurlexception {
ClassLoader  urlclassloader  url  url(path)});
Return loader.loadClass(clzName);
    }
}

18th. Customizing generics, enumerations, and annotations

Comments

    • Note Type

Use annotations in the source code, provide additional compilation hints to the compiler, or provide configuration information that can be read at the time of 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.

    • Common standard Annotations

@Override//is the standard comment, the annotated method must be a method that is already defined in the parent class or interface, and the compiler assists in the process of actually 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:

(1) If the comment name itself cannot provide enough information, set a single-value comment

(2) Note Attributes can also be specified in an array form.

(3) When defining annotation properties, if the property name is value, you can omit the property name and specify the value directly.

(4) Set the default value for the member and use the Defaults keyword.

(5) To set the array default value, you can add {} after default, and you can place the element value 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.

Trading profile

    • With support for isolation behavior, JDBC can obtain the current isolation behavior of the database through connection's gettransactionisolation (), and through Settransactionisolation () can prompt the database to set the specified isolation behavior , the settable constants are defined on the connection
    • Do not set isolation behavior on transactions Transaction_none

Problems in teaching materials learning and the solving process

The content of this chapter is still relatively small, and there are many new things, but in terms of understanding, there is no previous conceptual knowledge difficult to understand, in the book code after a bit, for these things have a good understanding, but the book is still quite a bit of knowledge, which requires us to spend a lot of time to understand.

Think of the day after tomorrow can see the teacher, in the heart is quite excited, hehe.

By the right, do you still have to write a blog for the letter Ann class? I remember a 0th week summary? Some words I'll go back tomorrow to have a good look at the book.

20145207 Java Programming 9th Week of study summary

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.