简单的三层框架以及使用dbutils进行数据库操作入门.docx

上传人:b****8 文档编号:9187986 上传时间:2023-02-03 格式:DOCX 页数:51 大小:21.72KB
下载 相关 举报
简单的三层框架以及使用dbutils进行数据库操作入门.docx_第1页
第1页 / 共51页
简单的三层框架以及使用dbutils进行数据库操作入门.docx_第2页
第2页 / 共51页
简单的三层框架以及使用dbutils进行数据库操作入门.docx_第3页
第3页 / 共51页
简单的三层框架以及使用dbutils进行数据库操作入门.docx_第4页
第4页 / 共51页
简单的三层框架以及使用dbutils进行数据库操作入门.docx_第5页
第5页 / 共51页
点击查看更多>>
下载资源
资源描述

简单的三层框架以及使用dbutils进行数据库操作入门.docx

《简单的三层框架以及使用dbutils进行数据库操作入门.docx》由会员分享,可在线阅读,更多相关《简单的三层框架以及使用dbutils进行数据库操作入门.docx(51页珍藏版)》请在冰豆网上搜索。

简单的三层框架以及使用dbutils进行数据库操作入门.docx

简单的三层框架以及使用dbutils进行数据库操作入门

简单的三层框架以及使用dbutils进行数据库操作(入门)

1.搭建三层框架

1.1.什么是三层框架

三层框架就是将代码简单的分为三层并对某些地方进行必要的封装,方便日后维护,主要分为以下三部分:

界面层(Swing或JSP):

主要是界面显示的代码。

业务层(business):

主要是处理业务逻辑的代码。

数据访问层(DAO):

主要是与数据库交互的代码。

1.2.各层之间如何协作

关系:

界面层(调用)à业务层(调用)à数据访问层(与数据库交互)

(显示)界面层ß(结果)业务层ß(结果)数据访问层

数据传递:

各层之间将数据封装成实体类(entity)进行传递。

写入信息:

界面层将用户录入的信息封装成实体类对象,然后传给业务层进行相关的业务处理,然后业务层选择需要调用的数据访问层,数据访问层负责将传递过来的实体对像中封装的信息提取出来,并且拼出SQL语句,将信息写入数据库。

显示信息:

数据访问层将数据库中的数据提取出来封装成实体对象,如果有多个对象的话继而封装成集合,然后传递给调用它的业务层进行业务处理,业务层再将处理过的实体对象或是集合传递给调用业务层的界面,界面最后将实体对象或是集合中封装的信息提取出来进行显示。

===============================================================================

2.编写实例(省略界面层和业务层)

需要的jar包:

数据库直连包,dbutils包

数据库:

stuDB

表:

USERS

表结构:

userIdintNOTNULLIDENTITY(1,1)PRIMARYKEY,

userNamenvarchar(20)NOTNULL,

passwordnvarchar(20)NOTNULL,

realNamenvarchar(20),

sexnvarchar

(2)

=========================================================

实体类:

UsersEntity

viewplaincopytoclipboardprint?

packagecom.saga.entity;

/**

*USERS表的实体类

*@authorsaga1320

*/

publicclassUsersEntity{

privateintuserId;

privateStringuserName;

privateStringpassword;

privateStringrealName;

privateStringsex;

publicUsersEntity(){

}

publicStringgetPassword(){

returnpassword;

}

publicvoidsetPassword(Stringpassword){

this.password=password;

}

publicStringgetRealName(){

returnrealName;

}

publicvoidsetRealName(StringrealName){

this.realName=realName;

}

publicStringgetSex(){

returnsex;

}

publicvoidsetSex(Stringsex){

this.sex=sex;

}

publicintgetUserId(){

returnuserId;

}

publicvoidsetUserId(intuserId){

this.userId=userId;

}

publicStringgetUserName(){

returnuserName;

}

publicvoidsetUserName(StringuserName){

this.userName=userName;

}

}

packagecom.saga.entity;

/**

*USERS表的实体类

*@authorsaga1320

*/

