资源预览内容
第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
第9页 / 共9页
亲,该文档总共9页全部预览完了,如果喜欢就下载吧!
资源描述
使用工厂模式(可以使用简单工厂、抽象工厂、工厂方法)实现系统支持多种类型数据库链接的方法 标签:工厂模式, 工厂, 抽象工厂 乀尘 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 U
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号