Java Learning Note-JDBC1 (16)

Source: Internet
Author: User
Tags getdate

1 , Data Persistence

L Persistence (Persistence): Saves data to a removable storage device for later use, also known as "curing". In most cases, the persistence of data in a server or client application is achieved through a relational database

L Storage Device: Disk, hard disk, USB drive, CD, etc.

L Storage Format: Database, XML file, TXT file, etc.

2 , knowledge point 2:java data storage technology in the

In Java, database access technology can be divided into the following categories:

L JDBC Direct access to database

L third-party O/R tools such as Hibernate, Ibatis, etc.

JDBC is the cornerstone of Java's access to databases, and other technologies are the encapsulation of JDBC

3 , what is JDBC

JDBC (Java database Connectivity) is a interface (a set of APIs) that is independent of a particular database management system, a common operational database, and defines a standard Java class library to access the database, and the implementation of the interface is done by each database vendor

L JDBC Driver (jar package): A set of implementation classes (jar packages) for the JDBC interface of the database vendor

4 , what is ODBC

ODBC (Open database Connectivity) is an integral part of the database in the Microsoft Open Services architecture, which establishes a set of specifications and provides a set of standard APIs for database access

5 , JDBC the classification

There are currently four types of JDBC drivers available, and different types of drivers are used differently, so when we connect to the database, we have to choose the appropriate driver according to our requirements, the four different types of drivers are:

1 JDBC-ODBC Bridge: Driver for bridge type,

2 part of the local API part Java driver, is also one of the bridge type driver

3 JDBC Network pure Java Driver

4 Native protocol-only Java driver: This type of driver is the most mature JDBC driver, not only does not need to install any additional drivers on the consumer computer, nor need to install any intermediary programs (middleware) on the server side, all access to the database operation, is done directly by the driver.

6. Introduction to JDBC API interface

7. Initial Java Experience

1. The database service is turned on. 2. You need to import the database driver.

JDBC Experience

 Public classTest1 { Public Static voidMain (string[] args) throws Exception {//1. Registering the driver (can be linked to the database)Drivermanager.registerdriver (NewDriver ()); //2. Get an object connected to the databaseConnection Con=drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/mydb","Root","Root"); //3. Get an Execute SQL ObjectStatement stm=con.createstatement (); //4. Execute the SQL statement. As an example of a query, a resultset result set is returned after execution. (In fact, it is the information that contains the query)ResultSet Rs=stm.executequery ("SELECT * from emp");//Check the table name//5. Iterate over the contents of the result set.         while(Rs.next ()) {Object obj=rs.getobject ("ename");//Check list nameSystem. out. println (obj); }        //6. Need to release resourcesRs.close ();        Stm.close ();    Con.close (); }}  

8, the above code for exception handling:

//JDBC Exception Handling Public classTest3 { Public Static voidMain (string[] args) {Connection con=NULL; Statement STM=NULL; ResultSet RS=NULL; Try{class.forname ("Com.mysql.jdbc.Driver"); Con=Drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/mydb","Root","Root"); STM=con.createstatement (); RS= Stm.executequery ("SELECT * from emp");  while(Rs.next ()) {String name= Rs.getstring ("ename"); intNo = Rs.getint ("empno"); Date Date= Rs.getdate ("HireDate"); System. out. println (name +" "+ No +" "+date); }        } Catch(ClassNotFoundException e) {e.printstacktrace (); Throw NewRuntimeException ("Load driver failed"); } Catch(SQLException e) {e.printstacktrace (); Throw NewRuntimeException ("SQL operation failed"); } finally {            Try {                if(rs! =NULL) Rs.close (); } Catch(SQLException e) {e.printstacktrace (); Throw NewRuntimeException ("Close the RS failed"); }            Try {                if(STM! =NULL) Stm.close (); } Catch(SQLException e) {e.printstacktrace (); Throw NewRuntimeException ("shutdown of STM failed"); }            Try {                if(Con! =NULL) Con.close (); } Catch(SQLException e) {e.printstacktrace (); Throw NewRuntimeException ("Close con failed"); }        }    }}

9, the above code optimization (1)

 Public classTest4 {@Test Public voidjdbctest () {//defined on the outside, while the process exception method is defined on the outsideString Driver ="Com.mysql.jdbc.Driver"; String URL="Jdbc:mysql://127.0.0.1:3306/mydb"; String username="Root"; String Password="Root"; Connection Con=NULL; Statement STM=NULL; ResultSet RS=NULL; Try{class.forname (driver); Con=drivermanager.getconnection (URL, username, password); STM=con.createstatement (); RS= Stm.executequery ("SELECT * from emp");  while(Rs.next ()) {String name= Rs.getstring ("ename"); intNo = Rs.getint ("empno"); Date Date= Rs.getdate ("HireDate"); System. out. println (name +" "+ No +" "+date); }        } Catch(ClassNotFoundException e) {e.printstacktrace (); Throw NewRuntimeException ("Load driver failed"); } Catch(SQLException e) {e.printstacktrace (); Throw NewRuntimeException ("SQL operation failed"); } finally{Closeresultset (RS);            Closestatment (STM);        CloseConnection (con); }    }    //Close Connection//off the statement//close the ResultSet method     Public voidcloseconnection (Connection con) {Try {            if(Con! =NULL) Con.close (); } Catch(SQLException e) {e.printstacktrace (); Throw NewRuntimeException ("Close con failed"); }    }     Public voidclosestatment (Statement stm) {Try {            if(STM! =NULL) Stm.close (); } Catch(SQLException e) {e.printstacktrace (); Throw NewRuntimeException ("shutdown of STM failed"); }    }     Public voidCloseresultset (ResultSet rs) {Try {            if(rs! =NULL) Rs.close (); } Catch(SQLException e) {e.printstacktrace (); Throw NewRuntimeException ("Close the RS failed"); }    }}

10 to optimize the above code again, define a mysqldb.properties configuration file .... Take a look at the next article

Java Learning Note-JDBC1 (16)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.