After two years of learning. net, its three-tier architecture is very useful. Now, we can use a three-tier architecture to develop JSP. The following details
1) Create a database
Drop table myuser
Create Table myuser
(
Id int identity (1, 1) not null,
Name varchar (50 ),
Psw varchar (50 ),
Nickname varchar (50 ),
Age int,
Email varchar (50)
)
2) model layer
Package Model;
Public class myuser {
Private int ID;
Private string name;
Public int GETID (){
Return ID;
}
Public void setid (int id ){
This. ID = ID;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public String getpsw (){
Return psw;
}
Public void setpsw (string psw ){
This. psw = psw;
}
Public String getnickname (){
Return nickname;
}
Public void setnickname (string nickname ){
This. Nickname = nickname;
}
Public int getage (){
Return age;
}
Public void setage (INT age ){
This. Age = age;
}
Public String getemail (){
Return email;
}
Public void setemail (string email ){
This. Email = Email;
}
Private string psw;
Private string nickname;
Private int age;
Private string email;
}
3) database connection
Package util;
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. SQL. statement;
Public class dB {
Public static connection createconn (){
Connection conn = NULL;
Try {
Class. forname ("net. SourceForge. jtds. JDBC. Driver ");
Conn = drivermanager. getconnection ("JDBC: jtds: sqlserver: // localhost: 1433/jsp", "sa", "123456 ");
} Catch (classnotfoundexception e ){
E. printstacktrace ();
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return conn;
}
Public static preparedstatement prepare (connection Conn, string SQL ){
Preparedstatement PS = NULL;
Try {
PS = conn. preparestatement (SQL );
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return pS;
}
Public static void close (connection conn ){
Try {
Conn. Close ();
Conn = NULL;
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
Public static void close (statement stmt ){
Try {
Stmt. Close ();
Stmt = NULL;
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
Public static void close (resultset RS ){
Try {
Rs. Close ();
Rs = NULL;
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
}
4) Service Layer
Package Service;
Import java. SQL. connection;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import util. dB;
Import model. myuser;
Public class myuserservice {
Public static void add (myuser user ){
Connection conn = dB. createconn ();
String SQL = "insert into myuser values (?,?,?,?,?) ";
Preparedstatement PS = dB. Prepare (Conn, SQL );
Try {
PS. setstring (1, user. getname ());
PS. setstring (2, user. getpsw ());
PS. setstring (3, user. getnickname ());
PS. setint (4, user. getage ());
PS. setstring (5, user. getemail ());
Ps.exe cuteupdate ();
} Catch (sqlexception e ){
E. printstacktrace ();
}
DB. Close (PS );
DB. Close (conn );
}
Public static myuser getbyname (string name ){
Connection conn = dB. createconn ();
String SQL = "select * From myuser where name =? ";
Preparedstatement PS = dB. Prepare (Conn, SQL );
Myuser user = NULL;
Try {
PS. setstring (1, name );
Resultset rs = ps.exe cutequery ();
If (Rs. Next ()){
User = new myuser ();
User. setid (Rs. getint ("ID "));
User. setname (Rs. getstring ("name "));
User. setpsw (Rs. getstring ("psw "));
User. setnickname (Rs. getstring ("nickname "));
User. setage (Rs. getint ("Age "));
User. setemail (Rs. getstring ("email "));
}
} Catch (sqlexception e ){
E. printstacktrace ();
}
DB. Close (PS );
DB. Close (conn );
Return user;
}
}
5) The page layer can call the service layer method.
<%
String name = request. getparameter ("name ");
String psw = request. getparameter ("psw ");
String nickname = request. getparameter ("nickname ");
Int age = integer. valueof (request. getparameter ("Age "));
String email = request. getparameter ("email ");
Myuser newuser = new myuser ();
Newuser. setname (name );
Newuser. setpsw (psw );
Newuser. setnickname (nickname );
Newuser. setage (AGE );
Newuser. setemail (email );
Myuser user = myuserservice. getbyname (name );
If (user! = NULL) {%>
<JSP: Forward page = "error. jsp"> </jsp: Forward>
<%}
Else
{
Myuserservice. Add (newuser );
%>
<JSP: Forward page = "success. jsp"> </jsp: Forward>
<%
}
%>
....
....
....