如何访问数据库?
基本的数据访问程序
class User{
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;
}
}
/*用于操作User表,假设只有新增用户和得到用户方法*/
class SqlserverUser {
public void Insert(User user) {
System.out.println("在Sqlserver中给User表增加一条记录");
}
public User GetUser(int id)
{
System.out.println("在Sqlserver中根据ID得到User表一条记录");
return null;
}
}
public class Main{
public static void main(String[] args) {
User user=new User();
SqlserverUser su=new SqlserverUser();
su.Insert(user);
su.GetUser(1);
}
}
这里不能换数据库,因为SqlserverUser su=new SqlserverUser()使得su这个对象被框死在了Sqlserver上了。
增加一个Access数据库
package fx;
class User{
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;
}
}
/*IUser接口,解除与具体数据库的耦合*/
interface IUser{
void Insert(User user);
User GetUser(int id);
}
/*用于操作User表,假设只有新增用户和得到用户方法*/
class SqlserverUser implements IUser{
public void Insert(User user) {
System.out.println("在Sqlserver中给User表增加一条记录");
}
public User GetUser(int id)
{
System.out.println("在Sqlserver中根据ID得到User表一条记录");
return null;
}
}
class AccessUser implements IUser{
public void Insert(User user) {
System.out.println("在Access中给User表增加一条记录");
}
public User GetUser(int id)
{
System.out.println("在Access中根据ID得到User表一条记录");
return null;
}
}
/*定义一个创建访问User表对象的工厂接口*/
interface IFactory{
IUser CreateUser();
}
class SqlserverFactory implements IFactory{
@Override
public IUser CreateUser() {
// TODO Auto-generated method stub
return new SqlserverUser();
}
}
class AccessFactory implements IFactory{
@Override
public IUser CreateUser() {
// TODO Auto-generated method stub
return new AccessUser();
}
}
public class Main{
public static void main(String[] args) {
User user=new User();
IFactory factory=new SqlserverFactory();
/*若要改成Accesss数据库,只需改此处*/
IUser iu=factory.CreateUser();
iu.Insert(user);
iu.GetUser(1);
}
}
增加一个部门表
提供一个创建一系列相关的或者相互依赖对象的接口,而无需指定它们具体的类。
class User{
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;
}
}
class Department{
private int id;
private String deptName;
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName=deptName;
}
}
/*IUser接口,解除与具体数据库的耦合*/
interface IUser{
void Insert(User user);
User GetUser(int id);
}
interface IDepartment{
void Insert(Department department);
Department GetDepartment(int id);
}
/*用于操作User表,假设只有新增用户和得到用户方法*/
class SqlserverUser implements IUser{
public void Insert(User user) {
System.out.println("在Sqlserver中给User表增加一条记录");
}
public User GetUser(int id)
{
System.out.println("在Sqlserver中根据ID得到User表一条记录");
return null;
}
}
class AccessUser implements IUser{
public void Insert(User user) {
System.out.println("在Access中给User表增加一条记录");
}
public User GetUser(int id)
{
System.out.println("在Access中根据ID得到User表一条记录");
return null;
}
}
class SqlserverDepartment implements IDepartment{
public void Insert(Department department) {
System.out.println("在Sqlserver中给Department表增加一条记录");
}
public Department GetDepartment(int id)
{
System.out.println("在Sqlserver中根据ID得到Department表一条记录");
return null;
}
}
class AccessDepartment implements IDepartment{
public void Insert(Department department) {
System.out.println("在Access中给Department表增加一条记录");
}
public Department GetDepartment(int id)
{
System.out.println("在Access中根据ID得到Department表一条记录");
return null;
}
}
/*定义一个创建访问User表对象的工厂接口*/
interface IFactory{
IUser CreateUser();
IDepartment CreateDepartment();
}
class SqlserverFactory implements IFactory{
@Override
public IUser CreateUser() {
// TODO Auto-generated method stub
return new SqlserverUser();
}
@Override
public IDepartment CreateDepartment() {
// TODO Auto-generated method stub
return new SqlserverDepartment();
}
}
class AccessFactory implements IFactory{
@Override
public IUser CreateUser() {
// TODO Auto-generated method stub
return new AccessUser();
}
@Override
public IDepartment CreateDepartment() {
// TODO Auto-generated method stub
return new AccessDepartment();
}
}
public class Main{
public static void main(String[] args) {
User user=new User();
IFactory factory=new SqlserverFactory();
/*若要改成Access数据库,只需修改这个地方 */
IUser iu=factory.CreateUser();
iu.Insert(user);
iu.GetUser(1);
Department dept=new Department();
IDepartment id=factory.CreateDepartment();
id.Insert(dept);
id.GetDepartment(1);
}
}
抽象工厂模式:提供一个创建一系列相关或相互以来的对象的接口,而无需指定他们具体的类。
优点:
①易于交换产品系列,改变具体工厂即可使用不同的产品配置。
②它让具体的创建实例过程与客户端分离。
抽象工厂模式
**抽象工厂(Abstract Factory)角色:**这个角色的是抽象厂模式的核心,它是与应用系统商业逻辑无关的。IFactory是一个抽象厂接口,它里面应该包含所有的产品创建的抽象方法。
**具体工厂(Concrete Factory)角色:**这个角色直接在客户端的调用下创建产A品的实例。这个角色含有选择合适的产品对象的逻辑,而这个逻辑是与应用系统的商业逻辑紧密相关的。ConcreteFactory1和ConcreteFacotry2就是具体的工厂。
**抽象产品( AbstractProduct)角色:**担任这个角色的类是抽象工厂模式所创建的对象的父类,或它们共同拥有的接口。AbstractProductA和AbstractProductB是两个抽象产品,它们都有可能有两种不同的实现,上例中就是User和IDepartment。
**具体产品(Concrete Product)角色:**抽象工厂模式所创建的任何产品对象都是某一个具体产品类的实例。这是客户端最终需要的东西,其内部定充满了应用系统的商业逻辑。
反射+抽象工厂模式
class User{
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;
}
}
class Department{
private int id;
private String deptName;
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName=deptName;
}
}
/*IUser接口,解除与具体数据库的耦合*/
interface IUser{
void Insert(User user);
User GetUser(int id);
}
interface IDepartment{
void Insert(Department department);
Department GetDepartment(int id);
}
/*用于操作User表,假设只有新增用户和得到用户方法*/
class SqlserverUser implements IUser{
public void Insert(User user) {
System.out.println("在Sqlserver中给User表增加一条记录");
}
public User GetUser(int id)
{
System.out.println("在Sqlserver中根据ID得到User表一条记录");
return null;
}
}
class AccessUser implements IUser{
public void Insert(User user) {
System.out.println("在Access中给User表增加一条记录");
}
public User GetUser(int id)
{
System.out.println("在Access中根据ID得到User表一条记录");
return null;
}
}
class SqlserverDepartment implements IDepartment{
public void Insert(Department department) {
System.out.println("在Sqlserver中给Department表增加一条记录");
}
public Department GetDepartment(int id)
{
System.out.println("在Sqlserver中根据ID得到Department表一条记录");
return null;
}
}
class AccessDepartment implements IDepartment{
public void Insert(Department department) {
System.out.println("在Access中给Department表增加一条记录");
}
public Department GetDepartment(int id)
{
System.out.println("在Access中根据ID得到Department表一条记录");
return null;
}
}
class DataAccess{
private static String db="Sqlserver";
public static IUser CreateUser()
{
IUser result=null;
switch(db) {
case"Sqlserver":result=new SqlserverUser();break;
case"Access":result=new AccessUser();break;
}
return result;
}
public static IDepartment CreateDepartment()
{
IDepartment result=null;
switch(db) {
case"Sqlserver":result=new SqlserverDepartment();break;
case"Access":result=new AccessDepartment();break;
}
return result;
}
}
class Main{
public static void main(String[] args) {
User user=new User();
Department dept=new Department();
IUser iu=DataAccess.CreateUser();/*直接得到实际的数据库访问实例,而不存在任何依赖。*/
iu.Insert(user);
iu.GetUser(1);
IDepartment id=DataAccess.CreateDepartment();
id.Insert(dept);
id.GetDepartment(1);
}
}
事先设置了db的值(Sqlserver或Access),所以简单工厂的方法都不需要输入参数,这样在客户端就只需要DataAccess.createUser()和DataAccess.createDepartment()来生成具体的数据库访问类实例。客户端没有出现任何一个Sqlserver或Access的字样,达到了解耦的目的。