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