使用工厂模式实现数据库连接.docx_第1页
使用工厂模式实现数据库连接.docx_第2页
使用工厂模式实现数据库连接.docx_第3页
使用工厂模式实现数据库连接.docx_第4页
使用工厂模式实现数据库连接.docx_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

使用工厂模式(可以使用简单工厂、抽象工厂、工厂方法)实现系统支持多种类型数据库链接的方法标签:工厂模式,工厂,抽象工厂乀尘2010-07-04 14:47满意答案好评率:0%/数据库连接接口package util;import java.sql.*;public interface DBConnection /获取连接对象public Connection getcon();/关闭数据库的链接public void closed(ResultSet set, PreparedStatement pre, Connection con);/mysql连接实现类package util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class DBConnectionMysqlIM implements DBConnectionprivate final String DRIVER=com.mysql.jdbc.Driver;private final String URL=jdbc:mysql:/localhost:3306/angel;public Connection getcon() Connection con=null;try Class.forName(DRIVER);con = DriverManager.getConnection(URL, root, root); catch (ClassNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return con;public void closed(ResultSet set, PreparedStatement pre, Connection con) try if(set!=null)set.close();if(pre!=null)pre.close();if(con!=null)con.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();/sqlserver连接实现类package util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class DBConnectionSqlIM implements DBConnection private final String DRIVER = com.microsoft.sqlserver.jdbc.SQLServerDriver;private final String URL = jdbc:sqlserver:/localhost:1433;DatabaseName=angel;public Connection getcon() Connection con = null;try Class.forName(DRIVER);con = DriverManager.getConnection(URL, sa, sa); catch (ClassNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return con;public void closed(ResultSet set, PreparedStatement pre, Connection con) try if(set!=null)set.close();if(pre!=null)pre.close();if(con!=null)con.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();/工厂类package util;public class DBConFactory public static DBConnection getcon(String name)DBConnection conif=null;try Class c = Class.forName(name);conif = (DBConnection) c.newInstance(); catch (ClassNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (InstantiationException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (IllegalAccessException e) / TODO Auto-generated catch blocke.printStackTrace();return conif;/测试类package model;import java.util.ArrayList;import java.util.List;import util.DBConFactory;import util.DBConnection;import java.sql.*;public class UsersDaoIM implements UsersDao private final String SQL_ADD = insert into users (name,pass) values (?,?);private final String SQL_UPDATE = update users set name=?,pass=? where id=?;private final String SQL_DELETE = delete from users where id=?;private final String SQL_SELECT = select * from users;private final String SQL_LOGIN = select * from users where name=? and pass=?;private final String SQL_GETINFO = select * from users where id=?;private final String SQL_TEXT = select count(*) from users where name=?;DBConnection conif = DBConFactory.getcon(util.DBConnectionSqlIM);public boolean add(UsersInfo u) boolean b = false;Connection con = null;PreparedStatement pre = null;try con = conif.getcon();pre = con.prepareStatement(SQL_ADD);pre.setString(1, u.getName();pre.setString(2, u.getPass();int i = pre.executeUpdate();if (i 0) b = true; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace(); finally conif.closed(null, pre, con);return b;public boolean delete(int i) boolean b = false;Connection con = null;PreparedStatement pre = null;try con = conif.getcon();pre = con.prepareStatement(SQL_DELETE);pre.setInt(1, i);int k = pre.executeUpdate();if (k 0) b = true; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace(); finally conif.closed(null, pre, con);return b;public boolean login(UsersInfo u) boolean b = false;ResultSet set = null;Connection con = null;PreparedStatement pre = null;UsersInfo user = null;try con = conif.getcon();pre = con.prepareStatement(SQL_LOGIN);pre.setString(1, u.getName();pre.setString(2, u.getPass();set = pre.executeQuery();while (set.next() user = new UsersInfo();user.setId(set.getInt(1);user.setName(set.getString(2);user.setPass(set.getString(3);if (user != null) b = true; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace(); finally conif.closed(set, pre, con);return b;public List select() List list = new ArrayList();ResultSet set = null;Connection con = null;PreparedStatement pre = null;try con = conif.getcon();pre = con.prepareStatement(SQL_SELECT);set = pre.executeQuery();while (set.next() UsersInfo u = new UsersInfo();u.setId(set.getInt(1);u.setName(set.getString(2);u.setPass(set.getString(3);list.add(u); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace(); finally conif.closed(set, pre, con);return list;public boolean update(UsersInfo u) boolean b = false;Connection con = null;PreparedStatement pre = null;try con = conif.getcon();pre = con.prepareStatement(SQL_UPDATE);pre.setString(1, u.getName();pre.setString(2, u.getPass();pre.setInt(3, u.getId();int i = pre.executeUpdate();if (i 0) b = true; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace(); finally conif.closed(null, pre, con);return b;public UsersInfo getInfoById(int i) ResultSet set = null;Connection con = null;PreparedStatement pre = null;UsersInfo user = null;try con = conif.getcon();pre = con.prepareStatement(SQL_GETINFO);pre.setInt(1, i);set = pre.executeQuery();while (set.next() user = new UsersInfo();user.setId(set.getInt(1);user.setName(set.getStrin

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论