Beginner benefits: teach you how to quickly get started with JDBC

Hello everyone!

JDBC is the foundation stone for accessing databases. Frameworks such as JDO, Hibernate, and MyBatis just encapsulate JDBC better.

Therefore, if you want to use the framework better and understand it better, you still have to start with the basics of JDBC.

First of all, let's see why there is JDBC?

Concept: Java Database Connectivity (Java Database Connectivity, referred to as JDBC) is an application program interface used in the Java language to regulate how client programs access the database , and provides methods such as querying and updating data in the database.

Then, it means that when you use JDBC, you can add, delete, modify and check data in the database through the Java language to complete the data requirements you want.

Next, you can use the following figure to understand how java applications and databases are connected to each other:

From the figure, we find that Java applications do not directly interact with the database, but complete data-related operations through the "middleman" of JDBC. So why do we need this "middleman" ?

From the analysis of the JDBC API, we found that there are two levels, one is for application APIs, and for use by Java application developers; the other is for database APIs, and for developers to develop database drivers.

So, this will mean that we Java programmers only need to master the application-oriented API, can interact with the databases of different businesses, greatly reducing the learning cost of developers. At the same time, as long as different database developers can follow the JDBC-oriented database API, the versatility of their own databases will be greatly improved.

For example, if you need to rent a house now, if there is no real estate agent, it means that you will go to different places to see one by one, and it will take a lot of time. Moreover, the owner of the renting house can’t let you change the house if the house is empty. Many people know. Then, if there is this middleman, the buyer only needs to tell the individual's requirements for the location of the house, price and other requirements, and the middleman will directly provide matching rental houses. The seller also only needs to authorize the personal housing information to the middleman, and wait until the transaction is completed, without spending too much effort in the process.

Therefore, in order for Java applications to access the database better and more efficiently, JDBC is essential.

Next, let's take a look at the steps to use JDBC

First of all, the first step, this thing is provided to us by others, so don't forget to guide the package.

If maven is not used, manually import the package, copy mysql-connector-java-5.1.37-bin.jar under the idea project, and add it under the library:

Just as follows:

Secondly, the second step is to register the database driver

Class.forName("com.mysql.jdbc.Driver");

Note: The parameter here is the Driver in the jdbc package under com in the mysql-connector-java-5.1.37-bin.jar package that I imported just now

Entering Driver, we found that in fact, driver registration is realized through DriverManager.

The DriverManager class is a driver manager class, responsible for managing drivers

Next, the third step is to get the connection object

        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获得连接对象
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";//个人数据库用户名
        String password = "root";//个人数据库密码
        DriverManager.getConnection(url,user,password);

It’s worth talking about the meaning of String url = "jdbc:mysql://localhost:3306/test"

Then, the fourth step is to obtain the Statement object to implement CRUD operations

        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获得连接
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "root";
        Connection con = DriverManager.getConnection(url, user, password);
        //获得Statement实现CRUD操作
        //注意:要防范 SQL 注入,只要用PreparedStatement(从Statement扩展而来)取代Statement 
        //关于注入SQL注入问题,下一个专题专门讲
        Statement sta = con.createStatement();

Finally, the fifth step, through the Statement object just now, execute the SQL statement to manipulate the data (here only for the addition, deletion and modification of the database)

Here is an example to complete the modification of database data

Requirement: Use JDBC to change the monitor of the table to the deputy monitor

        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获得连接
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "root";
        Connection con = DriverManager.getConnection(url, user, password);
        //获得PreparedStatement实现CRUD操作
        注意:要防范 SQL 注入,只要用 PreparedStatement(从Statement扩展而来)取代Statement
        Statement sta = con.createStatement();
        //定义Sql语句
        String sql = "UPDATE sys_role SET roleName='副班长' WHERE id =6";
        //执行sql语句
        sta.executeUpdate(sql);
        //关闭资源
        sta.close();
        con.close();

operation result:

Well, the above is a quick start operation process about JDBC. This is for the beginners who are just learning JDBC. We have not covered many details, including some exception handling, using PreparedStatement to prevent sql injection, and database data Query operations, etc., we will gradually expand the topic on these details one by one. See you in the next issue!

 

Guess you like

Origin blog.csdn.net/Study_every/article/details/114930324