java连接数据库2

利用接口,封装类实现,增删改查

  1. 接口,增,删,改,查4种方法.
package com.wenhe.dao;

import java.util.Vector;

import com.wenhe.vo.Employee;

public interface EmployeeDao {
	
	/**
	 *  增
	 * @param Employee
	 * @return
	 */
	int saveEmployeeDao(Employee Employee);
	
	/**
	 * 	删
	 * @param id
	 * @return
	 */
	int deleteEmployeeDao(int id);
	
	/**
	 * 	改
	 * @param Employee
	 * @return
	 */
	int modifyEmployeeDao(Employee Employee);
	
	/**
	 * 	查1
	 * @param id
	 * @return
	 */
	Employee load(int id);
	
	
	/*
	 * 	查所有
	 * 
	 */
	Vector<Employee> queryAll();

}

  1. 封装类.数据库中对应键
package com.wenhe.vo;

public class Employee {
	
	private int id ;
	private String name ;
	private String sex ;
	private int age ;
	private int deptId;
	
	public Employee() {}

	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;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getDeptId() {
		return deptId;
	}

	public void setDeptId(int deptId) {
		this.deptId = deptId;
	}
	
}

接口继承子类重写方法

  1. connection接口
  2. statement 接口
  3. resultset接口
package com.wenhe.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import com.wenhe.dao.EmployeeDao;
import com.wenhe.total.DCji;
import com.wenhe.vo.Employee;

public class EmployeeDaoimpl implements EmployeeDao{

	@Override
	public int saveEmployeeDao(Employee emp) {
		//加载驱动,创建连接
		Connection connection = DCji.getConnection();
		Statement statement = null;
		try {
			//创建申明
		    connection = DCji.getConnection();
			statement = connection.createStatement();
			String sql= "INSERT INTO t_employee (name,sex,age,dept_id) VALUES ('"
			+emp.getName()
			+"','"
			+emp.getSex()
			+"',"
			+emp.getAge()
			+","
			+emp.getDeptId()
			+")";
			System.out.println(sql);
			//执行sql
			
			return statement.executeUpdate(sql);
		} catch (SQLException e) {
		//	e.printStackTrace();
			//抛异常
			throw new RuntimeException(e);
		}finally {
			DCji.release(null, statement, connection);
		}
		
	}

	@Override
	public int deleteEmployeeDao(int id) {
		//加载驱动,创建连接
				Connection connection = DCji.getConnection();
				Statement statement = null;
				try {
					//创建申明
				    connection = DCji.getConnection();
					statement = connection.createStatement();
					String sql= "DELETE FROM t_employee where id ="+ id ;
					System.out.println(sql);
					//执行sql
					
					return statement.executeUpdate(sql);
				} catch (SQLException e) {
				//	e.printStackTrace();
					//抛异常
					throw new RuntimeException(e);
				}finally {
					DCji.release(null, statement, connection);
				}
	}

	@Override
	public int modifyEmployeeDao(Employee emp) {
		//加载驱动,创建连接
				Connection connection = DCji.getConnection();
				Statement statement = null;
				try {
					//创建申明
				    connection = DCji.getConnection();
					statement = connection.createStatement();
					String sql= "UPDATE t_employee SET name = '"
							+ emp.getName()
							+ "', sex = '"
							+ emp.getSex()
							+ "', age = "
							+ emp.getAge()
							+ ", dept_id = "
							+ emp.getDeptId()
							+ " WHERE id = "
							+ emp.getId();
					System.out.println(sql);
					//执行sql
					
					return statement.executeUpdate(sql);
				} catch (SQLException e) {
				//	e.printStackTrace();
					//抛异常
					throw new RuntimeException(e);
				}finally {
					DCji.release(null, statement, connection);
				}
	}

