jdbc访问数据库简单封装与配置 使用工厂和mvc

package org.lei.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author renlei 
 * @E-mail:renlei0109@yeah.net
 * @version 创建时间:2014-6-17 下午3:57:54 简单说明
 */
public final class JDBCUtil {
		//private String url ="jdbc:mysql://localhost:3306/leitest";
		//private String password="010910";
		//private String user = "root";
		//通过配置文件初始化
		private String url = null;
		private String driver = null;
		private String password=null;
		private String user = null;
		private static JDBCUtil instance = null;
		private JDBCUtil(){}
		//构造一个instance的单例
		public static JDBCUtil getInstance(){
			if(instance == null){
				synchronized (JDBCUtil.class) {
						if(instance == null){
							instance = new JDBCUtil();
						}
				}
			}
			return instance;
		}
		
		// 当一个类真正被使用的时候,JVM会初始化该类。主要操作就是执行静态代码块和初始化静态域。
		public Connection getConnection() throws SQLException{
			setProperties();
			try {
				Class.forName(driver);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return DriverManager.getConnection(url,user,password);
		}
		public void free(ResultSet rs, Statement st, Connection conn) {
			try {
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (st != null)
						st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					if (conn != null)
						try {
							conn.close();
						} catch (SQLException e) {
							e.printStackTrace();
						}
				}
			}
		}
		
		public void setProperties(){
			InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
			Properties properties = new Properties();
			try {
				properties.load(in);
				if(properties.containsKey("driver"))
					this.driver = properties.getProperty("driver");
				if(properties.containsKey("url"))
					this.url = properties.getProperty("url");
	            if(properties.containsKey("user")){  
	                this.user = properties.getProperty("user");  
	            }  
	            if(properties.containsKey("password")){  
	                this.password = properties.getProperty("password");  
	            }  
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
}
</pre><h2> DaoFactory</h2><div><pre name="code" class="java">package org.lei.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author renlei 
 * @E-mail:renlei0109@yeah.net
 * @version 创建时间:2014-6-17 下午3:57:54 简单说明
 */
public final class JDBCUtil {
		//private String url ="jdbc:mysql://localhost:3306/leitest";
		//private String password="010910";
		//private String user = "root";
		//通过配置文件初始化
		private String url = null;
		private String driver = null;
		private String password=null;
		private String user = null;
		private static JDBCUtil instance = null;
		private JDBCUtil(){}
		//构造一个instance的单例
		public static JDBCUtil getInstance(){
			if(instance == null){
				synchronized (JDBCUtil.class) {
						if(instance == null){
							instance = new JDBCUtil();
						}
				}
			}
			return instance;
		}
		
		// 当一个类真正被使用的时候,JVM会初始化该类。主要操作就是执行静态代码块和初始化静态域。
		public Connection getConnection() throws SQLException{
			setProperties();
			try {
				Class.forName(driver);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return DriverManager.getConnection(url,user,password);
		}
		public void free(ResultSet rs, Statement st, Connection conn) {
			try {
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (st != null)
						st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					if (conn != null)
						try {
							conn.close();
						} catch (SQLException e) {
							e.printStackTrace();
						}
				}
			}
		}
		
		public void setProperties(){
			InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
			Properties properties = new Properties();
			try {
				properties.load(in);
				if(properties.containsKey("driver"))
					this.driver = properties.getProperty("driver");
				if(properties.containsKey("url"))
					this.url = properties.getProperty("url");
	            if(properties.containsKey("user")){  
	                this.user = properties.getProperty("user");  
	            }  
	            if(properties.containsKey("password")){  
	                this.password = properties.getProperty("password");  
	            }  
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
}

package org.lei.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.lei.dao.UserDao;
import org.lei.model.User;
import org.lei.util.JDBCUtil;

public class UserDaoImpl implements UserDao{

	@Override
	public User findUser(int id) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JDBCUtil.getInstance().getConnection();
			String sql = "select * from userinfo where id = ?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			User user = null;
			while(rs.next()){
				user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
			}
			return user;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JDBCUtil.getInstance().free(rs, ps, conn);
		}
		return null;
	}

}

package org.lei.service.impl;

import org.lei.dao.DaoFactory;
import org.lei.dao.UserDao;
import org.lei.model.User;
import org.lei.service.UserService;

public class UserServiceImpl implements UserService{
	DaoFactory daoFactory = DaoFactory.getInstance();
	private UserDao userDao = daoFactory.getUserDao();
	
	@Override
	public User getUser(int id) {
		User user = userDao.findUser(id);
		return user;
	}
		
}

配置文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/leitest
user=root
password=010910

User

package org.lei.model;

import java.util.Date;

/**
 * @author renlei 
 * @E-mail:renlei0109@yeah.net
 * @version 创建时间:2014-6-17 下午8:13:55 简单说明
 */
public 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;
	}


}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值