PHP笔记网

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2013-04-07 17:38:46  修改时间:2024-11-22 15:19:16  分类:07.Java基础  编辑
DBAccess.java :
/**
 * 数据库访问操作类
 */
package php-note.util;

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

public class DBAccess {

	// 数据库驱动
	private String drv = "com.mysql.jdbc.Driver";

	// 连接URL
	private String url = "jdbc:mysql://localhost:3306/php-note";

	// 用户名
	private String usr= "root";

	// 密码
	private String pwd = "110";

	// 数据库连接对象
	private Connection conn = null;

	// 数据库声明对象
	private Statement stm = null;

	// 数据结果集对象
	private ResultSet rs = null;

	/**
	 * @return the drv
	 */
	public String getDrv() {
		return drv;
	}

	/**
	 * @param drv the drv to set
	 */
	public void setDrv(String drv) {
		this.drv = drv;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the usr
	 */
	public String getUsr() {
		return usr;
	}

	/**
	 * @param usr the usr to set
	 */
	public void setUsr(String usr) {
		this.usr = usr;
	}

	/**
	 * @return the pwd
	 */
	public String getPwd() {
		return pwd;
	}

	/**
	 * @param pwd the pwd to set
	 */
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	/**
	 * @return the conn
	 */
	public Connection getConn() {
		return conn;
	}

	/**
	 * @param conn the conn to set
	 */
	public void setConn(Connection conn) {
		this.conn = conn;
	}

	/**
	 * @return the stm
	 */
	public Statement getStm() {
		return stm;
	}

	/**
	 * @param stm the stm to set
	 */
	public void setStm(Statement stm) {
		this.stm = stm;
	}

	/**
	 * @return the rs
	 */
	public ResultSet getRs() {
		return rs;
	}

	/**
	 * @param rs the rs to set
	 */
	public void setRs(ResultSet rs) {
		this.rs = rs;
	}

	/**
	 * 创建连接对象
	 * @return
	 */
	public boolean createConn() {
		boolean b = false;
		try {
			Class.forName(drv).newInstance();
			conn = DriverManager.getConnection(url, pwd, pwd);
		} catch(SQLException e) {

		} catch (ClassNotFoundException e) {

		} catch (InstantiationException e) {

		} catch (IllegalAccessException e) {

		}

		return b;
	}

	/**
	 * 更新SQL函数
	 */
	public boolean update(String sql) {
		boolean b = false;
		try {
			stm = conn.createStatement();
			stm.execute(sql);
			b = true;
		} catch(Exception e) {

		}

		return b;
	}

	/**
	 * 查询SQL函数
	 */
	public void query(String sql) {
		try {
			stm = conn.createStatement();
			rs = stm.executeQuery(sql);
		} catch (Exception e) {

		}
	}

	/**
	 * 移到下一条记录函数
	 */
	public boolean next() {
		boolean b = false;
		try {
			if (rs.next()) {
				b = true;
			}
		} catch(Exception e) {

		}

		return b;
	}

	/**
	 * 获取字段field的值
	 */
	public String getValue(String field) {
		String value = null;
		try {
			if (rs != null) {
				value = rs.getString(field);
			}
		} catch(Exception e) {

		}

		return value;
	}

	/**
	 * 关闭结果集对象RS
	 */
	public void closeRs() {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch(SQLException e) {

		}
	}

	/**
	 * 关闭申明对象stm
	 */
	public void closeStm() {
		try {
			if (stm != null) {
				stm.close();
			}
		} catch(SQLException e) {

		}
	}

	/**
	 * 关闭数据库连接对象
	 */
	public void closeConn() {
		try {
			if (conn != null) {
				conn.close();
			}
		} catch(SQLException e) {

		}
	}
}

 

应用示例:

 UserBean.java
package php-note.tools;

import php-note.util.DBAccess;

public class UserBean {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.print("yes");
	}

	/**
	 * 验证用户名与密码
	 */
	public boolean valid(String username, String password) {
		boolean isValid = false;
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			String sql = "select * from user where username='" + username + "' and password='" + password + "'";
			db.query(sql);
			if (db.next()) {
				isValid = true;
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}

		return isValid;
	}

	/**
	 * 检查用户名是否存在
	 */
	public boolean isExist(String username) {
		boolean isExist = false;
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			String sql = "select * from user where username='" + username + "'";
			db.query(sql);
			if (db.next()) {
				isExist = true;
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}

		return isExist;
	}

	/**
	 * 添加用户
	 * @param String username 用户名
	 * @param String password 用户密码
	 * @param String email 用户Email
	 */
	public void add(String username, String password, String email) {
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			String sql = "insert into user(`username`,`password`,`email`) values ('" + username + "','" + password + "','" + email + "')";
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
	}
}