	@Override
	public Employee load(int id) {
		//加载驱动,创建连接
		Connection connection = DCji.getConnection();
		Statement statement = null;
		ResultSet rs = null ;
		try {
			//创建申明
		    connection = DCji.getConnection();
			statement = connection.createStatement();
			String sql= "select id,name,sex,age,dept_id FROM t_employee where id ="+ id ;
			System.out.println(sql);
			//执行sql
			
			rs = statement.executeQuery(sql);
		//遍历打印
			//封装实例化
			Employee employee = new Employee();
			while(rs.next()) {
			employee.setId(rs.getInt("id"));
			employee.setName(rs.getString("name"));
			employee.setSex(rs.getString("sex"));
			employee.setAge(rs.getInt("age"));
			employee.setDeptId(rs.getInt("dept_id"));
			}
			return  employee  ;    //employee类型的返回值
		} catch (SQLException e) {
		//	e.printStackTrace();
			//抛异常
			throw new RuntimeException(e);
		}finally {
			DCji.release(rs, statement, connection);
		}
	}

	@Override
	public Vector<Employee> queryAll() {
		//加载驱动,创建连接
				Connection connection = DCji.getConnection();
				Statement statement = null;
				ResultSet rs = null ;
				try {
					//创建申明
				    connection = DCji.getConnection();
					statement = connection.createStatement();
					String sql= "SELECT id, name, sex, age, dept_id FROM t_employee ";
					System.out.println(sql);
					//执行sql
					
					rs = statement.executeQuery(sql);
				//遍历打印
					//封装实例化
					Vector<Employee> employees = new  Vector<Employee>();
					while(rs.next()) {
					Employee employee = new Employee();
					employee.setId(rs.getInt("id"));
					employee.setName(rs.getString("name"));
					employee.setSex(rs.getString("sex"));
					employee.setAge(rs.getInt("age"));
					employee.setDeptId(rs.getInt("dept_id"));
					employees.add(employee);
					}
					return  employees  ;    //employee类型的返回值
				} catch (SQLException e) {
				//	e.printStackTrace();
					//抛异常
					throw new RuntimeException(e);
				}finally {
					DCji.release(rs, statement, connection);
				}
	}

}

预编译,重写方法实现

  1. connection接口
  2. PreparedStatement 接口 ,预编译
  3. resultset接口
package com.wenhe.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import com.wenhe.dao.EmployeeDao;
import com.wenhe.total.DCji;
import com.wenhe.vo.Employee;

public class EmployeeDaoimpl2 implements EmployeeDao{

	@Override
	public int saveEmployeeDao(Employee emp) {
		//加载驱动,创建连接
		Connection connection = DCji.getConnection();
		PreparedStatement ps = null;
		try {
			//创建申明
		    connection = DCji.getConnection();
		
			String sql= "INSERT INTO t_employee (name,sex,age,dept_id) VALUES (?,?,?,?)";
			System.out.println(sql);
			//执行sql
			
			//预编译
			ps = connection.prepareStatement(sql);
			ps.setString(1, emp.getName());
			ps.setString(2, emp.getSex());
			ps.setInt(3, emp.getAge());
			ps.setInt(4, emp.getDeptId());
			
			
			
			return ps.executeUpdate();
		} catch (SQLException e) {
		//	e.printStackTrace();
			//抛异常
			throw new RuntimeException(e);
		}finally {
			DCji.release(null, ps, connection);
		}
		
	}

	@Override
	public int deleteEmployeeDao(int id) {
		//加载驱动,创建连接
				Connection connection = DCji.getConnection();
				PreparedStatement ps = null;
				try {
					//创建申明
				    connection = DCji.getConnection();
					String sql= "DELETE FROM t_employee where id =?  ";
					System.out.println(sql);
					//执行sql
					
					//预编译
					ps = connection.prepareStatement(sql);
					ps.setInt(1, id);
					
					return ps.executeUpdate();
				} catch (SQLException e) {
				//	e.printStackTrace();
					//抛异常
					throw new RuntimeException(e);
				}finally {
					DCji.release(null, ps, connection);
				}
	}

	@Override
	public int modifyEmployeeDao(Employee emp) {
		//加载驱动,创建连接
				Connection connection = DCji.getConnection();
				PreparedStatement ps = null;
				try {
					//创建申明
				    connection = DCji.getConnection();
					
					String sql= "UPDATE t_employee SET name = ? ,sex =? , age =? ,dept_id =?  where id =?" ;
							
					System.out.println(sql);
					//执行sql
					
					//预编译
					ps = connection.prepareStatement(sql);
					ps.setString(1, emp.getName());
					ps.setString(2, emp.getSex());
					ps.setInt(3, emp.getAge());
					ps.setInt(4, emp.getDeptId());
					ps.setInt(5, emp.getId());

					return ps.executeUpdate();
				} catch (SQLException e) {
				//	e.printStackTrace();
					//抛异常
					throw new RuntimeException(e);
				}finally {
					DCji.release(null, ps, connection);
				}
	}

