关闭

关于jdbc-util的封装

标签: jdbc数据库sql
382人阅读 评论(0) 收藏 举报

-、jdbc的封装应该分为两层:

1.dao层[此层封装的是对数据库的操作 例如:增删改差 ]

2.domain层[这是封装的数据库的对象,在传递数据的时候 一般都是传递对象]

=、dao层

1.在dao层里面我分为了两个文件 第一个:JdbcDao.java 第二个:JdbcUtil.java 从名字 相信也能知道是用来干嘛的了....


三、JdbcDao.java[这个类封装的全是静态方法 对数据库的增删改差 全部封装在里面]

package com.util.dao;//包名最好是规范命名

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.util.domain.Users;


public class JdbcDao {

<span style="white-space:pre">	</span>//对数据库的数据进行增加操作 参数传users也就是刚才提到的domain里面的对象 对应数据库中的表 也叫users
	public static void adduser(Users users) throws Exception{
		Connection conn = JdbcUtil.getconn();
		String sql = "insert into users(username,password,email,hobby,address,brithday,gender) values(?,?,?,?,?,?,?)";
		PreparedStatement psm = null;
		psm = conn.prepareStatement(sql);		
		psm.setString(1, users.getUsername());
		psm.setString(2, users.getPassword());
		psm.setString(3, users.getEmail());
		psm.setString(4, users.getHobby());
		psm.setString(5, users.getAdderss());
		psm.setDate(6, new java.sql.Date( users.getBrithday().getTime()));
		psm.setString(7, users.getGender());
		psm.executeUpdate();
	}
	
	//这里是写登录的时候 用来判断用户名和密码
	public static boolean check(String username, String password){
		Users user = null;
		try {
			List<Users> list = selectuser();
			Iterator<Users> it = list.iterator();
			while(it.hasNext()){
				user = it.next();
				if(username.equals(user.getUsername()) && password.equals(user.getPassword())){
					return true;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
<span style="white-space:pre">	</span>//查询用户 判断前台输入的用户名和密码是否与数据库中存储的用户名和密码一致
	public static List<Users> selectuser() throws SQLException{
		Connection conn = JdbcUtil.getconn();
		 Statement sta = null;
		 Users user = null;
		 ResultSet rs = null;
		 List<Users> list = new ArrayList<Users>();
			sta = conn.createStatement();		
		
		String sql = "select username,password from users";
			rs = sta.executeQuery(sql);
			while(rs.next()){
				user=new Users(rs.getString("username"),rs.getString("password"));
				list.add(user);
			}
			conn.close();
			return list;
		}
		
}

四:JdbcUtil.java[封装连接数据库  关闭数据库]

package com.util.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcUtil {
	private static String url;
	private static String username;
	private static String password;
	static{
			String driver = "com.mysql.jdbc.Driver"; 
			 url = "jdbc:mysql://localhost:3306/15java1";
			 username ="root";
			 password ="root";
			 try {
				Class.forName(driver);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		
	}
	//链接数据库
	public static Connection getconn() throws SQLException{
		
			return DriverManager.getConnection(url,username,password);
	}
	//关闭数据库连接 从后往前关闭
	public static void closeAll(Connection conn,Statement st,ResultSet rs){
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(st != null){
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

五:domain层

对应数据库中的表也叫users表,这里的属性和数据库中的字段是对应的

package com.util.domain;

import java.util.Date;


public class Users {
	private String username;
	private  String password;
	private String email;
	private String hobby;
	private String adderss;
	private Date  brithday;
	private String gender;
	
	public Users(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}
	public Users() {
		// TODO Auto-generated constructor stub
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getHobby() {
		return hobby;
	}
	public void setHobby(String hobby) {
		this.hobby = hobby;
	}
	public String getAdderss() {
		return adderss;
	}
	public void setAdderss(String adderss) {
		this.adderss = adderss;
	}
	
	public Date getBrithday() {
		return brithday;
	}
	public void setBrithday(Date brithday) {
		this.brithday = brithday;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	
	
	

	
	
}

	

整个jdbc的简单连接和操作基本就封装完毕 如果想要对数据库有更多的操作就直接在dao层里面的javadao.java 自定义 然后调用使用就行啦!

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9198次
    • 积分:285
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章存档