Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java uses JDBC connect database     - To learn from scratch OpenWrt perfect tutorial (Linux)

- 127.0.0.1 and localhost difference (Server)

- CentOS NAT iptables (Linux)

- C ++ implementation of the list of basic operations and test cases (Programming)

- Linux system started to learn: how to view the contents of the seed on the Linux file (Linux)

- Understanding Linux firewall Iptables (Linux)

- Manage SQL Server services login (start) account and password (Database)

- Android Studio commonly used shortcuts and how to follow the Eclipse Shortcuts (Linux)

- Ubuntu 14.04.02 LTS startup items erroneous writing / dev / sda1 (win 7 loader) Repair (Linux)

- To install JDK1.7 and compiler Hadoop-2.7.1 under CentOS7 (Server)

- A list of the basics of Python, Ganso, Dictionary (Programming)

- Use XtraBackup be physical standby database MySQL (Database)

- Using Python multithreaded mistakes summary (Programming)

- Oracle 11g upgrade PSU detailed steps (Database)

- ADSL router to defend their own network security methods (Linux)

- CentOS 6.5 start ActiveMQ being given to solve (Server)

- Cool Android realization SVG animation (Programming)

- Four Methods of Self - Learning Linux (Linux)

- Configuring Eclipse Note Templates (Linux)

- MySQL separation Amoeba achieve literacy (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:      
 
- SpringMVC the use of interceptors (Programming)
- JDK tools jstat (Linux)
- linux system optimization and security configuration (Linux)
- CentOS 6.4 Python 2.6 upgrade to 2.7 (Linux)
- CentOS6.7 text installation system (Linux)
- Linux System Getting Started tutorial: Ubuntu desktop using the command line to change the system proxy settings (Linux)
- Ubuntu install the camera driver (Linux)
- redis configuration in detail (English) (Database)
- Android to determine whether the device to open WIFI, GPRS data connection (Programming)
- Linux System Getting Started Learning: The Linux logrotate (Linux)
- When Linux Detailed time zone and common function of time (Linux)
- A detailed introduction to the Hadoop ecosystem (Server)
- Five useful commands to manage file types and system time in linux (Linux)
- Using PHP MySQL library (Programming)
- Port is not being used, how will bind failure? (Server)
- Why HBase need to build SQL engine layer (Database)
- Kafka + Log4j log implement centralized management (Server)
- Getting the Linux shell variable test (Programming)
- Disk partition MBR (Linux)
- Ubuntu 14.04 LTS 64-bit install GNS3 1.3.7 (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.