Learning from the Hibernate framework (II): Basic JDBC operations

Source: Internet
Author: User

In my previous blog, "go deep and learn the Hibernate framework (I): start from an instance and get started with the Hibernate framework", I briefly introduced the Hibernate framework and gave an example to learn about hibernate. This blog will introduce the basic operations of JDBC. Speaking of JDBC, most programmers are already very familiar with it. For the sake of the serialization of hibernate learning, so in this article, we will briefly introduce the operations related to JDBC, because this is the foundation of hibernate implementation. Without JDBC, there will be no Hibernate framework, because the Hibernate framework is highly encapsulated for JDBC. This blog introduces how to establish a database configuration connection through JDBC, related classes and interfaces in JDBC, and the implementation of various JDBC operations.

 

It is hoped that readers will be able to study JDBC seriously. The basic things are the most important. Nothing else is the foundation of the cloud. Of course, this article is also the basis and foreshadowing for the future study of The Hibernate framework, I hope readers can learn it well.

 

JDBC is composed of a group of classes and interfaces written in Java. In specific development, JDBC provides a standard API that enables you to use Java APIs to compile database applications.

 

The following describes how to establish and configure a connection. Database connection is a prerequisite for JDBC queries. After development, we all know that mainstream databases now provide their own dedicated database connection drivers, with the database connection driver, it has brought us a lot of help, but it has also brought us a lot of trouble, such as version incompatibility and so on.

 

The following uses MySQL as an example to establish and configure a connection:

 

1. Add the driver. Copy the MySQL JDBC driver to the directory that can be found by the running program.

2. Make sure that the MySQL database is in the running state (the first two steps are silly operations which will not be described here)

3. Compile the JDBC program (the source code is provided here)

Source code:

Import Java. SQL. connection; <br/> Import Java. SQL. drivermanager; <br/> Import Java. SQL. sqlexception; </P> <p> Public classdbutil {</P> <p>/** <br/> * Get connection <br/> * @ return <br/> */ <br/> Public static connection getconnection () {</P> <p> connectionconn = NULL; <br/> try {</P> <p> class. forname ("com. mySQL. JDBC. driver "); <br/> stringurl =" JDBC: mysql: // localhost: 3306/user "; <br/> stringusername =" root "; <br/> stringpassword = "123"; <br/> conn = drivermanager. getconnection (URL, username, password); <br/>} catch (classnotfoundexception e) {<br/> E. printstacktrace (); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/>}< br/> return conn; <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> system. out. println ("test started"); <br/> system. out. println (dbutil. getconnection (); <br/> system. out. println ("test ended"); <br/>}< br/>}

 

Result:

 


The following describes the JDBC interface:

The JDBC interface is a public access method provided by database operations. Using these methods can simplify database operations. The JDBC interface is placed in the Java. SQL package of JDK.

:

 


Here we will introduce several more reusable and important interfaces and a drivermanager class.

 


 

The previous sections are boring concepts and theories. Let's take a look at how JDBC uses these interfaces and classes to implement various operations.

 

Statement and preparedstatement:

The statement interface represents the status of a database. These two interfaces are used when sending corresponding SQL statements to the database. While statement is an SQL statement without parameters, and preparedstatement can operate with or without parameters. Therefore, preparedstatement avoids SQL injection.

 

Example:

Statement source code:

Import Java. SQL. connection; <br/> Import Java. SQL. sqlexception; <br/> Import Java. SQL. statement; </P> <p> public class statementtest {</P> <p> Public static void main (string [] ARGs) throws sqlexception {<br/> string SQL = "insert into user (name, password, ID) values ('1', '2', '123 ')"; <br/> connection conn = dbutil. getconnection (); <br/> statement stmt = Conn. createstatement (); <br/> stmt.exe cuteupdate (SQL); <br/> system. out. println ("executed successfully"); </P> <p >}</P> <p>}


Source code of preparedstatement:

Import Java. SQL. connection; <br/> Import Java. SQL. preparedstatement; <br/> Import Java. SQL. sqlexception; <br/> Import Java. SQL. statement; </P> <p> public class preparedstatementtest {</P> <p> Public static void main (string [] ARGs) throws sqlexception {<br/> string SQL = "insert into user (name, password, ID) values (?,?,?) "; <Br/> connection conn = dbutil. getconnection (); <br/> preparedstatement pstmt = Conn. preparestatement (SQL); <br/> pstmt. setstring (1, "1"); <br/> pstmt. setstring (2, "2222"); <br/> pstmt. setstring (3, "3333"); <br/> pstmt.exe cuteupdate (); <br/> system. out. println ("executed successfully"); </P> <p >}</P> <p>}

 

Resultset

The resultset interface is the query result set interface that processes the returned result set.

Example:

Source code:

Import Java. SQL. connection; <br/> Import Java. SQL. preparedstatement; <br/> Import Java. SQL. resultset; <br/> Import Java. SQL. sqlexception; </P> <p> public class resultsettest {</P> <p> Public static void main (string [] ARGs) throws sqlexception {<br/> string SQL = "select * from user where id =? "; <Br/> connection conn = dbutil. getconnection (); <br/> preparedstatement pstmt = Conn. preparestatement (SQL); <br/> pstmt. setstring (1, "11111"); <br/> resultset rs1_pstmt.exe cutequery (); <br/> If (RS. next () {<br/> system. out. println ("name =" + Rs. getstring ("name"); <br/> system. out. println ("Password =" + Rs. getstring ("password"); <br/>}</P> <p>}

Result:

 

 

Resultsetmetadata

The resultsetmetadata interface can traverse the attributes of each field in the database in the form of arrays. This mechanism is of great significance to developers.

Example:

Source code:

Import Java. SQL. connection; <br/> Import Java. SQL. resultset; <br/> Import Java. SQL. resultsetmetadata; <br/> Import Java. SQL. sqlexception; <br/> Import Java. SQL. statement; </P> <p> public class resultsetmetadatatest {</P> <p> Public static void main (string [] ARGs) throws sqlexception {<br/> string SQL = "select * from user order by ID"; <br/> connection conn = dbutil. getconnection (); <br/> // obtain the type that can be rolled back and forth. <br/> statement stmt = Conn. createstatement (resultset. type_scroll_insensitive, resultset. concur_updatable); </P> <p> resultset rs1_stmt.exe cutequery (SQL); <br/> resultsetmetadata RSM = Rs. getmetadata (); <br/> system. out. println ("co-obtained" + RSM. getcolumncount () + "column information"); <br/> for (INT I = 1; I <RSM. getcolumncount (); I ++) {<br/> system. out. println ("column" + I + "is named" + RSM. getcolumnname (I) + "type:" + RSM. getcolumntypename (I); </P> <p >}</P> <p>}


Result:

 

 

Summary:

This blog briefly analyzes some important JDBC and JDBC interfaces to help readers understand JDBC and learn about hibernate in the future, hibernate operations are ultimately converted to JDBC operations. So the significance of this blog is to learn more about it later. After learning JDBC, I will introduce the Java reflection mechanism in the next blog, because this knowledge will be of great help to further study hibernate. Coming soon!

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.