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 use DBUtils to add, delete, modify and check JavaWeb

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "JavaWeb how to use DBUtils to achieve add, delete, change and query", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "JavaWeb how to use DBUtils to achieve add, delete, change and query" bar!

JavaWeb uses DBUtils to add, delete, modify and query 1. Create C3p0Utils class

Create a cn.itcast.jdbc.utils package

The code is as follows:

Package cn.itcast.jdbc.utils;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class C3p0Utils {private static DataSource ds; static {ds = new ComboPooledDataSource ();} public static DataSource getDataSource () {return ds;}} 2, create the DBUtilsDao class

Under the src directory, create a package for cn.itcast.jdbc.demo and a DBUtilsDao class under that package

The code is as follows:

Package cn.itcast.jdbc.demo;import java.sql.SQLException;import java.util.List;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import cn.itcast.chapter10.example.User;import cn.itcast.jdbc.utils.C3p0Utils Public class DBUtilsDao {/ / query all, return List collection public List findAll () throws SQLException {/ / create QueryRunner object QueryRunner runner = new QueryRunner (C3p0Utils.getDataSource ()); / / write SQL statement String sql = "select * from user" / / call method List list = (List) runner.query (sql, new BeanListHandler (User.class)); return list } / / query single, return object public User find (int id) throws SQLException {/ / create QueryRunner object QueryRunner runner = new QueryRunner (C3p0Utils.getDataSource ()); / / write SQL statement String sql = "select * from user where id=?" / / call method User user = (User) runner.query (sql, new BeanHandler (User.class), new Object [] {id}); return user } / / add user's action public Boolean insert (User user) throws SQLException {/ / create QueryRunner object QueryRunner runner = new QueryRunner (C3p0Utils.getDataSource ()); / / write SQL statement String sql = "insert into user (name,password) values (?,?)" / / call method int num = runner.update (sql, new Object [] {user.getName (), user.getPassword ()}); if (num > 0) return true; return false } / / modify user's operation public Boolean update (User user) throws SQLException {/ / create QueryRunner object QueryRunner runner = new QueryRunner (C3p0Utils.getDataSource ()); / / write SQL statement String sql = "update user set name=?,password=? Where id=? "; / / call method int num = runner.update (sql, new Object [] {user.getName (), user.getPassword (), user.getId ()}); if (num > 0) return true; return false } / / delete user's action public Boolean delete (int id) throws SQLException {/ / create QueryRunner object QueryRunner runner = new QueryRunner (C3p0Utils.getDataSource ()); / / write SQL statement String sql = "delete from user where id=?" / / call method int num = runner.update (sql, id); if (num > 0) return true; return false;}} 3, create test class

Create a test class DBUtilsDaoTest class in the cn.itcast.jdbc.demo package

The code is as follows:

Package cn.itcast.jdbc.demo;import java.sql.SQLException;import java.util.List;import cn.itcast.chapter10.example.User;public class DBUtilsDaoTest1 {private static DBUtilsDao dao = new DBUtilsDao (); public static void testInsert () throws SQLException {User user = new User (); user.setName ("zhaoliu"); user.setPassword ("666666") Boolean b = dao.insert (user); System.out.println ("testInsert:" + b);} public static void testupdate () throws SQLException {User user = new User (); user.setName ("zhaoqi"); user.setPassword ("666777"); user.setId (1) Boolean b = dao.update (user); System.out.println ("testupdate:" + b);} public static void testdelete () throws SQLException {boolean b = dao.delete (4); System.out.println ("testdelete:" + b) } public static void testfindById () throws SQLException {User user = dao.find (2); System.out.println (user.getId () + "," + user.getName () + "," + user.getPassword ()) } public static void testfindAll () throws SQLException {List list = dao.findAll (); for (User user: list) {System.out.println (user.getId () + "," + user.getName () + "," + user.getPassword ()) } public static void main (String [] args) throws SQLException {testInsert (); testupdate (); testdelete (); testfindById (); testfindAll ();}}

The above code consists of several test functions, which in turn are: insert, modify, delete, query according to id, query all

4. Execute the test class

1. The raw data of data sheet user is as follows:

The results are as follows:

Both inserts and deletions are for the fourth data, so they are not shown.

Access to database DBUtils by Java DBUtils technology

Dbutils is a component that operates the database. By secondary encapsulation of the classes that operate the database, the result set can be transformed into List.

Introduction

Compared with the previous mode of connecting to the database to get the result set, DBUtils has more concise code and faster access. Here I do a simple example of a Cuisine table that I designed by myself.

The code for the query statement against the database

Search the existing cuisine table Cuisine for all the data of the corresponding cuisine number cuid.

1. Entity class of cuisine table

/ / entity class public class Cuisine {private static final long serialVersionUID = 1L; private int cuid; private String cuname; public Cuisine () {super (); / / TODO Auto-generated constructor stub this.cuid = 0; this.cuname = "" of the cuisine table } public Cuisine (int cuid, String cuname) {super (); this.cuid = cuid; this.cuname = cuname;} public int getCuid () {return cuid;} public void setCuid (int cuid) {this.cuid = cuid } public String getCuname () {return cuname;} public void setCuname (String cuname) {this.cuname = cuname;} @ Override public String toString () {return "Cuisine [cuid=" + cuid + ", cuname=" + cuname + "]";}}

2. The method of realizing data query.

/ / implement data query public Cuisine getCuisine (Cuisine cu) {/ / get the corresponding cuisine information / / TODO Auto-generated method stub QueryRunner queryRunner = new QueryRunner (); if (cu.getCuid ()! = 0) {String sql = "select * from cuisine where cuid =?" Try {return queryRunner.query (DBUtilsPro.getConnection (), sql,cu.getCuid (), new BeanHandler (Cuisine.class));} catch (SQLException e) {/ / TODO Auto-generated catch block e1.printStackTrace () }} return null;} Thank you for your reading. The above is the content of "how JavaWeb uses DBUtils to add, delete, modify and query". After the study of this article, I believe you have a deeper understanding of how JavaWeb uses DBUtils to add, delete, modify and query, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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