How to use JDBC to implement the data Access object layer (DAO)

Source: Internet
Author: User

Java is an object-oriented language, and developers are often more accustomed to face a particular type of object when manipulating data, such as a user being a
The object of the user class. What the DAO layer needs to do is to provide the upper level with sufficient object support, so that the upper layer can no longer see the specific data, but a living object.

Add, delete, query, and modify operations are the most basic 4 things that DAO needs to do. Queries generally need to provide traversal queries and ID queries, and for traversal queries, DAO needs to provide
The user generic List object, which provides the user object with the data already assembled for the ID query, and for addition and modification operations, the upper layer typically provides a user object, which DAO
The data in the user object is inserted into the table using the INSERT statement. The delete operation simply provides an ID

 Class User{private Long id;private string name;private string gender;  Public User () {super (); The public User (long id,string name,string gender) {super (); this.id = Id;this.name = Name;this.gender = gender;} Get,set Method}//dao Class public class Jdbcdao{static{try{class.forname ("Com.mysql.jdbc.Driver");} catch (Exception e) {e.printstacktrace ();}} Private Connection Getconn () {Try{return drivermanager.getconnection ("Jdbc:mysql://localhost:3306:xe", "Root", " Password ");} catch (Exception e) {e.printstacktrace ();}} return null;} private void Release (ResultSet rs,statement ps,connection conn) {if (rs!=null) {try{rs.close ();} catch (Exception e) {e.printstacktrace ();}} if (ps!=null) {try{ps.close ();} catch (Exception e) {e.printstacktrace ();}} if (conn!=null) {try{conn.close ();} catch (Exception e) {e.printstacktrace ();}}} Get the user object with ID public Getuserbyid (long id) {ResultSet rs = null; PreparedStatement PS = null; Connection conn = null; String sql = "SELECT * from user where id =?"; Try{conn = This.getconnection ();p s = COnn.preparestatement (SQL);p S.setlong (1,id); rs = Ps.executequery (); if (Rs.next ()) {//if present, build and return user object directly User (Rs.getlong ("id"), rs.getstring ("name"), Rs.getstring ("gender"), return user;}} catch (Exception e) {e.printstacktrace ();} Finally{this.release (rs,ps,conn);} return null;} Query all Users public list<user> getAllUsers () {list<user> List = new arraylist<user> (); ResultSet rs = null; PreparedStatement PS = null; Connection conn = null; String sql = "SELECT * from user"; try{conn = This.getconnection ();p s = conn.preparestatement (sql); rs = Ps.executequery (); Loop add User Object while (Rs.next ()) {User user = new User (Rs.getlong ("id"), rs.getstring ("name"), Rs.getstring ("gender")); List.add (user);}} catch (Exception e) {e.printstacktrace ();} Finally{this.release (rs,ps,conn);} return list;} Modify user Data Public user updateUser (user user) {PreparedStatement PS = null; Connection conn = null; String sql = "Update user set ID =?,name=?,gender=?"; Try{conn = This.getconnection (); Conn.setautocommit (false);p S = conN.preparestatement (SQL);p S.setlong (1,user.getid ());p s.setstring (2,user.getname ());p s.setstring (3, User.getgender ()); int rst = Ps.executeupdate (); if (rst>0) {return new User (User.getid (), User.getname (), User.getgender ()); }conn.commit ();} catch (Exception e) {e.printstacktrace (); Try{conn.rollback ();} catch (Exception E1) {e1.printstacktrace ();}} Finally{this.release (null,ps,conn);} return null;}} Delete User Data public Boolean deleteuser (long id) {PreparedStatement PS = null; Connection conn = null; String sql = "Delete from user where id =?; Try{conn = This.getconnection (); Conn.setautocommit (false);p s = conn.preparestatement (sql);p S.setlong (1,user.getid ( ));p s.setstring (2,user.getname ());p s.setstring (3,user.getgender ()), int rst = Ps.executeupdate (), if (rst>0) { return user;} Conn.commit ();} catch (Exception e) {e.printstacktrace (); Try{conn.rollback ();} catch (Exception E1) {e1.printstacktrace ();}} Finally{this.release (null,ps,conn);} return null;}} Insert user Data public insertuser (user user) {PreparedStatement PS = null; Connection conn = null; String sql = "INSERT into user values (?,?,?)"; Try{conn = This.getconnection (); Conn.setautocommit (false);p s = conn.preparestatement (sql);p S.setlong (1,user.getid ( ));p s.setstring (2,user.getname ());p s.setstring (3,user.getgender ()), int rst = Ps.executeupdate (), if (rst>0) { return user;} Conn.commit ();} catch (Exception e) {e.printstacktrace (); Try{conn.rollback ();} catch (Exception E1) {e1.printstacktrace ();}} Finally{this.release (null,ps,conn);}  return null;}}} }

  

How to use JDBC to implement the data Access object layer (DAO)

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.