Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java uses JDBC connect database     - Dom4j change XML coding (Programming)

- Linux shell script debugging (Linux)

- crontab cause CPU exception Analysis and Processing (Linux)

- Impact test noatime Linux file access time (Linux)

- Linux, rename the file or folder (mv command and rename command) (Linux)

- PostgreSQL Source Customization: Online global read only (Database)

- To remove those IP is prohibited Fail2ban on CentOS 6/7 (Server)

- Oracle LONG RAW BLOB CLOB type of presentation (Database)

- The method of MySQL two kinds of incomplete recovery (Database)

- iOS Sensor Development - add to APP phone password, fingerprint security authentication (Programming)

- Compiling source code Nginx module installation subs_filter (Server)

- Ubuntu install Avast antivirus software (Programming)

- Father of Python: Why Python zero-based index (Programming)

- Install Ubuntu Software Center App Grid (Linux)

- How to Install Android Studio on Ubuntu 15.04 / CentOS7 (Linux)

- Fast Sort Algorithms (Programming)

- Linux RPM (Linux)

- How to install and configure in Ubuntu 14.10 'Weather Information Indicator' (Linux)

- Use scripts easily install the latest Linux kernel in Ubuntu (Linux)

- To resolve Ubuntu 14.04 Unity Desktop Environment Login freeze problem (Linux)

  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.


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.


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.

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.


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 ();
- Linux System Getting Started Learning: Repair (Linux)
- Oracle procedure or function Empty Table (Database)
- Ubuntu 14.04 Enable root and disable the guest (Linux)
- Linux yum command Detailed (Linux)
- Vim useful plugin: YouCompleteMe (Linux)
- Oracle metadata Reconstruction experiments (Database)
- Fast Sort Algorithms (Programming)
- CentOS yum install LNMP PHP5.4 version (Server)
- Percona MySQL 5.6 semi-synchronous replication (Database)
- Linux file and directory permissions settings (Linux)
- Story timestamp and time zones: daily programmer (Programming)
- Copy files between two Linux hosts (Linux)
- Installation and use Docker under Ubuntu 14.04 (Linux)
- Android Studio and Git Git configuration file status (Linux)
- Summary of Docker mounted directory (Server)
- Machine Learning: Classification of the curse of dimensionality (Programming)
- Kitematic how seamless and DockerFILE (Server)
- Installation of JDK and Tomcat under Linux (CentOS) (Linux)
- PostgreSQL export data dictionary documents (Database)
- RHEL5 multipath configuration (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.