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 **/PublicClassBasedao {PrivateStaticFinal String driver= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";PrivateStaticFinal String url= "jdbc:sqlserver://localhost:1433;databasename=s2223";PrivateStaticFinal String username= "sa";PrivateStaticFinal String 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*/Public Connection getconnection ()Throwsexception{Class.forName (driver);//Under what conditions, build the Connection objectif (con==null| |Con.isclosed ()) {con=Drivermanager.getconnection (URL, username, pwd); }//The comrades met aReturnCon }//Unit Test//Java JUnit@TestPublicvoid Testquery ()Throwsexception{ResultSet rs = executeQuery ("SELECT * FROM Student");if (rs!=Null){While(Rs.next ()) {String name = rs.getstring ("sname"); SYSTEM.OUT.PRINTLN (name); } } }//Test add@TestPublicvoid Testupdate ()Throwsexception{int count= 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*/Public ResultSet executeQuery (String sql,object ... objs)Throwsexception{con=Getconnection (); PS =Con.preparestatement (SQL);for (int i = 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*/Publicint executeupdate (String sql,object ... objs)Throwsexception{con=Getconnection (); PS =Con.preparestatement (SQL);for (int i = 0; i < objs.length; i++) {Ps.setobject (i+1, Objs[i]); }int count =Ps.executeupdate ();Return//2. Reclaim Connection Resources /** * Reclaim Connection resources * @throws Exception */public void CloseAll () throws exception{if (Rs!=nullif (Ps!=nullif (Con!=null
Layered development of Java