publicclassUsersEntity{

privateintuserId;

privateStringuserName;

privateStringpassword;

privateStringrealName;

privateStringsex;

publicUsersEntity(){

}

publicStringgetPassword(){

returnpassword;

}

publicvoidsetPassword(Stringpassword){

this.password=password;

}

publicStringgetRealName(){

returnrealName;

}

publicvoidsetRealName(StringrealName){

this.realName=realName;

}

publicStringgetSex(){

returnsex;

}

publicvoidsetSex(Stringsex){

this.sex=sex;

}

publicintgetUserId(){

returnuserId;

}

publicvoidsetUserId(intuserId){

this.userId=userId;

}

publicStringgetUserName(){

returnuserName;

}

publicvoidsetUserName(StringuserName){

this.userName=userName;

}

}

===============================================================

2.1.没有使用dbutils的数据访问层(只需直连包)

首先,创建所有数据访问类的抽象基类BaseDAO

viewplaincopytoclipboardprint?

packagecom.saga.dao;

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.PreparedStatement;

importjava.sql.ResultSet;

importjava.sql.SQLException;

/**

*所有数据访问类的基类,其他数据访问类需继承此类

*@authorsaga1320

*/

publicabstractclassBaseDAO{

//数据库连接驱动名

privatestaticfinalStringDRIVERNAME

="com.microsoft.sqlserver.jdbc.SQLServerDriver";

//数据库连接URL

privatestaticfinalStringRUL

="jdbc:

sqlserver:

//localhost:

1433;databaseName=stuDB";

//数据库用户名

privatestaticfinalStringUNAME="sa";

//密码

privatestaticfinalStringPWD="";

privateConnectionconn=null;

/**

*建立数据库连接

*@throwsClassNotFoundException

*@throwsSQLException

*/

privatevoidgetConnection()

throwsClassNotFoundException,SQLException{

Class.forName(DRIVERNAME);

conn=DriverManager.getConnection(RUL,UNAME,PWD);

}

publicBaseDAO(){

super();

}

/**

*执行增,删,改,(带参数)

*@paramstrSqlsql语句

*@paramparam参数

*@returnsql语句所影响的行数

*@throwsSQLException

*@throwsClassNotFoundException

*/

protectedintexecuteUpdate(StringstrSql,Object[]param)

throwsClassNotFoundException,SQLException{

if(conn==null||conn.isClosed()){

this.getConnection();

}

PreparedStatementpstat=conn.prepareStatement(strSql);

for(inti=0;i

pstat.setObject(i+1,param[i]);

}

returnpstat.executeUpdate();

}

/**

*执行增,删,改,(不带参数)

*@paramstrSqlsql语句

*@returnsql语句所影响的行数

*@throwsSQLException

*@throwsClassNotFoundException

*/

protectedintexecuteUpdate(StringstrSql)

throwsClassNotFoundException,SQLException{

if(conn==null||conn.isClosed()){

this.getConnection();

}

PreparedStatementpstat=conn.prepareStatement(strSql);

returnpstat.executeUpdate();

}

/**

*带参数查询

*@paramstrSqlsql语句

*@paramparam参数

*@returnResultSet结果集

*@throwsClassNotFoundException

*@throwsSQLException

*/

protectedResultSetexecuteQuery(StringstrSql,Object[]param)

throwsSQLException,ClassNotFoundException{

if(conn==null||conn.isClosed()){

this.getConnection();

}

PreparedStatementpstat=conn.prepareStatement(strSql);

for(inti=0;i

pstat.setObject(i+1,param[i]);

}

returnpstat.executeQuery();

}

/**

*无参查询

*@paramstrSqlsql语句

*@paramparam参数

*@returnResultSet结果集

*@throwsSQLException

*@throwsClassNotFoundException

*/

protectedResultSetexecuteQuery(StringstrSql)

throwsSQLException,ClassNotFoundException{

if(conn==null||conn.isClosed()){

this.getConnection();

}

PreparedStatementpstat=conn.prepareStatement(strSql);

returnpstat.executeQuery();

}

/**

*关闭数据库连接

*@throwsSQLException

*/

protectedvoidcloseConnection()throwsSQLException{

if(conn!

=null){

if(!

conn.isClosed()){

conn.close();

}

}

}

}

packagecom.saga.dao;

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.PreparedStatement;

importjava.sql.ResultSet;

