Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java uses JDBC connect database     - Squid proxy server configuration under Linux (Server)

- echo command (Linux)

- JavaScript notes --Objects (Programming)

- Install Ruby on Rails in Ubuntu 15.04 in (Linux)

- grep command usage (Linux)

- mysqldump issue a note (Database)

- Oracle Data Pump Example (Database)

- Github Getting Started Basic Course (Linux)

- Talk about the Linux folder permissions issue again (Linux)

- QEMU image file used: qcow2 and raw (Linux)

- Vi syntax highlighting settings (Linux)

- Mac OS X 10.9 compiler OCI8 module (Programming)

- Ubuntu Tutorial: How to Upgrade a New Linux Kernel 3.12.7 on Ubuntu (Linux)

- WinSCP to transfer text files will automatically convert the format (Linux)

- expdp reported ORA-39181 Export Processing Method (Database)

- Ubuntu How to install screen recording tool Simple Screen Recorder 0.3.1 (Linux)

- VirtualBox - Linux system virtual machine shared folders (Linux)

- RealVNC Server 5.2.3 Installation and Configuration In Fedora (Server)

- Cobbler remotely install CentOS system (Linux)

- Linux file and directory management - ls, cp, mv (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 ();
- Tip: Use Cryptsetup U disk encryption (Linux)
- Lucene Getting Started Tutorial (Server)
- Linux atomic operations and synchronization mechanisms (Programming)
- Sublime Text Add instructions to insert the current time zone (Linux)
- Android Studio quick overview of Gradle (Programming)
- Linux NIC driver and version information (Linux)
- Copy Recovery using RMAN repository development environment (Database)
- MySQL dual master configuration (Database)
- Android Send HTTP POST requests (Programming)
- Extended use of the swap file swap space on Linux (Linux)
- MySQL multi-instance configuration (Database)
- MySQL my.cnf sql_mode schema modifications (Database)
- Installation Elementary OS Freya 20 things to do (Linux)
- Java foundation comb: Array (Programming)
- Linux Getting Started Tutorial: / var / spool / clientmqueue fill the root directory (Linux)
- Ubuntu and Archlinux install Notepadqq 0.50.2 (Linux)
- Binding multiple network cards in Linux using command nmcli (Linux)
- Proficient in C / C ++ can also find a very good job (Programming)
- CentOS7 installed VMware 10 (Linux)
- Swift rewrite initialize method of navigation controller class (Programming)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.