	@Override
	public Employee load(int id) {
		//加载驱动,创建连接
		Connection connection = DCji.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null ;
		try {
			//创建申明
		    connection = DCji.getConnection();

			String sql= "select id,name,sex,age,dept_id FROM t_employee where id =? " ;
			System.out.println(sql);
			//执行sql
			
			//预编译
			ps = connection.prepareStatement(sql);
			ps.setInt(1, id);
			
			rs = ps.executeQuery(sql);
		//遍历打印
			//封装实例化
			Employee employee = new Employee();
			while(rs.next()) {
			employee.setId(rs.getInt("id"));
			employee.setName(rs.getString("name"));
			employee.setSex(rs.getString("sex"));
			employee.setAge(rs.getInt("age"));
			employee.setDeptId(rs.getInt("dept_id"));
			}
			return  employee  ;    //employee类型的返回值
		} catch (SQLException e) {
		//	e.printStackTrace();
			//抛异常
			throw new RuntimeException(e);
		}finally {
			DCji.release(rs, ps, connection);
		}
	}

	@Override
	public Vector<Employee> queryAll() {
		//加载驱动,创建连接
				Connection connection = DCji.getConnection();
				PreparedStatement ps = null;
				ResultSet rs = null ;
				try {
					//创建申明
				    connection = DCji.getConnection();
					String sql= "SELECT id, name, sex, age, dept_id FROM t_employee ";
					System.out.println(sql);
					//执行sql
					
					//预编译
					ps = connection.prepareStatement(sql);
					
					
					
					rs = ps.executeQuery();
				//遍历打印
					//封装实例化
					Vector<Employee> employees = new  Vector<Employee>();
					while(rs.next()) {
					Employee employee = new Employee();
					employee.setId(rs.getInt("id"));
					employee.setName(rs.getString("name"));
					employee.setSex(rs.getString("sex"));
					employee.setAge(rs.getInt("age"));
					employee.setDeptId(rs.getInt("dept_id"));
					employees.add(employee);
					}
					return  employees  ;    //employee类型的返回值
				} catch (SQLException e) {
				//	e.printStackTrace();
					//抛异常
					throw new RuntimeException(e);
				}finally {
					DCji.release(rs, ps, connection);
				}
	}

}

测试

import java.util.Vector;

import com.wenhe.dao.impl.EmployeeDaoimpl;
import com.wenhe.vo.Employee;

public class test {

	public static void main(String[] args) {
		
		/**
		 *  增
		 */
		/*
		EmployeeDaoimpl employeeDaoimpl = new EmployeeDaoimpl();
		Employee employee = new Employee();
		employee.setName("吕布");
		employee.setSex("M");
		employee.setAge(31);
		employee.setDeptId(2);
		
		employeeDaoimpl.saveEmployeeDao(employee);
		
		
		*/
		
		/**
		 * 
		 * 	删
		 */
		/*
		EmployeeDaoimpl employeeDaoimpl = new EmployeeDaoimpl();
		
		employeeDaoimpl.deleteEmployeeDao(19);
		*/
		
		
		
		
		/**
		 * 		单查询	
		 *  	单查询并更新表数据
		 *      
		 */
		
		EmployeeDaoimpl employeeDaoimpl = new  EmployeeDaoimpl();
		//取返回值
		Employee load = employeeDaoimpl.load(1);
		System.out.println(load.getName());
	//	单查询并更新表数据
//		Employee y = employeeDaoimpl.load(1);
//		y.setName("刘表");
//		employeeDaoimpl.modifyEmployeeDao(y);
		
		
		
		
		
		
		
		/**
		 * 
		 * 		多查询
		 */
		/*
		EmployeeDaoimpl employeeDaoimpl = new EmployeeDaoimpl();
		//取返回值
		Vector<Employee> queryAll = employeeDaoimpl.queryAll();
		for (Employee employee : queryAll) {
			System.out.println(employee.getName());
		}
		*/
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值