importjava.sql.SQLException;

/**

*所有数据访问类的基类,其他数据访问类需继承此类

*@authorsaga1320

*/

publicabstractclassBaseDAO{

//数据库连接驱动名

privatestaticfinalStringDRIVERNAME

="com.microsoft.sqlserver.jdbc.SQLServerDriver";

//数据库连接URL

privatestaticfinalStringRUL

="jdbc:

sqlserver:

//localhost:

1433;databaseName=stuDB";

//数据库用户名

privatestaticfinalStringUNAME="sa";

//密码

privatestaticfinalStringPWD="";

privateConnectionconn=null;

/**

*建立数据库连接

*@throwsClassNotFoundException

*@throwsSQLException

*/

privatevoidgetConnection()

throwsClassNotFoundException,SQLException{

Class.forName(DRIVERNAME);

conn=DriverManager.getConnection(RUL,UNAME,PWD);

}

publicBaseDAO(){

super();

}

/**

*执行增,删,改,(带参数)

*@paramstrSqlsql语句

*@paramparam参数

*@returnsql语句所影响的行数

*@throwsSQLException

*@throwsClassNotFoundException

*/

protectedintexecuteUpdate(StringstrSql,Object[]param)

throwsClassNotFoundException,SQLException{

if(conn==null||conn.isClosed()){

this.getConnection();

}

PreparedStatementpstat=conn.prepareStatement(strSql);

for(inti=0;i

pstat.setObject(i+1,param[i]);

}

returnpstat.executeUpdate();

}

/**

*执行增,删,改,(不带参数)

*@paramstrSqlsql语句

*@returnsql语句所影响的行数

*@throwsSQLException

*@throwsClassNotFoundException

*/

protectedintexecuteUpdate(StringstrSql)

throwsClassNotFoundException,SQLException{

if(conn==null||conn.isClosed()){

this.getConnection();

}

PreparedStatementpstat=conn.prepareStatement(strSql);

returnpstat.executeUpdate();

}

/**

*带参数查询

*@paramstrSqlsql语句

*@paramparam参数

*@returnResultSet结果集

*@throwsClassNotFoundException

*@throwsSQLException

*/

protectedResultSetexecuteQuery(StringstrSql,Object[]param)

throwsSQLException,ClassNotFoundException{

if(conn==null||conn.isClosed()){

this.getConnection();

}

PreparedStatementpstat=conn.prepareStatement(strSql);

for(inti=0;i

pstat.setObject(i+1,param[i]);

}

returnpstat.executeQuery();

}

/**

*无参查询

*@paramstrSqlsql语句

*@paramparam参数

*@returnResultSet结果集

*@throwsSQLException

*@throwsClassNotFoundException

*/

protectedResultSetexecuteQuery(StringstrSql)

throwsSQLException,ClassNotFoundException{

if(conn==null||conn.isClosed()){

this.getConnection();

}

PreparedStatementpstat=conn.prepareStatement(strSql);

returnpstat.executeQuery();

}

/**

*关闭数据库连接

*@throwsSQLException

*/

protectedvoidcloseConnection()throwsSQLException{

if(conn!

=null){

if(!

conn.isClosed()){

conn.close();

}

}

}

}

接着,创建USERS表的数据访问类:

USersDAO

viewplaincopytoclipboardprint?

packagecom.saga.dao;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.util.ArrayList;

importjava.util.List;

importcom.saga.entity.UsersEntity;

/**

*USERS表的数据访问类,继承BaseDAO

*@authorsaga1320

*/

publicclassUsersDAOextendsBaseDAO{

publicUsersDAO(){

super();

}

/**

*向USERS表中添加一条数据

*@paramuserUsersEntity封装了数据信息

*@returnint添加记录的行数

*@throwsSQLException

*@throwsClassNotFoundException

*/

publicintinsertUser(UsersEntityuser)

throwsClassNotFoundException,SQLException{

int=0;

StringstrSql="INSERTINTOUSERSVALUES(?

?

?

?

)";

Object[]param=newObject[]{

user.getUserName(),

user.getPassword(),

user.getRealName(),

user.getSex()

};

i=super.executeUpda

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 环境科学食品科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1