Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java uses JDBC connect database     - Oracle first Automated Installation Packages (Database)

- Using Linux / Unix Text processing (Linux)

- Compile and install LNMP under CentOS 6.5 (Server)

- Solve the compatibility problem between Linux and Java at the source in bold font (Linux)

- When Linux virtual machine to another copy of the operating system, a static IP NAT mode Invalid (Linux)

- Ubuntu 14.10 Apache installation and configuration (Server)

- Lua regex (string function) (Programming)

- Ubuntu disable graphics card (Linux)

- shellinabox: one uses AJAX Web-based terminal emulator (Linux)

- The Java way to stop a thread of execution (Programming)

- CentOS7 Kubernetes used on container management (Server)

- Summary Linux bond of multi-interface load balancing (Linux)

- Rely on IP chain established Linux firewall (Linux)

- Oracle rebuild index script (Database)

- Android HTTP request with Get Information (Programming)

- VMware installed Linux system and JDK deployment (Linux)

- MySQL 5.7 and 5.6 group by differences (Database)

- Configuring the remote Linux server SSH key authentication to automatically login in Mac OS X (Server)

- Linux host system monitoring implement the cgi (Server)

- MySQL Statistics (Database)

 
         
  Java uses JDBC connect database
     
  Add Date : 2018-11-21      
         
         
         
  Develop a JDBC application, the basic needs of the following steps:

1. The JDBC driver class is loaded into the JAVA virtual machine. Use the static method forName java.lang.Class class (String className) implementation.

Example: Class.forName ( "JDBC driver class name")

2. load the driver, and establishes a connection to the database. DriverManager class followed by a registered driver, when we call the getConnection () method, which will traverse the list of drivers until a match on the connection string to connect to the data specified in the driver database, after loading the driver, use getConnection method of the DriverManager class to establish a connection between the database.

example:

Connection con = DriverManager.getConnection (database connection string, database user name, password)
3. Send the SQL statement and get the result set. Create a Statement interface instance and SQL statements to the database it is connected.

 Statement instance is divided into three types:

 (1) executing a static SQL statements. Usually achieved by Statement instance.

 (2) execute dynamic SQL statements. Typically implemented by PreparedStatement instance.

 (3) the implementation of database stored procedures. Usually through CallableStatement instance.

example:

Statement stmt = con.createStatement ();
ResultSet rs = stmt.executeQuery ( "select * from table1");
 Statement interface provides three methods execute SQL statements: executeQuery, executeUpdate, execute statement.

ResultSet executeQuery (String sqlString): execute SQL statements to query the database and returns a result set (ResultSet) object.
int executeUpdate (String sqlString): used to execute INSERT, UPDATE, or DELETE statements as well as SQL DDL statements, such as: CREATE TABLE and DROP TABLE, etc.
execute (sqlString): used to perform return multiple result sets, count, or a combination of both multiple update statements.
  example:

ResultSet rs = stmt.executeQuery ( "SELECT * FROM ...");
int rows = stmt.executeUpdate ( "INSERT INTO ...");
boolean flag = stmt.execute (String sql);
 

4. results. Results are divided into two situations:

(1) to perform the update returns this number of records affected by the operation.

 Results (2) to execute the query returns a ResultSet object.

example:

while (rs.next ()) {
         int x = rs.getInt ( "a");
         String s = rs.getString ( "b");
         float f = rs.getFloat ( "c");
}
5. Close the JDBC objects

  After the operation is complete, close all JDBC objects to be used to release JDBC resources, closed in reverse order and declaration order.

 (1) off the record set

 (2) Close Statement

 (3) close the connection object

 if (rs! = null) {// set off the record
        try {
            rs.close ();
        } Catch (SQLException e) {
            e.printStackTrace ();
        }
          }
          if (stmt! = null) {// Close Statement
        try {
            stmt.close ();
        } Catch (SQLException e) {
            e.printStackTrace ();
        }
          }
          if (conn! = null) {// Close the connection object
         try {
            conn.close ();
         } Catch (SQLException e) {
            e.printStackTrace ();
         }
          }
     
         
         
         
  More:      
 
- Print Linux system error codes (Linux)
- systemd Power Management (Linux)
- Analysis examples: Intrusion Response Linux platform Case (Linux)
- ld.so.conf.d profile (Linux)
- The easiest 11g Active DataGuard (ADG) to establish the configuration process (Database)
- Commonly used Linux system camouflage method (Linux)
- Spark source code compiler package (Linux)
- MySQL tmpdir parameter modification (Database)
- iOS Sensor Development - add to APP phone password, fingerprint security authentication (Programming)
- Linux file permissions bit forced bits and adventure Comments (Linux)
- Use CutyCapt to convert HTML pages to png images on Linux (Linux)
- How to enable Software Collections (SCL) on CentOS (Server)
- C ++ function object (Programming)
- How to Upgrade Ubuntu GNOME 14.10 to GNOME 3.16 Desktop (Linux)
- Puppet 3.x installed on Debian 7 (Server)
- Elasticsearch 2.20 Highlight (Server)
- Web cache basics: terminology, HTTP headers, and caching policies (Server)
- Linux common network tools: ping host sweep (Linux)
- Definition Format Oracle basis of various statements (Database)
- To install and configure the Jetty server and JDK under Ubuntu 14.04.2 (Server)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.