Java Tiered Development

Source: Internet
Author: User

Since it is layered development, first we need to know is divided into those layers, and what is it?

1. A table in the physical layer (entity) corresponding to the database, with which it can reduce coupling and is also the carrier of the data.

2. Data Access object contains two Java source files, one is Basebao, and the other is the interface to query the table.

 PackageCn.news.dao;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importorg.junit.Test;/*** Data Access Tool class *@version1.1 *@authorHappy **/ Public classBasedao {Private Static FinalString driver= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; Private Static FinalString url= "jdbc:sqlserver://localhost:1433;databasename=s2223"; Private Static FinalString username= "SA"; Private Static FinalString pwd= "6375196"; Connection Con=NULL; PreparedStatement PS=NULL; ResultSet RS=NULL; //01. Write a method that the user obtains to a connection object, and the return value of the method is connection type   /*** 01. Write a method that the user obtains to a Connection object, the return value of the method is connection type *@returnConnection Object *@throwsException*/    PublicConnection getconnection ()throwsexception{Class.forName (driver); //under what conditions, build the Connection object       if(con==NULL||con.isclosed ()) {Con=drivermanager.getconnection (URL, username, pwd); }       //The comrades met a       returncon; }         //Unit Test//Java junit@Test Public voidTestquery ()throwsexception{ResultSet rs= ExecuteQuery ("SELECT * FROM Student"); if(rs!=NULL){            while(Rs.next ()) {String name= Rs.getstring ("sname");        SYSTEM.OUT.PRINTLN (name); }       }   }   //Test Add@Test Public voidTestupdate ()throwsexception{intcount= executeupdate ("Update student set Sname= ' Big strong ' where Sname= ' Huang Qiang '");   System.out.println (count); }            //perform query operation purpose: Returns a reader   /*** Perform query operation purpose: Return a reader *@paramSQL SQL statements *@paramOBJS parameter list *@returnReader Object *@throwsException*/    PublicResultSet executeQuery (String sql,object ... objs)throwsexception{con=getconnection (); PS=con.preparestatement (SQL);  for(inti = 0; i < objs.length; i++) {Ps.setobject (i+1, Objs[i]); } RS=Ps.executequery (); returnrs; }         //perform additions or deletions to this action   /*** Do not delete the operation *@paramSQL SQL statements *@paramOBJS parameter list *@returnnumber of rows affected *@throwsException*/    Public intExecuteupdate (String sql,object ... objs)throwsexception{con=getconnection (); PS=con.preparestatement (SQL);  for(inti = 0; i < objs.length; i++) {Ps.setobject (i+1, Objs[i]); }        intCount =ps.executeupdate (); returncount; }            //2. Reclaim Connected Resources  /*** Reclaim Connection Resources *@throwsException*/    Public voidCloseAll ()throwsexception{if(rs!=NULL) {rs.close (); }       if(ps!=NULL) {ps.close (); }       if(con!=NULL) {con.close (); }          }   }

3. The data Access layer

Java Tiered Development

Related Article

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.