Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the operation of adding, deleting, modifying and searching JDBC in java

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Xiaobian to share with you how to achieve JDBC add, delete and change operations in java, I hope you have gained something after reading this article, let's discuss it together!

Let's go through the steps first:

Statement is not recommended here for two reasons:

1. There is a string operation, cumbersome.

2. There is a sql injection problem.

Who should we replace the Statement?

I. Addition, deletion and modification operations

Before we do that, let's solve the above problem first. We use Prepared Statement instead of Statement.

1.1 PreparedStatement Introduction

You can get the PreparedStatement object by calling the preparedStatement(String sql) method of the Connection object

The PreparedStatement interface is a subinterface of Statement that represents a precompiled SQL statement.

Parameters in SQL statements represented by the Prepared Statement object are marked with question marks (?) To indicate, call the setXxx() method of the PreparedStatement object to set these parameters. The setXxx() method takes two parameters, the first parameter is the index (starting with 1) of the parameter in the SQL statement to be set, and the second parameter is the value of the parameter in the SQL statement to be set.

Advantages:

Solve string spelling problem, prevent sql injection to improve performance

1.2 add, delete and modify operation

Here is given a general addition and deletion for different tables to change the operation, take the direct use of it.

//General add, delete and modify operations public boolean updateInfo(String sql,Object... args){ Connection conn = null; PreparedStatement ps = null; boolean b = true; try { //1. Create a connection conn = JDBCUtils.getConnection(); //2. Precompiled sql statement ps = conn.prepareStatement(sql); //3. Fill placeholder for (int i = 0; i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report