The preambular database, the programmer will always face the development link. Its importance is self-evident. I have learned from learning VB, Delphi, and then the technology of Java, the database is followed, ODBC, ADO, BDE, JDBC, JDO, Hibernate, and there are many. Remember that in graphics programming, most of the use of controls, such as DataSource, very convenient. Java is all Class's style, still reflected in the database. I started to learn these database technologies today and write some notes. Today launched the most familiar JDBC a JDBC (Java Database Connectivity)
Is JDBC old in front of an object-oriented database? Do we need to learn JDBC today? As a newcomer, you may ask this. First of all, the qualification of the relational database is unattended in the rivers and lakes, even if it is eating old, we must use the database now, this is the fact. Second, JDBC is an excellent guy in the technology of accessing the relational database. Because it can not only support the many kinds of databases we often use (ie, unrelated to the supplier), but also cross-platform development (ie, unrelated to the platform). Most importantly, JDBC development is very simple, and it is also very common. Different database vendors provide a specific driver for JDBC, so we started the first step in JDBC to face DRIVER development. JDBC DREVER - DRIVERMANAGER
Load: 1 Use the -Dproperty = value parameter Parameters Transfer command line options to Java application java -djdbc.drivers = jdbc.odbc.jdbcodbdriver querydb
2 Use the static method for the Class class forname () class.forname ("sun.jdbc.odbc.jdbcodbcdriver"); class.forname ("com.imaginary.sql.msql.msqldriver"); // msql
Registration: Oracle Driver Example Class.Forname ("Oracle.jdbc.driver.OracleDriver"); java.sql.driverManager.RegisterDriver (New Oracle.jdbc.driveroracledriver ());
Open: Connection DriverManager.getConnection (URL, UserId, Password); conifline (); URL general form: JDBC: Appropriate Subject: // Driver Specific Components (Machine Name, Port, Database Name); JDBC: Proper sub-protocol: @ driver specific components (machine name, port, database name);
Get data: string sql = "select * from onetable"; statement statement = con.createstatement (); resultset results = statement.executeQuery (SQL); statement.colse ();
Obtain dynamic SQL data :: Stringsql = select name from idtable where id = ";? PreparedStatement prepStatement = con.prepareStatement (sql); prepStatement.setString (1, a_id); ResultSet rs = prepStmt.executeQuery ();
Rowset independent processing problem: string sql = "select * from oneTable"; rowset = new cachedrowset (); // jdbcrowset needs and database permanent connection, CachedRowSet does not need rs.setCommand (SQL); RS.SetURL (URL); RS .SetUserName (UserID); rs.SetPassword (Password); rs.execute (); ... rs.close (); Results ResultSet Can Scroll: Does the JDBCS used by the DatabaseMetadata object to support scrollable Resultset, it a method for testing a supportsReseltSetType: DatabaseMetaData dbMD = connection.gtMetaData (); Boolean okiedoie = md.supportsResultSetType (ResultSet.TYPE_SCROLL_SENSITIVE); if (okiedoie) {Statement statement = con.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);} TYPE_SCROLL_SENSITIVE Indicates that the ResultSet can scroll and sensitive to the database changes caused by other objects. Concur_UPDATABLE indicates that the Resultset can be updated in the result set.
JDBC metadata metadata is data about data. The DateBaseMetadata object provides information about the structure of a particular database. ResultSetmetadata
When the JDBC data source is developed using Driver mode, the URL is written, and many inconveniences bring to maintenance. In order to solve this problem, JDBC 2.0 uses JNDI mapping the actual data source - DataSource.
Let's introduce JNDI before contacting the data source. JAVA NAMING AND DIRECTORY INTERFACE. It is designed to standardize access to directory and naming services. These services include: NIS / NIS , LDAP (LightWeight Directory Protocol, RMI (Java Remote Method Invocation) REIREGISTRY, CORBA Common Object Service (COS) Name Service NDS, DNS, Windows Registry, etc.
Naming Service is a basic feature in your computer system. Naming service is associated with an object in the name and computer, which can easily find the corresponding object through the name. For example, a file system in a computer contains a naming service, you can find the corresponding file object via the file name. Directory Service is an extension of the naming service. The directory service does not only save the object and object name, and save the various properties of this object, you can query, modify, increase, and delete operations on these properties. The JNDI structure includes two sets of API: JNDI API (Application Programming Interface), and Java applications accesses a variety of named directory services through JNDI API. JNDI SPI (Service Provider Interface), JNDI SPI enables a variety of named directory services to be transparently added to the JNDI structure.
JNDI Context Context is a collection of object bindings that provide solutions (ie, returning to objects). Other operations include: Name binding and cancel binding, listing the bounded names. Note that the name of a context object can be bound to another context object with the same naming convention. This is called the sub-context. Context provides the following main interface access naming service: bind (name name, object obj) establishes a matching relationship named an object, also called the binding lookup (String Name) to find its corresponding object rebind (Name Name, Object Obj Re-bound a named object, override the original binding unbind (name name) release this binding relationship to which it matches the match ... In order to acquire or resolve the object from the naming service or directory, Use the lookup method of context: Object obj = contxt.lookup (name). JDBC Data Source - DataSource Configuration Data Source (Tomcat as an Example) Server.xml:
Web.xml: